I am writing a program that should accept value of username and password. The system must check if the entered username and password are valid by checking if the entered values are matched to what are defined in the system. Upon logging in, the system can proceed on asking some information to the users. Only 3 attempts will be allowed, meaning if the user already reached 3 tries of entering invalid username and password the system will display "You exceeded the number of times that you need to input the correct username and password. The system now is locked!" Use appropriate data types in declaring your variables.
Equality == isn't determined that way with char[] strings. You have to use strcmp() function
You can lookup the reference on this website to see how it works.
#include<iostream.h>
#include<string.h>
#include<conio.h>
int main(){
char Username[30]="sammasangcap",Password[30]="abc123",username[30], password[30],age[2],name[60],sex[6],address[100];
unsignedint attempts = 0;
do
{
cout<<"\nUsername:";
cin>>username;
if(strlen(username) <4)
{
cout<<"\nUsername length must be atleast 4 characters";
}
else
{
cout<<"\nPassword:";
cin>>password;
}
if(!( (strcmp(username,Username) == 0) && (strcmp(password,Password) == 0)))
{
cout<<"\nWrong username or password";
attempts++;
if(3 == attempts)
{
cout<<"You exceeded the number of times that you need to input the correct username and password. The system now is locked!";
return 0;
}
}
else
{
cout<<"\nLogin Successful!";
cout<<"\nPlease input the following information:";
cout<<"\nName";
cin>>name;
cout<<"\nAge:";
cin>>age;
cout<<"\nSex:";
cin>>sex;
cout<<"\nAddress:";
cin>>address;
break;
}
}while(1);
getch();
return 0;
}
This is your program which I ran to make a test. Please note I had to change a few things to suit the compiler here online but they are not errors if the programs run OK on your system:
#include<iostream>//.h>
#include<string.h>
//#include<conio.h>
usingnamespace std; ///
int main(){
char Username[30]="sammasangcap",Password[30]="abc123",username[30], password[30],age[2],name[60],sex[6],address[100];
unsignedint attempts = 0;
do
{
cout<<"\nUsername:";
cin>>username;
if(strlen(username) <4)
{
cout<<"\nUsername length must be atleast 4 characters";
}
else
{
cout<<"\nPassword:";
cin>>password;
}
if(!( (strcmp(username,Username) == 0) && (strcmp(password,Password) == 0)))
{
cout<<"\nWrong username or password";
attempts++;
if(3 == attempts)
{
cout<<"You exceeded the number of times that you need to input the correct username and password. The system now is locked!";
return 0;
}
}
else
{
cout<<"\nLogin Successful!";
cout<<"\nPlease input the following information:";
cout<<"\nName";
cin>>name;
cout<<"\nAge:";
cin>>age;
cout<<"\nSex:";
cin>>sex;
cout<<"\nAddress:";
cin>>address;
break;
}
}while(1);
//getch();
return 0;
}
This is the output I get when I wanted to deliberately make it fail:
Username:test
Password:fail
Wrong username or password
Username:test
Password:failagain
Wrong username or password
Username:test3
Password:fail3
Wrong username or passwordYou exceeded the number of times that you need to input the correct username and password. The system now is locked!