Jump to content

Help with if statement for strcmp

- - - - -

  • Please log in to reply
4 replies to this topic

#1
kristyy_mariee

kristyy_mariee

    Newbie

  • Members
  • Pip
  • 6 posts
I have a text file with activities for a month like this: January 1 Monday Brushing_teeth 5
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);

         

	}



#2
DarkLordofthePenguins

DarkLordofthePenguins

    Programming Expert

  • Members
  • PipPipPipPipPipPip
  • 409 posts
I'm not sure of your exact problem, but when you use fgets() and then fscanf(), you're advancing one line with each of them, so you're skipping a line. There may be other errors.

I suggest you learn to debug code. If you find a problem, take the program apart and test each section of it, using printfs to tell you the exact state of the program at any given time. For example, if you get a segfault somewhere but you don't know where you can do something like this:


printf( "No segfault before scanf\n" );

scanf( "%d", num );

printf( "No segfault after scanf\n" );


If you want to know what exactly what text is being read you could tell the program to print the line after each read. It may sound tedious, but it will save you the trouble of having to go to CodeCall every time you get an error (something that happens a lot unless you're really careful).

Also, I suggest you use a comprehensive source for learning C. This would include something like The C Programming Language or an O'Reilly book. A web-based tutorial, a Learn C in X Minutes/Hours/Days book, etc. won't cut it. I'm not assuming that that's what you indeed are using, but I just want to make sure.
Programming is a journey, not a destination.

#3
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
This:
while(fgets(line, sizeof(line), search) != NULL)

will advance the pointer to the end of the file. Before going into another loop you need to set the pointer to point to the top of the file
rewind(search);

I ran your program with rewind(search) after the first loop. This is the output.
Welcome to Track Me
Below is the list of all compulsions patients have done this month
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
Enter Patients Name to access file with extension .txtfilename.txt
Month  Date  Day  Compulsion  Repetitions
total amount of reps for Brushing Teeth: 37
Total amount of reps for Washing hands are : 15

Process returned 0 (0x0)   execution time : 358.750 s
Press any key to continue.

I did not check the accuracy of the numbers.
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:

#4
kristyy_mariee

kristyy_mariee

    Newbie

  • Members
  • Pip
  • 6 posts
yeah the rewind def works, thankq you :)

#5
fread

fread

    Programming God

  • Members
  • PipPipPipPipPipPipPip
  • 787 posts
Anytime!
Perfection of means and confusion of ends seem to characterize our age. Albert Einstein :confused:




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users