为什么我会format '%s' expects argument of type 'char*'
?我该如何解决这个问题?
以下是我的代码:
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");
}
答案 0 :(得分:3)
& UserName是指向char数组的指针(即char **)。你应该使用
scanf( "%s", UserName );
答案 1 :(得分:2)
#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();
}
:)
答案 2 :(得分:0)
必须
scanf("%s", UserName);
scanf("%s", PassWord);
因为UserName
和PassWord
是指向char
数组的指针。
答案 3 :(得分:0)
&
语句中删除scanf
。==
进行比较。使用strcmp
。