Hello,

I want to know how to compare a string value which is input by the user. The following code gives an error saying "Result of comparison against string literal is unspecified (use strncmp instead)".

while(valid == 0) {
        printf("Yes-No-Question\n");
        scanf("%s", &yesno);
        valid = 1;
        if(yesno != "yes" || yesno != "no") {
            printf("\nERROR: Please enter yes or no.\n");
            valid = 0;
        }
    }

I want the program to continue only if the user enters either yes or no. Any other value will keep the loop going on.

Does anyone know how to solve this?

Thanks in advance.

Recommended Answers

All 7 Replies

Yes, we all do. The error message already told you to call strcmp() to compare two strings.

Yes, we all do. The error message already told you to call strcmp() to compare two strings.

Hello Ancient Dragon, thanks for the fast reply.

I have been googling for a while now but still don't get this small code to run.

So far I know that when using strncmp, you must include <string.h>, but I don't know how I could compare the input to "yes" or "no".

Any other hints/answers?

Thanks :)

You can't compare strings - you have to compare each char, one at a time. Include string.h and use strcmp(string1, string2).

strcmp(string1Name, string2Name) will return one of three values:

1) a positive number
2) zero
3) a negative number

That return tells you what the comparison of strings revealed:

1) string1 is greater than string2
2) the strings are equal
3) string2 is greater than string1

The comparison is made, based on your system's character set, not strictly according to the dictionary rules of alphabetizing. (not quite a lexicographic comparison)

If you don't have a good C reference book or help files, bookmark a C reference site in your browser. You will need it.

I have done some further research and am close to an answer. Here is what I currently got:

#include <stdio.h>
#include <string.h>

int main() {
    int valid = 0;
    char str[3];
    char str1[] = "yes";
    char str2[] = "no";

    while(valid == 0) {
        printf("\nIs this code correct?\n(yes or no)\n");
        scanf("%s", &str);
        valid = 1;
        if(strcmp(str1,str) != 0 || strcmp(str2,str) != 0) {
            printf("\nERROR: Please enter yes or no.\n");
            valid = 0;
        }

But the program keeps on looping even though yes or no is input.

On line 14 use && (and) operator instead || (or) operator. You want to find out if str is not equal to str1 AND if it is not equal to str2.

But strcmp() does a case sensitive comparison, meaning that Yes is not the same as yes or yEs or yeS. You should use a case insensitive comparison. One such function is stricmp(), which may not be available on all compilers. Another way to do that is to convert the entire string to either upper or lower case, depending on the string that you want to compare it with. To do that just loop through the string and call toupper() or tolower() for each character.

On line 14 use && (and) operator instead || (or) operator. You want to find out if str is not equal to str1 AND if it is not equal to str2.

But strcmp() does a case sensitive comparison, meaning that Yes is not the same as yes or yEs or yeS. You should use a case insensitive comparison. One such function is stricmp(), which may not be available on all compilers. Another way to do that is to convert the entire string to either upper or lower case, depending on the string that you want to compare it with. To do that just loop through the string and call toupper() or tolower() for each character.

Oh my, I can't believe I was desperatly struggling to find an answer for hours when the problem simply was changing || (or) to && (and).

Thanks a lot guys, and thanks a lot Ancient Dragon.

I will research a bit more on case sensitive comparison. The stings in C got me hooked.

DELETED - Guess I was too sleepy when asking this question :)

Anyways, problem is solved. I appreciate your help a lot, guys.

Cheers

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.