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

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

  • getch()
  • system("PAUSE")

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:

  1. A regular 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.
    What if he presses some key other than ENTER?
  2. It explicitly instructs the user in what he is expected to do. A program that just stops is both non-obvious and frightening to users. If you ever run your program again a year from now, you'll understand. Having to re-read your own code to see why the program stopped, or why you had to press ENTER, is evidence of bad UI.

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.