[英](C Programming) User name and Password Identification
Why do I get format '%s' expects argument of type 'char*'
? 为什么我得到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**). &UserName是指向char数组(即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. 因为UserName
和PassWord
是指向char
数组的指针。
&
from the scanf
statements. 从scanf
语句中删除&
。 ==
. 您不能将字符串与==
进行比较。 Use strcmp
. 使用strcmp
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.