+ 1
C++ playground; how do I submit multiple inputs for a while loop
I've only been at this for a few days and decided to try coding an idea I had to do a simple password checker with a failed attempts tracker. For the most part I think the code works as intended except it interacts strangely with the code playgrounds requirement to frontload all inputs. I put the cin>> statement into a while loop and if I enter 2 inputs, the first wrong and the second correct, it doesn't seem to take the second input. Also I know the second while loop at the bottom of the code needs more work to function correctly but I want to figure this out first. https://sololearn.com/compiler-playground/c3SQ1o3SnZ68/?ref=app
4 Réponses
+ 1
Anybody?
+ 1
Bump
+ 1
I don't even see my question on the list so trying to get the algorithm to notice me.
+ 1
Graktor To make the logic simpler, don't use an infinite loop. I also refactored the code so modifications can be done in one place instead of searching for it inside the code.
#include <iostream>
using namespace std;
int main() {
int password;
int fails = 0;
const int MAX_TRIES = 3;
const int pwd = 121518;
while(fails<MAX_TRIES){
cout<< "Enter password: ";
cin>> password;
if(password == pwd){
cout<< "\nHappy Anniversary!";
break;
}
fails++;
cout << "\nInvalid Password, please try again.\n It's not "
<< password
<< "\nFailed attempts: "
<< fails
<< '\n';
if(fails <= MAX_TRIES-1)
cout << "Hint: Our anniversary date MMDDYY\n\n";
}
cout<<"\nBye.";
}
/* Sample Sololearn input
121511
121512
121513
121514
121516
submit
*/