1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
|
#include <iostream>
using namespace std;
string username[10]={"user1", "user2", "user3", "user4", "user5", "user6", "user7", "user8", "user9", "user10"};
string password[10]={"pass1", "pass2", "pass3", "pass4", "pass5", "pass6", "pass7", "pass8", "pass9", "pass10"};
string user="";
string pass="";
int LoginCheck (string username[], string password[], string user, string pass)
{
for (int i=0; i<10; i++)
{
if (user==username[i] && pass==password[i])
return i+1; // I like to use 0 as a false return
}
return 0;
}
int main()
{
int loginattempts=0;
while (LoginCheck(username, password, user, pass)==0)
{
loginattempts++;
cout << "Username: ";
cin >> user;
cout << "Password: ";
cin >> pass;
if (LoginCheck(username, password, user, pass)!=0)
{
cout << "Welcome " << user << "." << endl;
break; // not needed, braces aren't needed in this block either as only 1 line then
}
else if (loginattempts==3)
{
cout << "Maximum login attempts exceeded." << endl;
break;
}
else
{
cout << "Invalid username/password combination" << endl;
}
}
int a;
cin >> a; // this has no purpose other than stopping the program closing automatically
return 0;
}
|