| | |
blood sheds cin.get (confusing)
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
•
•
Join Date: Nov 2004
Posts: 16
Reputation:
Solved Threads: 0
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.
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.
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
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...
C++ Syntax (Toggle Plain Text)
system("PAUSE"); // in <cstlib> i believe. There is a template that puts this code in (console app template)
C++ Syntax (Toggle Plain Text)
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:
no www
no nonsense
coming soon to a pc near you! :cool:
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?
>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?
C++ Syntax (Toggle Plain Text)
#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(); }
I'm here to prove you wrong.
•
•
Join Date: Nov 2004
Posts: 16
Reputation:
Solved Threads: 0
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
#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(); }
, merry xmas ![]() |
Similar Threads
- please help me (C++)
- C++ on Vista (C++)
- Looking up and displaying values in linked lists (C++)
Other Threads in the C++ Forum
- Previous Thread: is this a good book
- Next Thread: Modem_to_Sound_Card
| Thread Tools | Search this Thread |
api array arrays based beginner binary c++ c/c++ calculator char char* class classes code compile console conversion count delete deploy desktop directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory news number numbertoword output parameter pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






