Alternative for system("PAUSE")??

Hey guys, this should be an easy answer for all you experts out there.
Im making myself a text based RPG, so i can see how deep i understand the language. And i wanted some help creating my own personalized system("PAUSE").

Im having a tough time getting it to work. Now, i dont want a snippit of code to JUST to replace the system("PAUSE") commonly placed at the end of a main function, like most beginners are taught to do. I want a PAUSE that would would work anywhere in my program

I tried this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
void SomeFunction()
{
 while(){
  std::string name, confirm;
  std::cout << "enter your name: "
  ...
  ...
  std::cout << "Are you sure this is your name?"
  std::cin >> confirm;
  if(confirm == "No")
  {
    "I see, Press ENTER to try again"
    cin.clear(); 
    cin.ignore(); //This ignore didnt work
    continue;
  }
 }
}

When i put this in, it went straight through the ignore, to the continue

1
2
3
4
5
6
7
8
9
  std::cin >> confirm;
  if(confirm == "No")
  {
    "I see, Press ENTER to try again"
    cin.clear(); 
    cin.ignore(1); //This ignore STILL didnt work
    continue;
  }

I tried to adjust like this, thinking it would erase the 1 '\n' between cin >> confirm and cin.ignore(), but it didnt act like i wanted it to, and it still went straight through, past the ignore, to the continue

1
2
3
4
5
6
7
8
9
  std::cin >> confirm;
  if(confirm == "No")
  {
    "I see, Press ENTER to try again"
    cin.clear(); 
    cin.ignore(1, '\n'); //This ignore didnt work
    continue;
  }


Tried this, as you can tell, i dont know what the heck i am doing!

1
2
3
4
5
6
7
8
9
  std::cin >> confirm;
  if(confirm == "No")
  {
    "I see, Press ENTER to try again"
    cin.clear(); 
    cin.ignore(2); //Ignore finally works, but has a huge flaw T_T
    continue;
  }

Just playing trail and error, i got to this point, and the ignore finally had a notable effect! Except alas, i needed to hit enter TWICE before it moved to the continue;

So now im really confused. I really dont want to use system("PAUSE"), not because i was told it was bad, but because if i just use it... well then that means im not getting any better.

Im sure i will find the answer in the book im reading "C++ Primer 5th", but im only on chapter 3, and im sure it will be while until my problem is addressed


Sense i barely know what the heck i am doing with cin.ignore, im at a huge loss
i didnt read how to use it in an article or book, and i wasnt told what i am doing WHILE im using it
I was just bluntly told
"Use this instead of system("PAUSE")"
"system() is NOT good to use"

************
Conclusion
************

SO, im looking for this answer:

How can i make my own custom system("PAUSE"):
I want to be able to choose the button pressed to continue.
(EX: Press Esc to exit...) (Press Z to attack or Press X to flee)

If its also not too much trouble, im wondering how i can clear the screen without using system("CLS") aswell.

Something like this in human terms
1 - Pause Program
2 - If user hits Esc (exit)
* - If user hits Z (attack)
* - If user hits X (flee)
3 - Clear Screen
4 - Execute {exit()||attack()||flee()}

THE CLEAR SCREEN IS NOT AS IMPORTANT AS THE CUSTOM PAUSE.
Please focus on the pause in your posts, and let the clear screen come secondary


Thank you in advance for taking the time to read and/or answer my question =]
You rock if your reading this sentence right now!

Note that cin.clear(); is used to clear error flags. It doesn't discard any of the input data.

Streams are not really designed to pause. std::cin will pause only because it tries to read but there is nothing there to read, so it waits until there is something to read.

cin.ignore() will discard one character from std::cin. That is the purpose of ignore. It will only pause if there is no character to be discarded.

You could try to do something like cin.ignore(80, '\n'); to ignore all characters (max 80) up to and including the next newline character, but this will only pause if there is no newline character already in std::cin. If the last thing you did was to read from cin using operator>> there will be a newline character because >> doesn't remove the newline character after what was read, so in that case you will have to place two such lines in succession to make it pause.

If you want more control over the console you should consider using the WinAPI or a library like pdcurses. Only using standard C++ you can't react to individual key presses and the only way I know to clear the screen would be to print a bunch of newlines so that the old content is pushed out of sight.
Blocking for a period of time is possible with C++11.

1
2
3
4
#include <thread>
#include <chrono>

void block( int secs = 10 ) { std::this_thread::sleep_for( std::chrono::seconds(secs) ) ; }



There is no portable way of emulating the behaviour of system("PAUSE")

See if this gives you something close to what you want on your implementation:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>

void pause( const char* prompt = "press enter to continue..." ) 
{
    std::cin.clear() ;
    std::streambuf* buf = std::cin.rdbuf() ;
    const int eof = std::char_traits<char>::eof()  ;

    if( buf->in_avail() || ( buf->sungetc() != eof && std::cin.get() != '\n' ) )
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ;

    std::cout << prompt ;
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ;
}

@Peter87

thank you for explaining what these 2 statements are doing, that was a very helpful post.

@JLBorges
Thank you! i found the void block() very useful and helpful, and it seems simple enough for my to understand

but as for the system("PAUSE") , im going to pass on this, only because if i used the above code... well i wouldnt know what the heck i am doing. Using code that i dont understand is the whole reason why im here posting this thread:
(I was told to use cin.ignore() / cin.clear() without being told what the heck it was doing)


For now i will find a ways around my problems, and in the mean time, do some research on the WinAPI, and pdcurses.
Thank you both!
Last edited on
> in the mean time, do some research on the WinAPI ...

If you are using the Microsoft implementation, this should work
(and you should be able to understand what it does):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <limits>

void pause( const char* prompt = "press enter to continue..." ) 
{
    std::cin.clear() ; // clear failed/error states of the stream if they are set

    // std::cin.rdbuf() - get the input buffer 
    // http://www.cplusplus.com/reference/ios/basic_ios/rdbuf/
    // ->in_avail() get the number of characters available to read
    // http://www.cplusplus.com/reference/streambuf/streambuf/in_avail/

    if( std::cin.rdbuf()->in_avail() ) // if there are any characters in the input buffer
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ; // throw them away

    std::cout << prompt ;
    std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' ) ;
}

Last edited on
looks like the only way to this is to use a non-standard function, my friend.
i would suggest using _getch()microsoft or getch()usually.
consider this code:
1
2
3
4
5
6
7
8
9
10
11
#include<iostream>
#include<conio.h>
void pause()
{
	std::cout<<"Press Any Key To Continue";
	_getch();
}
void main()
{
	pause();
}



i've been brainstorming this problem for about two hours now, but i didn't find any way to solve it with standard C++.

EDIT:
i didn't see JLBorges's comment by the time i posted this, and due to my tiny knowledge in the inner workings of streams, i couldn't see that solution, i strongly recommend checking his solution.
Last edited on
Topic archived. No new replies allowed.