system("pause") clarification
Asked Answered
D

6

3

When i use system("pause"), then a line "Press any key to continue..." shows up on the screen.

This is iritating and makes reading the output quite cumbersome.

Is there some way to stop this from coming?

Davinadavine answered 23/2, 2013 at 10:8 Comment(1)
Use std::cin.get() instead.Galilee
A
7

Do you mean that you want to press any key to continue but not to display the "Press any key to continue" on the screen? Try this getchar(); this will capture one character typing from keyboard and continue.

Amberjack answered 23/2, 2013 at 10:11 Comment(1)
+1 This answer is simple and very good. Mine bellow is just to help the "clarification". Go with getchar() as suggested here.Hennessey
G
6

Rather than using platform dependent system("pause") you can use the platform independent std::cin.get() and if the buffer is messing with it, you can use:

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

before hand to clear the buffer.

Galilee answered 23/2, 2013 at 10:13 Comment(1)
will cin.get() read the charachter directly from the console without the buffer?Davinadavine
H
4

Assuming you're on Windows, replace the system("pause") with system("pause > NULL").

Hispaniola answered 23/2, 2013 at 10:11 Comment(1)
Thanks, good to know that you can pass the output of the program to the null device on windows too! :)Ambie
H
2

First of all, you should never use system("pause") because it is dangerous. Your code will be calling an external system procedure for no reason; and a cracker can find a way to substitute the "pause" command to other, making your program call the other program with your user permissions.

That said, you can avoid the message sending it to null device.

  • On Windows:

    pause > nul

And if you want to be bold to make this awful system call portable, you can use:

  • on linux:

    echo Press any key to continue ...; read x

Now you can apply the OR and AND (logic connectives) to both and make a system call that works on both systems:

void pause(void)
{
    system("echo Press any key to continue . . . && ( read x 2> nul; rm nul || pause > nul )");
    return;
}

Linux will create a temporary file called "nul" because it does not recognize this keyword. The null device on linux is /dev/null, not just nul. After that, the command will remove this temporary file with rm nul. So if you happen to have a file named nul on the same directory, be warned this command is not good for you (for yet another reason).

This command mimics the original. If you want to avoid the message, just remove the echo Pres... part of it.


Bonus:

  • Clear the terminal screen portably using system? (No, do not do this for the same reasons. Its dangerous.) But for tests purposes, you can use:

system("cls||clear");


Avoid pause. C is a language, one of the most powerful languages that there is. I'm sure there is a way to make a pause using only C (getchar() or scanf() for instance).

Hennessey answered 30/11, 2015 at 5:22 Comment(0)
P
0

That line is part of the system("pause"). You can try a different method, such as getline(std::cin, variable) or cin.get().

Parquet answered 23/2, 2013 at 10:13 Comment(0)
E
0

Use

system("pause>nul")

It works perfectly for windows!

Exhibitionism answered 29/5, 2017 at 16:43 Comment(1)
Hi Aayush. You are answering this question quite late. Can you tell the reader how your answer is better than the ones that have already been provided?Starspangled

© 2022 - 2024 — McMap. All rights reserved.