(C编程)用户名和密码识别

时间:2013-04-03 14:47:51

标签: c

为什么我会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");
    }

4 个答案:

答案 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);

因为UserNamePassWord是指向char数组的指针。

答案 3 :(得分:0)

  1. scanf for%s采用char数组/指针,而不是指向它的指针。从&语句中删除scanf
  2. 您无法将字符串与==进行比较。使用strcmp