Thread: Using strcmp ?

  1. #1
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61

    Angry Using strcmp ?

    Code:
    #include <stdio.h>
    #include <string.h>
    
    int main()
    {
        char myfruit[] = "apple";
        char guessing[20];
        
            printf("Guess my favorite fruit? ");
            scanf("%20s", guessing);
        
            if (strcmp (myfruit, guessing) == 0)
                puts("Correct answer!");
            else ("not correct");
            
            fflush(stdin);
        
        getchar();
    
    }
    need some help here , thank you

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Just from looking:

    1. The case where it isn't apple - I think you meant to call puts("not correct"); but if you look carefully, you did not.

    2. There is the chance that input which contains "apple" still does not compare with apple. Consider cases where the user accidentally types something before or after apple, or perhaps the user types just enough characters that there is not room for the scanf() function to append a zero to the string. Yes, that can happen.

    3. fflush(stdin); does not work everywhere.

    That said, something like this should work within reason.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void) {
    	const char myfruit[] = "apple";
    	char guessing[20];
    
    	printf("Enter fruit: ");
    	scanf("%19s", guessing);
    	if (strcmp(myfruit, guessing) == 0) {
    		puts("Correct!");
    	}
    	else {
    		puts("not correct");
    	}
    	return 0;
    }
    Last edited by whiteflags; 01-16-2015 at 11:16 PM.

  3. #3
    Registered User fouzimedfouni's Avatar
    Join Date
    Oct 2014
    Posts
    61
    Quote Originally Posted by whiteflags View Post
    Just from looking:

    1. The case where it isn't apple - I think you meant to call puts("not correct"); but if you look carefully, you did not.

    2. There is the chance that input which contains "apple" still does not compare with apple. Consider cases where the user accidentally types something before or after apple, or perhaps the user types just enough characters that there is not room for the scanf() function to append a zero to the string. Yes, that can happen.

    3. fflush(stdin); does not work everywhere.

    That said, something like this should work within reason.
    Code:
    #include <stdio.h>
    #include <string.h>
    int main(void) {
        const char myfruit[] = "apple";
        char guessing[20];
    
        printf("Enter fruit: ");
        scanf("%19s", guessing);
        if (strcmp(myfruit, guessing) == 0) {
            puts("Correct!");
        }
        else {
            puts("not correct");
        }
        return 0;
    }
    thank you my friend I compared your Code to mine and deleted some of your stuff like CONST and I added FFLUSH, and GETCHAR even I know a lot of compilers warning from using them, but I tried them though cause I wanted to know where did I go wrong and sure enough I forget to put something so simple which it (puts("not correct"); ) for ELSE ....lol
    thank you for your response

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp help...
    By Wiiplayer12 in forum C++ Programming
    Replies: 4
    Last Post: 05-04-2008, 10:24 PM
  2. help with strcmp
    By blork_98 in forum C Programming
    Replies: 8
    Last Post: 02-21-2006, 08:23 PM
  3. strcmp
    By kryonik in forum C Programming
    Replies: 9
    Last Post: 10-11-2005, 11:04 AM
  4. strcmp
    By joed in forum C Programming
    Replies: 4
    Last Post: 03-14-2005, 12:35 PM
  5. strcmp
    By Rob J in forum C++ Programming
    Replies: 1
    Last Post: 11-03-2004, 11:44 PM