Search code examples
cstringverification

How can I verify if one string variable is equal to a specific string that is not a variable in a do while in C language?? (not compare two strings)


I have already read the C string function man pages. The best thing I found was "strcmp" and "strncmp". But that's not what I want because it compares two strings. I just want to compare one like this...

char input[3];
    do {
        //important code
        printf ("You want to continue??);
        fflush(stdin);
        gets(input);
       } while (input == "yes" || "Yes);

It doesn't work. I would need to not use string and just a char like: "y" for yes. Is there a way I can do what I want? How can I compare just this one string with these values?


Solution

  • If you want to make string comparison, use strcmp. You will compare just one variable, as you want it, but with two different values. Change line :

    while (input == "yes" || "Yes");
    

    to :

    while ( strcmp(input,"yes") == 0 || strcmp(input, "Yes") == 0);
    

    Also, change :

    char input[3];
    

    to :

    char input[4];
    

    as you need to take into account the terminating \0 character.