Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Problem with if and else statements

Status
Not open for further replies.

Brian18

Technical User
Mar 20, 2005
4
US
Hey everyone, I hope I have this in the right forum.. I'm using Microsoft Visual C++ 6.0.. I'm just starting to learn to program, and I was trying to make an extremely basic calculator.
#include <iostream.h>
#include <string.h>
#include <stdlib.h>

int Add(int x, int y)
{
return(x+y);
}

int main()
{
char text[512];
int num1;
int num2;
int num3;

while(1)
{

cout << "Menu" << endl;
cout << endl <<"Add" << endl;
cout << "Subtract" << endl;
cout << "Multiply" << endl;
cout << "Divide" << endl;
cout << endl << "What would you like to do? ";
cin.getline(text, 512);

if (strcmpi(text, "add") == 0) {
cout << "Enter two numbers to add:" << endl;
cin >> num1;
cin >> num2;
num3 = Add(num1, num2);
cout << num3 << endl;
}

else
{
cout << "Unknown command." << endl;
}
}
}

That's my code.. My problem is, right when I type in add and it does everything inside of the if statement, it still goes down and does what's in the else statement.. So it will add two numbers together and then still say "Unknown command.". Does anyone have an idea of what might be going on? Thanks.
 
not sure... everything seems ok .. except the way you input stuff... that is the only source of error i can see...

instead of:
cin.getline(text, 512);

try:
cin>>text;

also, 512 seems a little excessive for a string that isn't likely to more longer than 10-15!

i'm not quite sure why your line doesn't work.. never used getline myself... but i think this must be the source of your error becasue everything else seems fine!

this is unlikely to be the source of your error, but just make sure that when you read something with cin, it is automatically null terminated. i'm pretty sure it is, but strcmp will not work if the string is not null terminated. to make sure you're actualy going into the if part of the if-else statement, you could put a print statemetn in the if part to make sure.

also, you should have a counter to make sure that it is not going into the if-else part in the same iteration of the loop. it is extremely unlikely that this is the case! but your post suggests that you think this is the case.. so, decalre a int i=0 before the while loop, increment it just before the closing brace of the while loop, and print it in the if part and the else part..

hope this helps!
 
ChipperMDW and drewdaman are both right - the problem is that the second time in the while loop you get the end of line character, which doesn't equal "add", so you go into the else part.
 
I just tried using cin instead, but it does the same thing.. Could it be anything else?
 
Nevermind, I figured it out.. I was doing it wrong.. Thanks everyone for the help.
 
Did you read the thread I posted a link to? There's your solution and your explanation.
 
I read it, and I understand why it was having a problem. But it was saying getline() should be used in place of cin, right? I'm not sure how I would use getline() instead.
 
The debugger can help you solve problems like this in a matter of minutes or seconds. If you don't know how to use it, you definitely need to learn. Here are the basics. In debug mode, run the program using F5. F9 sets breakpoints, and F5 resumes execution when it has hit a breakpoint. Mouse over variables to see their values or use the watch toolbar at the bottom of the screen. F10 single-steps through code when you have hit a breakpoint, and F11 single steps through code but enters any functions that are called.
 
Brian18 said:
But it was saying getline() should be used in place of cin, right?
That's one of the things it was saying.

The other thing it was saying was that if you didn't use [tt]cin.getline[/tt] and just used the [tt]>>[/tt] operator (like in your code), you can use [tt]cin.ignore[/tt] to ignore the newline stuck in your input stream.


Brian18 said:
I'm not sure how I would use getline() instead.
You would use [tt]cin.getline[/tt] to grab an entire input line, then use something like a Standard C++ [tt]stringstream[/tt] or a [tt]lexical_cast[/tt] ( or the Standard C [tt]atoi[/tt] function to convert the string to an integer.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top