Why do I get 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"); } 25 Answers
&UserName is a pointer to an array of char (i.e., a 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(); } :)
- scanf for %s takes a char array/pointer, not pointer to it. drop the
&from thescanfstatements. - You cannot compare strings with
==. Usestrcmp.
Must be
scanf("%s", UserName); scanf("%s", PassWord); because UserName and PassWord are pointers to char arrays.
#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"); } 2