When I compile and run my programs in Dev C++, the output window opens and shows the output. Then instanlty the window flashes and disappears.
How do I make the window stay long enough for me to read the output??
Thanks
When I compile and run my programs in Dev C++, the output window opens and shows the output. Then instanlty the window flashes and disappears.
How do I make the window stay long enough for me to read the output??
Thanks
The problem is (not really a problem), that the program is running too fast and doesn't have anything to stop it from closing. There are a number of options to solve this, most of them are mentioned here -- http://faq.cprogramming.com/cgi-bin/smartfaq.cgi?answer=1043803465&id=1043284385 so if you're using C++ throw a cin.ignore();
and cin.get();
before you return 0;
at the end of your program
here at home i'm using dev c++ and i do get the same "problem"
i use system("pause") which i think is not preferred by others,but it's just me using it to see the output
then i just remove it when i'm at school because there we use visual c++
OR
run the program with the command prompt
use getch in the end....
though getch is used to input.
cout<<"c++ is cool"<<endl;
getch(); // freezez the screen till a key is pressed.
Don't use
Both are non-portable.
Instead, do it the C++ way:
#include <iostream>
#include <limits>
void pause() {
std::cout << "Press ENTER to continue... ";
std::cin.ignore( std::numeric_limits<streamsize>::max(), '\n' );
}
Now if you want to pause, just use the function:
int main() {
using namespace std;
string name, color;
cout << "WHAT, is your NAME!?\n";
getline( cin, name );
cout << "WHAT, is your favorite COLOR!?\n";
getline( cin, color );
cout << "Aaaiiiiiieeeee!\n";
pause();
}
Hope this helps.
what does non-portable mean ??
and what does pause(); do?
what does non-portable mean ??
and what does pause(); do?
Non portable means that the code won't work on all systems -- suppose you were to compile on one system it might work but that doesn't mean it will compile on all systems. Using system is a bad idea for pausing. getch() is a lazy excuse. Duoas' solution is good. Use that.
Itz asking for a header file for pause and its giving a warning that function should return a value.
No idea about pause, but the warning is solved by putting a return 0;
before the closing }
in main:
int main() {
// Your code
return 0;
}
Edit: post double posted. Could an obliging moderator delete this one please?
what does non-portable mean ??
and what does pause(); do?
pause() is a self defined function made by duoas
but i think a plain cin.ignore() works fine
or maybe it's just me on my compiler though
Sorry about that missing return 0;
. Thanks twomers!
The reason I made a pause() function is two-fold:
cin.ignore();
is dangerous, because you don't know how it will leave the state of input. Remember, console input is usually line-buffered, meaning that the user must press ENTER at the end of everything he types.A good UI always tells the user exactly what is expected, then presumes that the user may do something stupid anyway. My pause() function does both.
Hope this helps.
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.