My password check function isn't working so I clearly did something wrong

So like I said I made a function to check the password and for some reason it won't work when I put in the string for x nothing happens the program just ends
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
#include <iostream>
#include <string>
using namespace std;

int password_check(string x)
{
    x = "blah";
    while (x != "blah") {
        cout << "Please enter the correct password\n";
        cin >> x;
        if (x == "blah") {
            cout << "Congrats you can go in";
        }
    }
}


int main()
{
    string x;
    cout << "Please enter your password: ";
    cin >> x;
    password_check(x);
}
The problem may be that you set x to "blah" and then tell the program that x has to be different from "blah" to enter the loop.
...
Last edited on
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
#include <iostream>
#include <string>
using namespace std;

void password_check(string x)
{
		string y = "blah";
		
		if (x == y)
		{
            cout << "Congrats you can go in";
        }

		
		while(x != y) 
		{
        cout << "Please enter the correct password\n";
        cin >> x;
		}
		cout << "Congrats you can go in";
	
}


int main()
{
    string x;
    cout << "Please enter your password: ";
    cin >> x;
    password_check(x);
	//system("pause");
}
Last edited on
1
2
x = "blah";
    while (x != "blah") {  // this will not execute since you compare (x) which is "blah" with "blah" also 




Do it like this:
1
2
3
4
5
6
7
8
9
10
11
12
int password_check(string x)
{
    
    while (x != "blah") {
        cout << "Please enter the correct password\n";
        cin >> x;
    }
     
    cout << "Congrats you can go in";
        
    
}
Topic archived. No new replies allowed.