I got ear Infection and missed two classes, and found out later that I missed alot more then just two classes. Soooo I could use a little help with my while and if statements. And if there is a way to make this program simpler please tell me. I now my class went over do statement and the such while i was sick.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string password, guess;
int tries=0;//<==think this is right...
password == "hello_world";//<==Not sure about this either
cout << "please enter password to continue:\n\n";
getline (cin,guess);
// This is where i think I am getting the errors
while (password != guess, tries != 3)
tries++
{
if (guess != password)
cout << "wrong password" << endl;
else (tries == 3)
cout << "sorry your guesses are up..." << endl;
ifelse (guess == password)
cout << "you may procede." << endl;
}
system("pause");
return 0;
}
sort of I need this program to ask for a password and if the password is not correct It loops three times and after the third time it supposed to lock the user out
Your logic is correct just syntactically wrong. The while loop should look something like this
1 2 3 4 5 6 7 8 9 10 11 12 13
while (password != guess)
{
if (tries < 3) {
if (guess != password)
cout << "wrong password" << endl;
}else
cout << "sorry your guesses are up..." << endl;
tries++;
getline (cin,guess); // make sure to include this line
}
if(password == guess)
cout << "you may proceed" << endl;
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string password, guess;
int tries=0;//<==think this is right...
password == "hello_world";//<==Not sure about this either
cout << "please enter password to continue:\n\n";
getline (cin,guess);
// This is where i think I am getting the errors
while (password != guess, tries != 3)
tries++
{
if (guess != password)
cout << "wrong password" << endl;
else (tries == 3)
cout << "sorry your guesses are up..." << endl;
ifelse (guess == password)
cout << "you may procede." << endl;
}
system("pause");
return 0;
}
I moved the cout << "you may proceed" << endl; line out of the while loop because the while loop only executes if the password != guess so in that case the you may proceed statement would never be printed. So just add that to the bottom of the loop instead.
I tinkered w/your code for a while and came up w/a solution. Are you required to use the ‘while’ loop? I was able to get the desired result using a ‘for’ loop. I hope this helps.
I dont think I learned "for" statements yet, but thanks for the example I will probally need it later.And also I appreciate all the help you guys have given me. Thanks again