+ 2

Password creating and testing

Hi I have tried to write this code that writes a new password and renters it to check if its right #include <iostream> using namespace std; int main() { int pass, pass1; int loo = 0; cout<<"enter password" <<endl; cin >> pass; cout<< "renter password" <<endl; cin>> pass1; if ( pass != pass1 ){ if (loo<3){ cout<<"retry"; loo=1+loo; cin>> pass1; } else { cout<<"failed"; } } else{ cout<<"congrats"; } }

28th May 2017, 7:42 PM
Khalid Fattoum
3 odpowiedzi
+ 5
Good work Khalid. We only have three chances!
28th May 2017, 7:48 PM
Babak
Babak - avatar
+ 4
This will only iterate one time though. Don't you need a while loop for it to continue asking for the correct password? Something like this? // code you had before is fine while (loo < 3 && pass != pass1) { cout << "Retry: "; cin >> pass1; loo++; } if (pass != pass1) { cout << "\nFailed to enter matching password.\n"; } else { cout << "\nCorrect password match. Congratulations.\n"; } EDIT: Sure, you will be able to enter the password twice the way you have it now, but it will only CHECK it one time without a loop.
29th May 2017, 5:54 PM
Zeke Williams
Zeke Williams - avatar
+ 1
You can see that i had an extra if statement for the loop to retry three times. I've tried the code you wrote and its the same situation.
29th May 2017, 3:09 PM
Khalid Fattoum