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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15790621/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 05:55:58  来源:igfitidea点击:

(C Programming) User name and Password Identification

c

提问by smnthLAW

Why do I get format '%s' expects argument of type 'char*'? How should I fix the problem?

为什么我得到format '%s' expects argument of type 'char*'?我应该如何解决问题?

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");
    }

采纳答案by anishsane

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

回答by K Scott Piel

&UserName is a pointer to an array of char (i.e., a char**). You should use

&UserName 是指向字符数组(即字符**)的指针。你应该使用

scanf( "%s", UserName );

回答by Maky

#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();
}

:)

:)

回答by Johnny Mnemonic

Must be

必须是

scanf("%s", UserName);
scanf("%s", PassWord);

because UserNameand PassWordare pointers to chararrays.

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