繁体   English   中英

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

[英](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);

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM