Return Value Function Error.

closed account (yR9wb7Xj)
When I'm typing the correct password, it's telling me incorrect. Where did I go wrong? Please explain.
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
using namespace std;

int displayMenu() {

	int choice;

	cout << "Welcome to Chase financial security!" << endl;
	cout << "Choose the option you want, below: \n" << endl;
	cout << "1. Access your bank account? " << endl;
	cout << "2. Access your deposit account?" << endl;
	cout << "3. Access your transfer account?" << endl;
	cin >> choice;

	return choice;
}

bool accessedMenu() {
	string password = "codex23jv";
	string input;
	cout << "Enter password to access this option." << endl;
	cin >> input;
	if (input == password) {
		true;
	}

	return false;
}
int main() {

	switch (displayMenu()) {
	case 1:
	case 2:
	case 3:

		if (accessedMenu()) {
			cout << "Access granted." << endl;
		} else {
			cout << "Access denied, password incorrect." << endl;
		}
		break;

	default:
		cout << "The option you have enter is invalid." << endl;
	}
	return 0;
}
Last edited on
add a return. should work fine.

1
2
3
4
5
6
7
8
9
10
11
12
13
bool accessedMenu() {
	string password = "codex23jv";
	string input;
	cout << "Enter password to access this option." << endl;
	cin >> input;
	if (input == password) {
	return true;///see, add a return here
	}

	return false;
}

You are missing a return before the true, otherwise that statement does nothing.
closed account (yR9wb7Xj)
Wow I just realized that too lol, thank you!
As the return value of operator== is already true or false, you could just do this:

1
2
3
4
5
6
7
bool accessedMenu() {
	string password = "codex23jv";
	string input;
	cout << "Enter password to access this option." << endl;
	cin >> input;
	return (input == password); // just return the result of the comparison
}


Andy
Last edited on
closed account (yR9wb7Xj)
Thank you!
Topic archived. No new replies allowed.