(C Programming) User name and Password Identification

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

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

:)

  1. scanf for %s takes a char array/pointer, not pointer to it. drop the & from the scanf statements.
  2. You cannot compare strings with ==. Use strcmp.
0

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

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

You Might Also Like