0
Follow
5
View

result of comparison against a string literal is unspecified (use strncmp instead) [-Wstring-compare] is the error i get please help me

csxpeter0 注册会员
2023-01-26 06:49
if(d=="Vayush" && e=="Vasireddy")

is comparing pointers to string literals, thats wrong because you want to compare contents and not addresses. The compiler is telling you to use the standard function strncmp instead of ==, in this case strcmp can do the job:

#include 
#include  // strcmp

int main(void) // Use a valid signature
{
    // Use meaningful names for your vars
    char usr[10];
    char pwd[10];

    // Do not use goto, loop until you get valid values
    while (1)
    {
        printf("me of the agent: ");
        // scanf return the number of input items successfully matched or EOF
        int n = scanf("%9s", usr); // %9s in order to avoid buffer overflows
        if (n == EOF) { break; }
        if (n == 1)
        {
            printf("Password: ");
            n = scanf("%9s", pwd);
            if (n == EOF) { break; }
            if ((n == 1) &&
                (strcmp(usr, "Vayush") == 0) &&
                (strcmp(pwd, "Vasireddy") == 0))
            {
                break;
            }
        }
        printf("wrong username and password\n");
    }
}

About the Author

Question Info

Publish Time
2023-01-26 06:49
Update Time
2023-01-26 06:49

Related Question