Why isn't my IF/ELSE statement working?

Hi,

I am in a beginner's C++ class and I am having a bit of trouble understanding my if/then statement doesn't work. It's a simple console app and I've used if/else a few times before and it looks right to me. Can someone find a reason why it shouldn't run? I am using VS2008 to compile and run.

The code is supposed to take 2 numbers from user input and decide greater than or less than values depending on what number they entered first.

Here is my code:

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

#include <iostream>

using namespace std;

int main()
{
	int num1;
	int num2;		

	cout << "This program will determine which number is larger than or smaller than between the two numbers you choose. \n\n";
	cout << "Pick your first number between 0-100: " <<endl;
	cin >> num1;
	cout << "Pick your second number between 0-100: " <<endl;
	cin >> num2; 
		
		if (num1 > num2)
		{
	 		cout <<num1<< " is larger than "<<num2<< endl;
		}
		else if (num1 < num2)
		{		
	      	cout <<num1<< " is less than "<<num2<< endl;
		}
		else 
		{
			cout << "Uh...both numbers are equal.";
		}

cin.get();
return 0;
		
}

It works fine for me. What is the error you're getting?
Yep. Looks fine to me.
Perhaps you need to read this: http://www.cplusplus.com/forum/beginner/1988/
When you say it doesn't work you mean that you can't see the output? If so, try adding one more cin.get(); line before return 0;. When you use cin to get numbers like you do here, the newline character is left on the input buffer. The cin.get(); you use here gets that pending newline character and the program exits. You need to add one more cin.get(); to make it wait.
Topic archived. No new replies allowed.