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.
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 */
elseif( userInput == option2 )
/* do another thing */
elseif( 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;
}
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.
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.
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.
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.