January 1 Monday Washing_hands 5
January 1 Monday Twitching 6
January 1 Monday Lock_checking 40
January 2 Tuesday Brushing_teeth 10
January 2 Tuesday Eating_hair 10
January 2 Tuesday Twitching 10
January 3 Wednesday Brushing_teeth 5
January 3 Wednesday Grouping_food 3
January 4 Thursday Brushing_teeth 6
January 4 Thursday Washing_hands 10
January 4 Thursday Washing_Face 5
January 5 Friday Brushing_teeth 4
January 5 Friday Grouping_food 4
January 6 Saturday Brushing_teeth 7
January 6 Saturday Twitching 6
January 7 Sunday Brushing_teeth 5
January 8 Monday Brushing_Teeth 3
The goal is to be able to search through each line of the file and compare the activity string to the one saved in the array comp[]. I need to able to add the total repetitions for each activity.
So far only one of my if statements work, the first one. The other one gives 0. I figure it has something to do with having to return to the top of the file to do the second if statement, but how?
#include <stdio.h>
#include <string.h>
int main()
{
int reps;
char comp[15][30]={"Brushing_teeth","Washing_hands","Eating_hair","Scratching","Biting_Nails","Cleaning_House","Changing_clothes","Twitching","Scrubbing_Floor","Skipping_last_step","Cracking_Knuckles","Grouping_Food","Washing_Face","Lock_Checking","Biting_Inside_Of_Mouth"};
char name[20];
char compulsions[40];
char month[10], day[20];
int date;
int i,j;
int total0=0;
int total1=0;//,total2,total3,total4,total5,total6,total7,total8,total9,total10,total11,total12,total13,total14=0;
printf("Welcome to Track Me\n");
printf("Below is the list of all compulsions patients have done this month\n");
for (int i = 0; i < 15; ++i)
printf("%s \n", comp[i]);
//to search for patient file
printf("Enter Patients Name to access file with extension .txt");
scanf("%s",name);
FILE *search;
search = fopen(name,"r");
if (search==NULL ){
printf("File could not be opened");
}
printf("Month Date Day Compulsion Repetitions\n");
char line[150];
while(fgets(line, sizeof(line), search) != NULL)
{
fscanf(search,"%s %d %s %s %d", month, &date, day, compulsions, &reps) ;
if(strcmp(comp[0],compulsions) == 0)
{
total0=total0+reps;
}
}
printf("total amount of reps for Brushing Teeth: %d\n", total0);
fflush;
while(fgets(line, sizeof(line), search) != NULL)
{
fscanf(search,"%s %d %s %s %d", month, &date, day, compulsions, &reps) ;
if(strcmp(comp[1],compulsions) == 0)
{
total1=total1+reps;
}
}
printf("Total amount of reps for Washing hands are : %d\n",total1);
fclose(search);
}


Sign In
Create Account

Back to top









