0

When I run a program through command line, once the program ends, cmd instantly closes, so I can't see the output easily. Is there anyway to stop this from happening so I can actually verify the output?

#include<iostream>
using namespace std;
class Exercises {
public: 
    void sayHello(int x) {
        for (int i = 0; i < x; i++)
            cout << "Hello!!" << endl;
    }
}exercise;

int main() {
    exercise.sayHello(4);
    return 0;
}
abaratham
  • 445
  • 1
  • 5
  • 19
  • If it's closing the window when it's run through the command line, we need code to see what's explicitly closing it. – chris Mar 19 '13 at 01:48
  • edited the code in. I tried removing `return 0;` but that didn't work – abaratham Mar 19 '13 at 01:49
  • 1
    @Jueecy, An object declaration. – chris Mar 19 '13 at 01:51
  • 1
    system("pause"); or getch() will do it. If you run it through the cmd line, you just get the prompt back - your program's output stays on the screen. I suspect that you mean "when I run it from within my IDE" that the program's output window closes. Look in your IDE for an option to automatically close the output window. - Also, please copy/paste code in the future. Your code above wouldn't compile. I.e loook at pluralization/capitalization of 'exercise'. – enhzflep Mar 19 '13 at 01:52
  • 1
    @abaratham, Nothing in here would close the window. By "run through command line", I would expect you to mean you open up a terminal and type `./exercise.exe` or whatever variant. – chris Mar 19 '13 at 01:52
  • @chris, totally forgot about it :P – Shoe Mar 19 '13 at 01:54
  • 1
    The standard way for this is a combination of `cin.ignore` and `cin.get`. – chris Mar 19 '13 at 01:54
  • 1
    @enhzflep *Please*, don't recommend the use of `system("pause")`. It's not only platform specific (the system may not have a `pause` command, and if it does, it may do *weird* things, such as pausing the cooling system of a nuclear reactor) but it's also **not** the *right* way to do this. For more, check out http://stackoverflow.com/questions/1107705/systempause-why-is-it-wrong – Nik Bougalis Mar 19 '13 at 01:57
  • @enhzflep in response to "your code above wouldn't compile": please make sure you know what you are talking about before you try to correct someone like that. But thanks for your advice, although, i'm not using an IDE, I'm using Sublime Text to build my code, then running the file that is created in my project folder. – abaratham Mar 19 '13 at 01:59
  • @enhzflep and the code, as shown, works fine. `Exercises` is the name of the class and `exercise` is the name of a variable, associated with an instance of the class. – Nik Bougalis Mar 19 '13 at 02:00
  • and @chris your suggestion of cin.ignore seems to have helped get rid of the symptom, but I have a feeling there is a better way of doing it/ I am doing something wrong? – abaratham Mar 19 '13 at 02:01
  • @abaratham, As far as standard, guaranteed behaviours go, that combination is the best I know of. You have to adjust it based on what's left in the input buffer in order to not have it just skip over or wait for two enter presses, though. – chris Mar 19 '13 at 02:02
  • In Visual studio, you can press ctrl+f5. – Zhi Wang Mar 19 '13 at 02:07

4 Answers4

5

You can also use cin.get();

It will wait for you to press enter or until you close the program.

Shan
  • 3,057
  • 4
  • 21
  • 33
2

Following methods can help in keeping the command window till another input is provided.

#include <conio.h>
void main(){

// your program here

 getch();
}

Another way is to use system("pause"); at the end of your program.

Yasir Malik
  • 441
  • 2
  • 9
0

You can pause the execution of the program for a certain amount of time with:

sleep(5); // sleep for 5 seconds

You could place that at the end of the program before return 0;.

Shoe
  • 74,840
  • 36
  • 166
  • 272
0

If you don't mind waiting for a keypress at the end of your program, you could put something in.

The simplest way in Windows is to do:

system("pause");

Don't do this if you are releasing your software though. You can implement the behaviour of the pause command easily enough.

std::cout << "Press any key to continue . . . " << std::flush;
while( !_kbhit() ) Sleep(25);
getch();

That's using stuff from conio.h.

However, I'm concerned about the cmd shell itself closing. When you say you "run with cmd", are you actually running up a shell, then typing in your program name and hitting Enter? If that closes the shell, then something is wrong. More likely, you're running it by double-clicking the file in Explorer, right?

paddy
  • 60,864
  • 6
  • 61
  • 103