Hello guys.

This is my first post on the site and I think I'll like it here very much. :)
I already have the site set as my home page.

So here is my dilemma.

The program is supposed to do some pretty basic comparisons of two numbers typed in by the user and then display a series of checks to see if it's bigger, smaller or equal than the other number.

Everything runs smoothly but at the end it CMD closes way too fast. I've read that cin.get(); would allow the window to remain open until a user presses a key, but it doesn't work as intended.

Remember I'm very new to C++ so the syntax is very alien to me right now even though I've programmed a lot in C#. Getting used to it, barely. :P

Anyway, here's my code:

#include <iostream>

using std::cout;
using std::cin;
using std::endl;

//Begin program execution.
int main()
	int number1;
	int number2;

	cout << "Enter two intergers to compare: "; //Requests user to insert two numbers.
	cin >> number1 >> number2;

	if ( number1 == number2 )
		cout << number1 << " is equal to " << number2 << endl;
	if ( number1 != number2 )
		cout << number1 << " is not equal to " << number2 << endl;
	if ( number1 < number2 )
		cout << number1 << " is less than " << number2 << endl;

	if ( number1 > number2 )
		cout << number1 << " is greater than " << number2 << endl;

	if ( number1 <= number2 )
		cout << number1 << " is less or equal than " << number2 << endl;

	if (number1 >= number2 )
		cout << number1 << " is greater or equal than " << number2 <<endl;

	cin.get(); //<----- This is where it should stay open.

    return 0; // indicate that program ended successfully

Recommended Answers

All 9 Replies

two things you can do from a newbie point of view

a) if you're using MS Visual Studio, try the menu option "Start without Debug" which will leave the screen up after the program finishes until you "press any key to continue"

b) another thing you can do is put a system pause right before the return 0, and this will pause the program so you can read your output.

Hm... I don't really know what a system pause is, and if you say it's a newbie solution I'd rather not use it. I don't want to get used to doing quick fixes like that.

What's a good programming convention for these instances?

I meant I'm still new to programming c++ :)

system ("PAUSE");

you can always remove it later, it's just a way to capture your output

Using the start without debugging also is just a way to capture your output

Well thank you very much for the answer. :) It worked wonderfully.

Now I have to learn what are the good programming conventions for this situation, I hope someone can tell us both. :D

+rep to you.

If you're just capturing output, I don't think it matters which way you do it, as far as the user is concerned, see below :)

Pausing the program and providing a graceful exit for the user is typically a matter of style and writing it to do so. Using a system pause gives the user time to view the output, and then terminate the program by pressing a key. It's short, sweet, and works. You could always include an option for the user to repeat the process, ensuring that one of the options is a "q" for quit or "x" for exit. This gives them the option of running the program as many times as they like and provides a specific option for exiting.


The above are two good threads that may be of interest to you that explain what is going on pretty well. Basically, you can add a second line of:


in lieu of the system("PAUSE"); command, which works but is frowned on because it makes a system call where none is necessary. It's unnecessarily resource heavy and can be a security problem (i.e. what if there's a malicious program called "PAUSE"?). WaltP (I think) often links to a page that explains why, but I don't have it off the top of my head.

The reason you need two lines of cin.get(); rather than one line is because after you hit the ENTER key after entering the second integer, that ENTER key is still in the input buffer. The first cin.get(); eats up that ENTER key and the second pauses so the screen doesn't go away in a blink of an eye. The two links explain how to "flush" the input stream to get rid of that extra ENTER key (and anything else) so you only need one cin.get(); line. But you can just put a second cin.get(); and it's preferred over system ("PAUSE"); .

Edit: Found the link: http://www.gidnetwork.com/b-61.html

Thanks Vernon, education appreciated

Thank you Vernon, I'll read those links ASAP.

another way to keep it open for as long as you wish would be to just declare an int(or really any type for that matter) and then read it in

place this where you have cin.get above... this does the same thing.. it will wait until you press return to exit the program window...

int x;

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, learning, and sharing knowledge.