[Dev-C++] Re: Dev-cpp-users digest, Vol 1 #612 - 8 msgs
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: David M. <ci...@ya...> - 2002-02-12 19:03:10
|
>Hello... > >I have a question about closing a terminal window during a C++ program >execution. I am using Dev C++ 4.01 as my compiler. At >http://www.cprogramming.com/close.html, it says that inserting the line >cin.get() before the return 0 line will keep the terminal window open >until I press enter. This I find is true for the following program: > >#include <iostream.h> >int main() >{ > cout<<"The Compiler Works"; > cin.get() > return 0; >} > > >However for the following code, I type my number, press enter, and the window >closes immediately. Why doesn't the compiler display "You entered: ", >then my >number, and then close the terminal window once I push enter (which would be >the second time)?? Am I putting the cin.get() statement in the wrong spot?? >Check out the code I'm using. > >#include <iostream.h> >int main() >{ > int thisisanumber; > cout<<"Please enter a number:"; > cin>>thisisanumber; > cout<<"You entered: "<<thisisanumber; > cin.get() > return 0; >} > >I'm stuck, and I'm trying to teach myself C++. Your help is greatly >appreciated. > >Sincerly, >Aaron There are 2 ways to do what you want the first requires you to replace the cin.get(); with system("PAUSE"); N.B. To use system("PAUSE") you must include stdlib.h The system is in common letters and the PAUSE is all capital since you are learning I will include a snippet that does the exact same thing /* This does the same thing as system("PAUSE") */ cin.ignore(256, '\n'); cout << "Press ENTER to continue..." << endl; cin.get(); The cin.ignore will keep reading from input and throwing it away until it gets to '\n' (for your purposes the equivalent of the enter key) in other word it flushes the buffer so the program will have nothing in the input buffer when it gets to the cin.get and should wait I have tested this in Dev-c++ |