Need Help!

closed account (yR9wb7Xj)
Hi, I'm new to programming and I'm having trouble with my own program that I created it's just for fun. Right now I'm teaching myself If and Else if statements. Here's the message I keep getting in Eclipse IDE. |"( invalid operands to binary expression ('int' and 'string' (aka 'basic_string<char, char_traits<char>, allocator<char>
>'))"|
I've tried multiple times fixing it but so far I have no luck, please help.
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>
#include <string> 
using namespace std;

int main() {


	string password = "codex23jv";
	int option1,option2,option3;

	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;

	if(option1 == password){
		cout << "Enter password to access this option." << endl;
		cin >> password;
		cout << "Access granted." << endl;
	}
	else if(option2==password){

		cout << "Enter the password to access this option." << endl;
		cin >> password;

		cout << "Access granted." << endl;
	}

	else if(option3 == password){
		cout << "Enter the password to access this option."<< endl;

		cin >> password;

		cout << "Access granted." << endl;
	}
		else{
			cout << "The password you have enter is invalid, the system has shut down for security purposes." << endl;
		}


return 0;
}




Last edited on
The compiler is complaining about your "if" statements where you are trying to compare an integer to a string. I assume you are reading the user's input somewhere, but I don't see that part of your code. You are comparing whatever is in option1 with your string, you probably meant:

1
2
3
4
5
6
7
8
9
10

if( userInput == option1 )
   /* do something */

else if( userInput == option2 )
   /* do another thing  */

else if( userInput == option 3 )
   /* do yet another thing */


Don't forget to assign default values to option1, option2, and option3.
What you're trying to do is like comparing your height to the name of your dog. Is 170cm equal to Fido?

To compare things there needs to be some way in which they can be considered equal. Strings can not be naturally converted to numbers ("121" can be, but "a121b" is a string too and can't be converted).
Your options are:
1) convert the number to a string.
2) convert the string to a number (but this can fail)
3) define some method of comparison. Stupid example:
1
2
3
4
5
// returns true if the number is the same as the amount of characters
bool Equals(int n, string str)
{
    return str.length == n;
}
closed account (yR9wb7Xj)
Thanks for the help, it turns out the code I was trying to do was missing a nested if statements.
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
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <string>
using namespace std;

int main() {

	string password = "codex23jv";
	string input;
	int option1 = 1, option2 = 2, option3 = 3;
	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;
	if (option1 == choice) {
		cout << "Enter password to access this option." << endl;
		cin >> input;
		if(input==password){
			cout << "Access granted." << endl;
		}
		else{
			cout << "Access denied, password incorrect." << endl;
		}
	}
	else if (option2 == choice) {

		cout << "Enter the password to access this option." << endl;
		cin >> input;
				if(input==password){
					cout << "Access granted." << endl;
				}
				else{
					cout << "Access denied, password incorrect." << endl;
				}
	}

	else if (option3 == choice) {
		cout << "Enter the password to access this option." << endl;

		cin >> input;
				if(input==password){
					cout << "Access granted." << endl;
				}
				else{
					cout << "Access denied, password incorrect." << endl;
				}
	} else {
		cout
				<< "The option you have enter is invalid."
				<< endl;
	}

	return 0;
}
Last edited on
You could improve the design of the code by not duplicating the 'cout << "Enter the password" line 3 times. If you think carefully about what you're trying to do, you need to ask the password question once, FIRST, then check the option the user has selected. You'll cut the size of the program down by at least half (= less debugging, less problems to track/fix). Good design will come with practice, but it always starts with thinking logically about the order in which you're doing things. If you find yourself duplicated sections of codes, you haven't designed it properly - that's a warning to tell you to think about the problem and start again.
closed account (yR9wb7Xj)
Can you provide an example so I know what you mean? It doesn't have to be an example of my code. I'm just a little confuse, also I was learning at the time nested if statements. That's why I apply it to my program. I'm open to any suggestions right now to improve my code, so thanks for the feedback.
What he means is the following:
Not:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
if (option1 == choice) {
	cout << "Enter password to access this option." << endl;
	cin >> input;
	if(input==password){
		cout << "Access granted." << endl;
	}
	else{
		cout << "Access denied, password incorrect." << endl;
	}
}
else if (option2 == choice) {
	cout << "Enter the password to access this option." << endl;
	cin >> input;
	if(input==password){
		cout << "Access granted." << endl;
	}
	else{
		cout << "Access denied, password incorrect." << endl;
	}
}

But:
1
2
3
4
5
6
7
8
9
10
11
12
13
// put it here since it's repeated
cout << "Enter password to access this option." << endl;
	cin >> input;
	if(input==password)
		cout << "Access granted." << endl;
	else
		cout << "Access denied, password incorrect." << endl;
if (option1 == choice) {
	// do sth
	}
else if (option2 == choice) {
        // do another sth
	}
Last edited on
Since all three actions require a password, it makes sense to ask for the password first, before displaying the menu. Then you don't have to ask for it separately for each option.

Sometimes you have a mix of options that require a password and some that don't. In that case, you generally want to defer asking for the password until it is needed. In this case, it makes sense to put prompting for the password in a function. A call to this function would replace lines 19-26, 29-37 and 41-49. Write the function once, use it where needed instead of writing a lot of repetitive code.
closed account (yR9wb7Xj)
Okay, I see what he means now. In this specific program I ask the user to choose an option and type a password to enter that option. Each option has to ask for a password for security purposes to make sure it's the user who's trying to access their account. I know that's kind of dumb, but that's the way I wanted the program to be. But If I were to try it your way, TheHardew they would have access to each option based on that one password they enter in the beginning. It makes sense too do that but I chose for the user to enter the password for each option to access those options. I should of just made three different passwords so you could see what I was doing, but I felt making one password for everything will be alot easier for the user. Because if you think about it the person trying to access their account would have a hard time to get in those options because each option ask them to type in a specific password.
closed account (yR9wb7Xj)
I edited my program to show you what I meant from above my post reply.
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
49
50
51
52
53
54
#include <iostream>
#include <string>
using namespace std;

int main() {

	string password = "codex23jv";
	string iPassword ="jvKy3";
	string jPassword = "Lobby";
	string input;
	int option1 = 1, option2 = 2, option3 = 3;
	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;
	if (option1 == choice) {
		cout << "Enter password to access this option." << endl;
		cin >> input;
		if (input == password) {
			cout << "Access granted." << endl;
		} else {
			cout << "Access denied, password incorrect." << endl;
		}
	} else if (option2 == choice) {

		cout << "Enter the password to access this option." << endl;
		cin >> input;
		if (input == iPassword) {
			cout << "Access granted." << endl;
		} else {
			cout << "Access denied, password incorrect." << endl;
		}
	}

	else if (option3 == choice) {
		cout << "Enter the password to access this option." << endl;

		cin >> input;
		if (input == jPassword) {
			cout << "Access granted." << endl;
		} else {
			cout << "Access denied, password incorrect." << endl;
		}
	} else {
		cout << "The option you have enter is invalid." << endl;
	}

	return 0;
}
Last edited on
Then, as AbstractionAnon said you should put that code in the function and, for example, pass password you want as a parameter.
closed account (yR9wb7Xj)
I haven't got that far in C++ to do functions yet. I'm on while loops right now learning how to do that.
Topic archived. No new replies allowed.