simple console halt

Hey guys, i've been using system("PAUSE") to halt the console and to see my output. But i've heard that it should not be used, so can anybody please tell me any other good alternatives so i can keep my console from closing instantly as soon as my program execution ends...
1. Open a console and run the program from it.
2. use something like cin.get();

A bunch of alternatives are also seen here:
http://cplusplus.com/forum/beginner/1988/
Or just see this Article about it:
http://www.cplusplus.com/articles/iw6AC542/

Hope this helps.
What IDE are you running? If VS, you can just "run without debugging", code::blocks leaves it open by default. I don't know about the others.
cin/cin.get/cin.getline/getch();
isn't getch(); a non standard library function ???
by the way i use both Netbeans IDE with cygwin and Dev-c++ with the mini-gw compiler..
I just define do this ....
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28

#include <iostream>

using namespace std;

bool run;
string choice;
    
void Pexit();

int main()
{
    run = true;
    while (run == true)
    {
        cout<<"/n What Do You Want To Do?"<<endl;
        cout<<" Type /q To Quit"<<endl;
        cin>>choice;
        
        Pexit();
    }
    return 0;
}
void Pexit()
{
    if(choice == "/Q" || choice == "/q")
    run = false;
}


The program keeps looping, even if a wrong command is put in. I find this pretty simple and works for a lot of simple console programs. Not sure what the community think of this, i have never used getch to be honest...

EDIT: When you do /q using this, it says 'Press another key to end' or whatever...
Last edited on
you can just include conio.h and it works with every compilator that I know off.
Do you know of GCC (often referred to as g++ by C++ coders)? That doesn't come with a conio.h implementation, and it's one of the world's most widely used compilers.
Correction: every compiler that i've worked with. Don't like the GCC layout. So I dont work with it. Simple enough?
layout?
I like to use a simple function to pause my console programs:
1
2
3
4
5
inline void pause()
{
      std::cin.sync();
      std::cin.ignore();
}

Then when you want to pause your program you just put pause();. Added bonus, if you happen to be including cstdlib for other things like "system()", "srand()" and "rand()" then use "atexit()" to make sure your application is held open at the very end. http://www.cplusplus.com/reference/clibrary/cstdlib/atexit/

I hope you mean you're using wxDev-C++: http://wxdsgn.sourceforge.net/
Don't like the GCC layout. So I dont work with it.


GCC doesn't have a layout. It's a compiler. Compilers don't have layouts.

I suspect that what you think is a compiler is not actually a compiler.

Simple enough?

Taking your own limited experience and assuming everyone else is the same is indeed pretty simple.
Last edited on
Topic archived. No new replies allowed.