943,954 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2056
  • C++ RSS
Aug 19th, 2008
0

CMD closes after executing the program as intended.

Expand Post »
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.

Anyway, here's my code:

c++ Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. using std::cout;
  4. using std::cin;
  5. using std::endl;
  6.  
  7.  
  8. //Begin program execution.
  9. int main()
  10. {
  11. int number1;
  12. int number2;
  13.  
  14. cout << "Enter two intergers to compare: "; //Requests user to insert two numbers.
  15. cin >> number1 >> number2;
  16.  
  17. if ( number1 == number2 )
  18. cout << number1 << " is equal to " << number2 << endl;
  19.  
  20. if ( number1 != number2 )
  21. cout << number1 << " is not equal to " << number2 << endl;
  22.  
  23. if ( number1 < number2 )
  24. cout << number1 << " is less than " << number2 << endl;
  25.  
  26. if ( number1 > number2 )
  27. cout << number1 << " is greater than " << number2 << endl;
  28.  
  29. if ( number1 <= number2 )
  30. cout << number1 << " is less or equal than " << number2 << endl;
  31.  
  32. if (number1 >= number2 )
  33. cout << number1 << " is greater or equal than " << number2 <<endl;
  34.  
  35. cin.get(); //<----- This is where it should stay open.
  36.  
  37. return 0; // indicate that program ended successfully
  38. }
Reputation Points: 19
Solved Threads: 1
Light Poster
papuccino1 is offline Offline
45 posts
since Aug 2008
Aug 19th, 2008
0

Re: CMD closes after executing the program as intended.

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.
Last edited by henpecked1; Aug 19th, 2008 at 11:55 pm.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

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?
Reputation Points: 19
Solved Threads: 1
Light Poster
papuccino1 is offline Offline
45 posts
since Aug 2008
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

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

C++ Syntax (Toggle Plain Text)
  1.  
  2. 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
Last edited by henpecked1; Aug 20th, 2008 at 12:09 am.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

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.

+rep to you.
Reputation Points: 19
Solved Threads: 1
Light Poster
papuccino1 is offline Offline
45 posts
since Aug 2008
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

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.
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

http://www.daniweb.com/forums/thread90228.html
http://www.dreamincode.net/forums/showtopic30581.htm

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:

C++ Syntax (Toggle Plain Text)
  1. cin.get();

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
Last edited by VernonDozier; Aug 20th, 2008 at 2:10 am. Reason: added link
Featured Poster
Reputation Points: 2614
Solved Threads: 687
Posting Expert
VernonDozier is offline Offline
5,375 posts
since Jan 2008
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

Thanks Vernon, education appreciated
Reputation Points: 10
Solved Threads: 2
Posting Whiz in Training
henpecked1 is offline Offline
244 posts
since Dec 2007
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

Thank you Vernon, I'll read those links ASAP.
Reputation Points: 19
Solved Threads: 1
Light Poster
papuccino1 is offline Offline
45 posts
since Aug 2008
Aug 20th, 2008
0

Re: CMD closes after executing the program as intended.

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;
cin>>x;
Last edited by sparty; Aug 20th, 2008 at 9:32 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
sparty is offline Offline
10 posts
since Aug 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Acessing indefinte arguments for a function?
Next Thread in C++ Forum Timeline: Help





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC