0

Possible Duplicate:
How to stop C++ console application from exiting immediately?

Forgive me for my rather extreme newbie-ness in the field of C++ but here goes. :)

When I run say for example this code in a terminal:

int g;
cout << "Please enter an integer value: ";
cin >> g;
cout << "The value you entered is " << g;

...to you C++ experts out there, obviously this accepts a value from the user's input and then displays in the output. However, right after I "submit" my input and click enter, I see the output for a mere millisecond and then the terminal closes. Anyway I could stop the terminal from closing so I could actually have a chance to see the result?

I'm using Visual Studio on Windows 7.

Thanks!

Community
  • 1
  • 1
codedude
  • 6,244
  • 14
  • 63
  • 99

3 Answers3

1

If you're working on Windows, you can add the line system("pause"); at the end of the program. Or cin.get() if on Linux

StackHeapCollision
  • 1,743
  • 2
  • 12
  • 19
1

Depends on what you're using. For Windows it is common to use a breakpoint (__debugbreak) but for Unix people usually just run it in an existing terminal which does not close.

Puppy
  • 144,682
  • 38
  • 256
  • 465
0

I don't know what kind of OS do you use, but the function is similary

start a terminal in Linux oder a Commandline in Windows. Switch with the command "cd" to the directory where the Program lies.

Linux:

./myProgram

Windows

myProgram

The Terminal which you started wouldn't be closed ;)

demonking
  • 2,584
  • 5
  • 23
  • 28