CMD closes after executing the program as intended.

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Aug 2008
Posts: 45
Reputation: papuccino1 is an unknown quantity at this point 
Solved Threads: 1
papuccino1's Avatar
papuccino1 papuccino1 is offline Offline
Light Poster

CMD closes after executing the program as intended.

 
0
  #1
Aug 19th, 2008
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:

  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. }
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: CMD closes after executing the program as intended.

 
0
  #2
Aug 19th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 45
Reputation: papuccino1 is an unknown quantity at this point 
Solved Threads: 1
papuccino1's Avatar
papuccino1 papuccino1 is offline Offline
Light Poster

Re: CMD closes after executing the program as intended.

 
0
  #3
Aug 20th, 2008
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?
My very first baby boy, coming December 2008. :D
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: CMD closes after executing the program as intended.

 
0
  #4
Aug 20th, 2008
I meant I'm still new to programming c++

  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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 45
Reputation: papuccino1 is an unknown quantity at this point 
Solved Threads: 1
papuccino1's Avatar
papuccino1 papuccino1 is offline Offline
Light Poster

Re: CMD closes after executing the program as intended.

 
0
  #5
Aug 20th, 2008
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.
My very first baby boy, coming December 2008. :D
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: CMD closes after executing the program as intended.

 
0
  #6
Aug 20th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,829
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster

Re: CMD closes after executing the program as intended.

 
0
  #7
Aug 20th, 2008
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:

  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
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 226
Reputation: henpecked1 is an unknown quantity at this point 
Solved Threads: 1
henpecked1 henpecked1 is offline Offline
Posting Whiz in Training

Re: CMD closes after executing the program as intended.

 
0
  #8
Aug 20th, 2008
Thanks Vernon, education appreciated
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 45
Reputation: papuccino1 is an unknown quantity at this point 
Solved Threads: 1
papuccino1's Avatar
papuccino1 papuccino1 is offline Offline
Light Poster

Re: CMD closes after executing the program as intended.

 
0
  #9
Aug 20th, 2008
Thank you Vernon, I'll read those links ASAP.
My very first baby boy, coming December 2008. :D
Reply With Quote Quick reply to this message  
Join Date: Aug 2008
Posts: 10
Reputation: sparty is an unknown quantity at this point 
Solved Threads: 0
sparty sparty is offline Offline
Newbie Poster

Re: CMD closes after executing the program as intended.

 
0
  #10
Aug 20th, 2008
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC