#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0

    Problems with strcmp ()


    Hey there people.. As you will soon see I am a total newbie in C programming. As the title suggests I am having problems with the strcmp () function, since it just doesn't seem to work. I am guessing I am using it wrong so this is the code I am using (a function of it) so if you could please help me out I'd be greatful.
    Code:
    void search (void)
    {
    	int i,a,c=0;
    	char AM_dumb[8];
    	printf ("Please input the AM you would like to search for\n");
    	gets (AM_dumb);
    	for (i=0;i<3;i++);
    	{
    		a=strcmp (AM_dumb,AM[i].am);
    		if (a == 0)
    		c=i;
    	}
    	if (c!=0)
    	printf ("The student's: AM is %s ,  final grade is %d, essay grade is %d, exam grade is %d\n",AM_dumb,AM[c].final_grade,AM[c].essay_grade,AM[c].exam_grade);
    	else 
    	printf ("The AM you searched for does not exist\n");
    		main ();
    }
    P.S: If I could avoid using pointers since I really can't understand their logic just yet, it would be great.

    Thanks in advance,
    Kostas
  2. #2
  3. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Please be more specific about what kind of problem you have. How do you know there is a problem? unexpected result?

    But you do have a flaw in you logic, just not sure if it relates to you problem.
    You logic is this:
    1. start a loop where i start with 0
    2. compare the array at index i (this can be 0)
    3. check that it is a match (correct)
    4. save value of i into c (this can be 0)
    5. check c is not 0 (wrong, 0 can be valid)

    To fix it, change step 4 to set the value of c to be 1 instead of i

    As a guideline, you should try to avoid one letter as variable names outside of loop index. e.g. instead of c you could have used matchFound or something similar.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    This isn't what I'm trying to do.. I have initialized C=0. Then if the dumb variable matches the one in the array it "saves" it's location (i) to c. Then it prints the 1st,2nd,3rd,...,c place of the array. If I change c=i to c=1 it will always print the 2nd variable in the array.
    The reason I use if (c!=0)
    else
    is because if the match has not been found, the value of C will never change, thus remaining 0. This means that I am not using C as a flag of some sorts. I use it as a place indicator.

    Now about me not being clear on the problem, you are absolutely right(sorry for that).
    My problem is that even when I know I give the exact same string, it just never seems to match it with the one in the array. If I'm still not clear please do tell me as I want to help you, help me.


    P.S: I don't use full named variables in this one because it is just a draft. When I have solved all of its problems I will rename all the single lettered variables.
  6. #4
  7. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    My point was that you can not check c not being 0 as this value is a valid result. With current check, a match in the first place will be displayed as a non-match.

    Have you tried to print out the user input and the array value you compare?

    You shouldn't call main() but instead return back to it.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    Yeah I just realized what you meant and you are absolutely right. I am now using C as a match indicator. However the problem of strcmp () still remains and I don't see any way to solve it. I could post my code (although it's a little big - about 200 lines -) because I can't find anything wrong with the function I sent you.. Also after executing this function , the program executes another function (not main) for no damn reason. So let me know if you would have the time and energy to read my code and help me out.
    Thanks in advance
  10. #6
  11. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Have you validated (print out) the values of AM[i].am and AM_Dumb, as I suggested in last post?

    Just a thought, you could try with scanf instead of gets:
    Code:
    scanf ("%s",AM_Dumb);
    (checking the return value of scanf should be added)

    If you have another problem, I think you should create a new thread where you post the description and related code for this problem.
    Last edited by MrFujin; February 11th, 2013 at 06:29 AM.
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    let me tell you what I want to do exactly. The user will input a string (AM_dumb) and then the function will search this string in a string array inside a structure. So when it does find it, I want to print out everything else on the specific place of the structure too.. Which means that if it finds it in i.e the 3rd place, it will print out the student's essay grade, exam grade and final grade.
    As for printing out the user input and the array I don't see the necessity of it, or I don't understand what you mean..
    If I could please trouble you a bit more with explaining to me what you meant , I'd be greatful.

    P.S I have already tried using scanf and it still wouldn't work, however I'll give it another shot.
  14. #8
  15. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Originally Posted by kostaskol
    As for printing out the user input and the array I don't see the necessity of it, or I don't understand what you mean..
    If I could please trouble you a bit more with explaining to me what you meant , I'd be greatful.
    It is part of debugging. How do you know that the (expected) value does exist in the array? by printing it out on the screen.
    The same is true for the user input.

    P.S I have already tried using scanf and it still wouldn't work, however I'll give it another shot.
    OK, it was just a thought.

    Please remember to post the new code if you make any changes to it.
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    I know that it's there because I fill up the array myself.. I'm just testing the code for now.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    int sum=0;
    int test (int);
    void format1 (int);
    void format2 (int);
    void format3 (int);
    char eisag_eksetaz (int);
    int eisag_eksetaz_essay_grade (int);
    void anazitisi (void);
    void statistics (int);
    struct student
    {
    	char am[8];
    	float final_grade;
    	float exam_grade;
    	float essay_grade;
    } AM[3];
    int b=-1,sum_test=0,examined_sum=0,counter=-1;
    
    
    int main (void)								//Students' Choices
    {
    int choice,a,i;
    	
    	printf ("1. Eisagwgi eksetazomenwn (A.M, Bathmos Ergasias\n 2. Dieksagwgi eksetasis \n 3. Anazitisi kai etupwsi tou vathmou enos foititi (Vathmos ergasias, Vathmos eksetasis, Telikos vathmos) me vasi to A.M \n 4. Ektupwsi tis telikis listas vathmologias (A.M,vathmos ergasias, vathmos eksetasewn, telikos vathmos. \n 5. Ektupwsi statistikvn stoixeiwn vathmologias(arithmos kai pososto epituxwntwn foititwn sto mathima. \n 6. Eksodos\n");
    	scanf ("%d",&choice);
    	getchar ();
    	if (choice==6)
    	exit (0);
    	else if (choice==2)
    	{
    	counter++;
    	test (counter);
    	AM[counter].final_grade=0.75*AM[counter].exam_grade+0.25*AM[counter].essay_grade;
    	}
    	else if (choice==1)
    	{
    	
    	eisag_eksetaz (b);
    	eisag_eksetaz_essay_grade (b);
        }
        else if (choice==4)
        {
        	for (i=0;i<3;i++)
        		printf ("AM: %s, Telikos Vathmos: %f, Vathmos Eksetasis: %f, Vathmos ergasias: %f\n",AM[i].am,AM[i].final_grade,AM[i].exam_grade,AM[i].essay_grade);
        		getch ();
    			main ();
        }
        else if (choice==3)
        anazitisi ();
        if (choice==5)
        statistics (counter);
    }
    	
    int test (b)					//Testing
    {
    sum_test++;
    if (sum_test==4) sum_test=1;
    	if (sum_test==1)
    	format1 (b);
    	if (sum_test==2)
    	format2 (b);
    	if (sum_test==3)
    	format3 (b);
    }
    
    
    
    void format1 (counter)
    {
    	int a,b;
    	printf ("Question 1\n 1.Answer 1\n 2.Answer 2 \n 3.Answer 3\n");
    	scanf ("%d",&a);
    	if (a==2)
    	AM[counter].exam_grade++;
    	printf ("Question 2\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==1)
    	AM[counter].exam_grade++;
    	printf ("Question 3\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==3)
    	AM[counter].exam_grade++;
    	printf ("Question 4\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==2)
    	AM[counter].exam_grade++;
    	printf ("Question 5\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==1)
    	AM[counter].exam_grade++;
    	main ();
    }
    
    void format2 (counter)
    {
    	int a;
    	printf ("Question 1\n 1.Answer 1\n 2.Answer 2 \n 3.Answer 3\n");
    	scanf ("%d",&a);
    	if (a==2)
    	AM[counter].exam_grade++;
    	printf ("Question 2\n 1. Answer 1\n 2. Answer 2 \n 3. Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==2)
    	AM[counter].exam_grade++;
    	printf ("Question 3\n 1. Answer 1\n 2. Answer 2 \n 3. Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==3)
    	AM[counter].exam_grade++;
    	printf ("Question 4\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==1)
    	AM[counter].exam_grade++;
    	printf ("Question 5\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==3)
    	AM[counter].exam_grade++;
    	main ();
    }
    
    void format3 (counter)
    {
    	int a;
    	printf ("Question 1\n 1.Answer 1\n 2.Answer 2 \n 3.Answer 3\n");
    	scanf ("%d",&a);
    	if (a==1)
    	AM[counter].exam_grade++;
    	printf ("Question 2\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==3)
    	AM[counter].exam_grade++;
    	printf ("Question 3\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==3)
    	AM[counter].exam_grade++;
    	printf ("Question 4\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==2)
    	AM[counter].exam_grade++;
    	printf ("Question 5\n 1. Answer 1\n 2. Answer 2 \n Asnwer 3\n");
    	scanf ("%d",&a);
    	if (a==2)
    	AM[counter].exam_grade++;
    	main ();
    }
    
    char eisag_eksetaz (b) 
    {
    	printf ("Please input your AM\n");
    		scanf ("%s",&AM[b].am);
    		eisag_eksetaz_essay_grade (b);
    }
    
    
    int eisag_eksetaz_essay_grade (b){
    	int a;
    	do
    	{
    	printf ("Please input your essay grade\n"); 
    	scanf ("%f",&AM[b].essay_grade);
    	}while (AM[b].essay_grade >10 || AM[b].essay_grade<0);
    	main ();
    }
    
    
    
    void anazitisi (void)
    {
    	int i,a,c=0;
    	char AM_dumb[8];
    	printf ("Please input the AM you would like to search for\n");
    	scanf ("%s",AM_dumb);
    	for (i=0;i<3;i++);
    	{
    		a=strcmp (AM_dumb,AM[i].am);
    		if (a == 0)
    		{printf ("The student's: AM is %s ,  final grade is %d, essay grade is %d, exam grade is %d\n",AM_dumb,AM[c].final_grade,AM[c].essay_grade,AM[c].exam_grade);
    		c=1;}
    		
    	}
    	if (c=0)
    	printf ("The AM you searched for does not exist\n");
    	return;	
    }
    
    void statistics (counter)
    {
    	int i,success=0;
    	float stat=0;
    	for (i=0;i<counter;i++)
    		if (AM[i].final_grade>=5)
    		success+=1;
    	if (counter!=-1)
    	stat=(success/counter)*100;
    	else
    	printf ("No students were examined\n");
    	printf ("The overall number of students examined is %d, the percentage of successful students was %f, which means that %d students were successful\n",examined_sum,stat,success);
    	getch ();
    	main ();
    	
    }

    So here is the whole code.. Some of the stuff in printf is in greek just let me know if you any questions.

    Also I don't really know how to debug a program.. Is there any way to fix it without using a debugger?
  18. #10
  19. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Try to add this before strcmp(): (untested)
    Code:
    printf("%d: Match '%s' (AM_dumb) against '%s' (AM[i].am )\n",i,AM_dumb,AM[i].am);

    When you run the code, how does the output looks like for the above line?

    BTW, you code at a match is indexing the AM with c instead of i.
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    it says "match '8120000' (AM_dumb) against ' ' (AM[i].am"
    which means that it doesn't recognize the array string?
  22. #12
  23. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Or something goes wrong when you try to insert in the function eisag_eksetaz
    You should check/validate the return value of scanf.

    Can see you call eisag_eksetaz_essay_grade (b); twice? in eisag_eksetaz and in main.

    I would also strongly recommend that you remove all the call to main.
    If something have to be repeated, it is better to place it in a loop.
  24. #13
  25. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    so if I were to remove all the calls to main, should I replace it with return;? or should I use return (main) or something? ( As I already told you I am a newbie...). Also I did find the problem with calling eisage_eksetaz twice so I erased the one in main..
    But my problem still remains. Why can strcmp match the user input string with the one in the array/structure...?
  26. #14
  27. Lord of the Dance
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Oct 2003
    Posts
    3,663
    Rep Power
    1958
    Just call return;

    It noticed me if you only got one match line.
    Just tried to compile and run you program and saw I had been blinded.

    You have a ; after you for loop:
    Code:
    for (i=0;i<3;i++);
    This mean the loop is a separate command completed before the next block with the comparison.

    Regarding eisage_eksetaz, i would have kept the one in the main.
  28. #15
  29. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    8
    Rep Power
    0
    sweet jesus you are right... I really can't believe that this was the problem this whole time.. Thank you so much for putting up with me not knowing C very well all this time..

IMN logo majestic logo threadwatch logo seochat tools logo