简体   繁体   English

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

[英](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. 因为UserNamePassWord是指向char数组的指针。

  1. scanf for %s takes a char array/pointer, not pointer to it. %s的scanf需要一个char数组/指针,而不是指向它的指针。 drop the & from the scanf statements. scanf语句中删除&
  2. You cannot compare strings with == . 您不能将字符串与==进行比较。 Use strcmp . 使用strcmp

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

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