2

为什么我会得到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

5 回答 5

4

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

scanf( "%s", UserName );
于 2013-04-03T14:50:12.270 回答
3
#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();
}

:)

于 2013-12-30T20:38:16.033 回答
0

一定是

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

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

于 2013-04-03T14:50:11.097 回答
0
  1. scanf for %s 需要一个字符数组/指针,而不是指向它的指针。&scanf语句中删除。
  2. 您不能将字符串与==. 使用strcmp.
于 2013-04-03T14:52:05.293 回答
0
#include <stdio.h>

int main(){
int serial;
int code;

printf("Type the Serial Here: ");
scanf("%d", &serial);

if(serial== 1590){
    printf("Almost there, now type the code: ");
    scanf("%d", &code);
    if(code==2359)
    {
            printf("\nYOU ARE DONE, ENJOY");
    }
    else{
            printf("TRY AGAIN NEXT TIME");
    }


}
    else{
        printf("OPPS");
}
于 2022-02-10T06:10:13.463 回答