end program (loops)

I am adding the final touch to this assignment where I have to end a program after 3 invalid password tries. I have it so you only get three tries, but it still goes on and shows the program even if the last try is incorrect (whoops :P) Any suggestions on what I should use to actually end after the last try? (i.e. if break, etc..
The code:

void getPassword()
{
int loopruntime =1; // This makes you have three tries
while (loopruntime <=3) // This makes you have three tries
{
string password;
cout << " Enter the password: ";
getline (cin, password);
loopruntime ++; // This makes you have three tries

if (password == "xxxxx") break; // this is the actual password

cout << "Invalid. " << endl;

}
}

int main ()

{
getPassword (); // should I be adding something here or earlier to get it to quit?

//rest of program that is controlled by password goes here

Any input would be appreciated! thanks in advance.
Last edited on
You could have the function return a bool and instead of breaking for a correct password you could return true, then after the while loop (since they got it wrong 3 times) return false. In main you could do if(!getPassword()){ return 1; }
Last edited on
the problem here is that nothing is ending the program. Main has to return 0(or something like x/0 or another error) before the program will end. try this:

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

bool getPassword()
{
int loopruntime =1; // This makes you have three tries
while (loopruntime <=3) // This makes you have three tries
{
string password;
cout << " Enter the password: ";
getline (cin, password);
loopruntime ++; // This makes you have three tries

if (password == "xxxxx") return true; // this is the actual password

cout << "Invalid. " << endl;

}
return false;
}

int main ()

{
if(!getPassword ()) return 0; // now the program will either go past or end at this line xD

//rest of program that is controlled by password goes here

well I'm not technically supposed to use bool yet, but what you say makes sense. Basically I have to send something to main in order to end the program?
yep, exactly! you could do the samething with a int too:

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
int getPassword()
{
int loopruntime =1; // This makes you have three tries
while (loopruntime <=3) // This makes you have three tries
{
string password;
cout << " Enter the password: ";
getline (cin, password);
loopruntime ++; // This makes you have three tries

if (password == "xxxxx") return 1; // this is the actual password

cout << "Invalid. " << endl;

}
return 0;
}

int main ()

{
if(getPassword ()=0) return 0; // now the program will either go past or end at this line xD

//rest of program that is controlled by password goes here
@LB: thank you for the explanation, and for the previous help!

@dong shi: That worked: I added and extra '=' in the if getpassword line, but other then that, it was fine. Also, I want to see if I have what you did down:

getting rid of void allows for more flexibility in checking if something is right i.e the password works

return 1: condition if the password is correct

}
return 0: a little confused why this is needed
}

if(getPassword ()=0) return 0: ends program if condition 1 is not met

I will mark this as completed, but a quick explanation would help me for future codes.

Thanks again!




its so that if it doesnt return 1, it returns something, such as 0. if the password was wrong all 3 times, what would getPassword() be in getPassword()==0 if it did not have the return 0.
Topic archived. No new replies allowed.