[英](C Programming) User name and Password Identification
為什么我得到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");
}
&UserName是指向char數組(即char **)的指針。 你應該用
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();
}
:)
一定是
scanf("%s", UserName);
scanf("%s", PassWord);
因為UserName
和PassWord
是指向char
數組的指針。
scanf
語句中刪除&
。 ==
進行比較。 使用strcmp
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.