954,499 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

blood sheds cin.get (confusing)

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.

nico
Newbie Poster
16 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

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

system("PAUSE"); // in <cstlib> i believe. There is a template that puts this code in (console app template)
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...

1o0oBhP
Posting Pro in Training
445 posts since Dec 2004
Reputation Points: 16
Solved Threads: 6
 

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 ...

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

Two virtually useless replies it seems. Dancing around the problem isn't very helpful. Let's cover the first reply:

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

>getchar(); // in
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?

#include <ios>      // For streamsize
#include <iostream>
#include <limits>   // For numeric_limits

using namespace std;

int main()
{
  // Put garbage on the stream
  cout<<"Enter several characters: ";
  cout<< cin.get() <<endl;
  
  // Fix the problem
  cin.ignore(numeric_limits<streamsize>::max(), '\n');
  cout<<"Press [Enter] to continue"<<flush;
  cin.get();
}
Narue
Bad Cop
Administrator
15,460 posts since Sep 2004
Reputation Points: 6,464
Solved Threads: 1,401
 

Two virtually useless replies it seems. Dancing around the problem isn't very helpful. Let's cover the first reply:

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

>getchar(); // in 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?

#include <ios>      // For streamsize
#include <iostream>
#include <limits>   // For numeric_limits

using namespace std;

int main()
{
  // Put garbage on the stream
  cout<<"Enter several characters: ";
  cout<< cin.get() <<endl;
  
  // Fix the problem
  cin.ignore(numeric_limits<streamsize>::max(), '\n');
  cout<<"Press [Enter] to continue"<<flush;
  cin.get();
}


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

nico
Newbie Poster
16 posts since Nov 2004
Reputation Points: 10
Solved Threads: 0
 

I am sorry , I didn't know i was been big headed! :cry:

Acidburn
Posting Pro
511 posts since Dec 2004
Reputation Points: 12
Solved Threads: 5
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You