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");
}
&UserName is a pointer to an array of char (ie, a char**). You should use
scanf( "%s", UserName );
#include<stdio.h>
#include<conio.h>
#include<string.h>
main(){
char name[20];
char password[10];
printf("Enter username: ");
scanf("%s",name);
printf("Enter password: ");
scanf("%s",password);
if (strcmp(name, "Admin") == 0 && strcmp(password, "pass") == 0)
printf("Access granted\n");
else printf("Access denied\n");
getch();
}
:)
Must be
scanf("%s", UserName);
scanf("%s", PassWord);
because UserName
and PassWord
are pointers to char
arrays.
&
from the scanf
statements. ==
. Use strcmp
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.