c

(C Programming) User name and Password Identification


Why do I get format '%s' expects argument of type 'char*'? How should I fix the problem?

Here are my codes:

char UserName[] = "iluvcake";
scanf("%s", &UserName);
printf("Please enter your password: \n");
char PassWord[] = "Chocolate";
scanf("%s", &PassWord);
    //if...else statement to test if the input is the correct username. 
    if (UserName == "iluvcake") 
    {
     if (PassWord == "Chocolate"){
     printf("Welcome!\n");
    }
    }else
    {
     printf("The user name or password you entered is invalid.\n");
    }

Solution

    1. scanf for %s takes a char array/pointer, not pointer to it. drop the & from the scanf statements.
    2. You cannot compare strings with ==. Use strcmp.