I need help with my password program

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.
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
#include <iostream>
#include <string>

using namespace 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;
		if else (guess == password)
			cout << "you may procede." << endl;
	}

	system("pause");
	return 0;
}
please help it due tonight
so what is it exactly you need help with, loops? do statement?
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;
Last edited on
it sort of works now but it will stream an ifinite amount of sorry your guesses are up after the first input were should I place a break statement?
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
#include <iostream>
#include <string>

using namespace 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;
		if else (guess == password)
			cout << "you may procede." << endl;
	}

	system("pause");
	return 0;
}
i forgot a line and changed some ordering around take a look at the post again for the edits i made
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.
thank you!
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
#include <iostream>
#include <string>

using namespace 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);

	while (guess != password)
	{
                if (tries < 2) {
		      if (guess != password)
			     cout << "wrong password" << endl;
				}else{
			     cout << "sorry your guesses are up..." << endl;
				 break;}
               tries++;
               getline (cin,guess);     
	}

if(guess == password)
       cout << "you may proceed" << endl;

	system("pause");
	return 0;
}
majorursa,

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.

Thanks,
Redd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <iostream>
#include <string>

using namespace std;

int main()
{
	string password, guess;
    password = "hello_world";
	cout << "Please enter password to continue:\n";
    int chances;
    for (chances =0; chances <3; chances++) {
        getline (cin, guess);
        if (guess == password){
            cout << "You may proceed.";
            return 0;
        }
        else
        cout << "Wrong password" << endl;
    }
    cout << "Sorry your guesses are up..."<< endl;
		return 0;
}
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
Topic archived. No new replies allowed.