Help, related to system("pause")

Hi,

I'm in some grave need of help.
I need a way to make the program pause without using the system("pause") as to make it compatible with any OS etc, and in addition I want a way to do it that makes it possible to alter the messeage given out on the screen, but does the same as the program behind the "press any button to continiue..." messeage.

I'm using Dec-C++ as compilator.

Thanks in Advance, Rev
How about getch(). Which waits for a keypress like enter.
For a message you could use printf() or cout()
For some reason it doesn't seem like I have included the overhead of the getch() command, any idea what I have to include?
getch() is a curses function, which you may or may not have (include <curses.h> to find out...)

Alternatively, getchar(), which will wait until the user presses ENTER but only consume the first character of the input, is provided in stdio.h.

If your program is C++, you should include <cstdio> instead of <stdio.h>.

It recognize that command at least Jsmith, however.
It continiue straight on without pausing it.
closed account (z05DSL3A)

Try putting the following two lines just before getchar().
1
2
std::cin.clear();
std::cin.ignore(std::numeric_limits<streamsize>::max(),'\n');


*edit: You need to
#include<limits>

std::cin.clear() clears any error flags on the stream (cin)
std::cin.ignore() clears out the any unewanted junk in the stream.


Last edited on
Yay, it works.. Thank you so much :)
closed account (z05DSL3A)
Just as a follow up.

You need to keep an eye on what you do with streams, check the documentation to see what gets left in the stream after function calls. for example:

1
2
    int i=0;
    cin >> i;


The user will enter a series of numbers followed by return, so the stream will have somthing like 1234\n put into it. 'cin >> i' only takes out the numbers leaving the \n in the stream. Then later on code such as getchar() is called, it looks at the stream fins the\n and returns straight away.
Topic archived. No new replies allowed.