blood sheds cin.get (confusing)

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

Join Date: Nov 2004
Posts: 16
Reputation: nico is an unknown quantity at this point 
Solved Threads: 0
nico nico is offline Offline
Newbie Poster

blood sheds cin.get (confusing)

 
0
  #1
Dec 24th, 2004
hi all i am a beginner and i use the devc++ to compile my programs ,here is my question ,after i compile my program the window disappears without giving me a chance to view my excutable file. i did my homework and found this piece of code online ,(supposed to be written by the same guy who wrote the dev c++ compiler ) and still can't get it done , here is the code,am sure some of you smart guys/gals will figure it out and give me an answer
thanx


Disappearing windows


If you execute your program (with or without parameters), you may notice something peculiar; a console window will pop up, flash some text and disappear. The problem is that, if directly executed, console program windows close after the program exits.
You can solve this problem one of two ways:


Method 1 - Scaffolding:
Add the following code before any return statement in main() or any exit() or abort() statement (in any function):

/* Scaffolding code for testing purposes */
cin.ignore(256, '\n');
cout << "Press ENTER to continue..." << endl;
cin.get();
/* End Scaffolding */

This will give you a chance to view any output before the program terminates and the window closes.

Method 2 - Command-prompt:
Alternatively, instead of using Dev-C++ to invoke your program, you can just open an MS-DOS Prompt, go to the directory where your program was compiled (i.e. where you saved the project) and enter the program name (along with any parameters). The command-prompt window will not close when the program terminates.
For what it's worth, I use the command-line method.
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 445
Reputation: 1o0oBhP is an unknown quantity at this point 
Solved Threads: 6
1o0oBhP's Avatar
1o0oBhP 1o0oBhP is offline Offline
Posting Pro in Training

Re: blood sheds cin.get (confusing)

 
0
  #2
Dec 24th, 2004
if the scaffolding code does NOT work then you have an error somewhere causing it to quit. Or you could try any of the following

  1. system("PAUSE"); // in <cstlib> i believe. There is a template that puts this code in (console app template)
  1. getchar(); // in <cstdio>

Posting the ENTIRE code you are working on would be useful. DevC++ Programs do have a habit of quitting (a mingw32 trait?) but its usually because of an error as the three methods between us ALL work...
http://sales.carina-e.com

no www
no nonsense

coming soon to a pc near you! :cool:
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: blood sheds cin.get (confusing)

 
0
  #3
Dec 24th, 2004
You should try my compiler.... I use microsoft, it doesnt close on you, instead along you use "\n"; to create a new line it will say " Press any key to close" rather than closing ...
Reply With Quote Quick reply to this message  
Join Date: Sep 2004
Posts: 7,698
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 730
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: blood sheds cin.get (confusing)

 
0
  #4
Dec 25th, 2004
Two virtually useless replies it seems. Dancing around the problem isn't very helpful. Let's cover the first reply:

>system("PAUSE"); // in <cstlib> i believe.
Bad suggestion. I've explained why before.

>getchar(); // in <cstdio>
And this is different from cin.get()...how? The only difference is that now you don't attempt to handle extraneous characters in the stream.

Next reply:

>You should try my compiler.... I use microsoft
Allow me to translate: "Your compiler has a trivial issue with a trivial fix, but switch compilers anyway because I like the one I'm using better than the one you're using." Do us all a favor and don't post again unless you have something of value to add.

nico: Post the code that exhibits the problem. We work much better with compilable examples than with descriptions of a problem. But before you do, does this not work?
  1. #include <ios> // For streamsize
  2. #include <iostream>
  3. #include <limits> // For numeric_limits
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. // Put garbage on the stream
  10. cout<<"Enter several characters: ";
  11. cout<< cin.get() <<endl;
  12.  
  13. // Fix the problem
  14. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  15. cout<<"Press [Enter] to continue"<<flush;
  16. cin.get();
  17. }
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Nov 2004
Posts: 16
Reputation: nico is an unknown quantity at this point 
Solved Threads: 0
nico nico is offline Offline
Newbie Poster

Re: blood sheds cin.get (confusing)

 
0
  #5
Dec 25th, 2004
Originally Posted by Narue
Two virtually useless replies it seems. Dancing around the problem isn't very helpful. Let's cover the first reply:

>system("PAUSE"); // in <cstlib> i believe.
Bad suggestion. I've explained why before.

>getchar(); // in <cstdio>
And this is different from cin.get()...how? The only difference is that now you don't attempt to handle extraneous characters in the stream.

Next reply:

>You should try my compiler.... I use microsoft
Allow me to translate: "Your compiler has a trivial issue with a trivial fix, but switch compilers anyway because I like the one I'm using better than the one you're using." Do us all a favor and don't post again unless you have something of value to add.

nico: Post the code that exhibits the problem. We work much better with compilable examples than with descriptions of a problem. But before you do, does this not work?
  1. #include <ios> // For streamsize
  2. #include <iostream>
  3. #include <limits> // For numeric_limits
  4.  
  5. using namespace std;
  6.  
  7. int main()
  8. {
  9. // Put garbage on the stream
  10. cout<<"Enter several characters: ";
  11. cout<< cin.get() <<endl;
  12.  
  13. // Fix the problem
  14. cin.ignore(numeric_limits<streamsize>::max(), '\n');
  15. cout<<"Press [Enter] to continue"<<flush;
  16. cin.get();
  17. }
narue you rock man!! I am very thankful for it , i would like to thank 100obhp and acidburn for the reply too , it was somehow helpful ,belive me , merry xmas
Reply With Quote Quick reply to this message  
Join Date: Dec 2004
Posts: 489
Reputation: Acidburn is an unknown quantity at this point 
Solved Threads: 5
Acidburn Acidburn is offline Offline
Posting Pro in Training

Re: blood sheds cin.get (confusing)

 
0
  #6
Dec 25th, 2004
I am sorry , I didn't know i was been big headed! :cry:
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
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