Permission Grnted or Not
May 3, 2014 at 4:46pm UTC
Im trying to write a code if the user enter 10 username and 10 password and after the the 10 username and password has been entered it should ask for again to input and username and password to check if it is in the array and if the username and password is in the array it should display access granted and if both wrong it should display username and password do no match and if the username is correct and password is wrong it should display username doesnt match
Heres my code so far
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
string inputUser, inputPass;
int foundFlag, k;
string userName [10]; // userName sets the upper limit to the array
string passWord [10]; // passWord sets the upper limit to the array
for (k = 0; k < 10; k++){
cout <<"Enter the Username: " ;
cin >> userName[k];
cout <<"Enter the Password: " ;
cin >> passWord[k];}
cout <<"Input User: " ;
cin >> inputUser;
foundFlag = false ;
for (k = 0; k < 10; k++){
if (inputUser == userName[k])
foundFlag = true ;
cout <<"Input Pass: " ;
cin >> inputPass;
}
if (inputPass == passWord[k])
cout <<"Access granted." ;
else
cout <<"Username and password do not match." ;
//}
if (foundFlag = false )
cout <<"Username not Found" ;
}
May 3, 2014 at 6:03pm UTC
If you hope to have a question answered, you should probably ask one...
May 3, 2014 at 6:31pm UTC
A little cleanup to help you out:
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
#include <iostream>
#include <string>
using namespace std;
int main()
{
string inputUser, inputPass;
int foundFlag, k;
string userName [10]; // userName sets the upper limit to the array
string passWord [10]; // passWord sets the upper limit to the array
for (k = 0; k < 10; k++)
{
cout <<"Enter the Username: " ;
cin >> userName[k];
cout <<"Enter the Password: " ;
cin >> passWord[k];
}
cout <<"Input User: " ;
cin >> inputUser;
foundFlag = false ;
for (k = 0; k < 10; k++){
if (inputUser == userName[k])
{
foundFlag = true ;
cout <<"Input Pass: " ;
cin >> inputPass;
if (inputPass == passWord[k])
cout <<"Access granted." ;
else
cout <<"Username and password do not match." ;
break ;
}
}
if (foundFlag == false )
cout <<"Username not Found" ;
}
The trick is to think of things in groups. You had the right idea with the brace on line 24 -- don't move it to 19.
Group all the stuff that happens for each thing you test. Notice how I added a
break to get out of the loop if the username was found.
Also, use braces {} consistently. In particular, lines 6 and 10: don't do that. It's an evil style and will mess you up.
One last thing: 'userName' and 'passWord' are arrays -- they don't set upper limits on anything.
Hope this helps.
@
jmadsen
True... but the question about it all is very strongly implied. "Code should do this [but it doesn't, so I'm posting for help]."
Last edited on May 3, 2014 at 6:33pm UTC
May 5, 2014 at 5:38pm UTC
thanks all fo help and big thanks to @ Duoas
Topic archived. No new replies allowed.