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?
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?
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.
getchar()
as suggested here. –
Hennessey 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.
Assuming you're on Windows, replace the system("pause")
with system("pause > NULL")
.
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:
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).
That line is part of the system("pause")
. You can try a different method, such as getline(std::cin, variable)
or cin.get()
.
Use
system("pause>nul")
It works perfectly for windows!
© 2022 - 2024 — McMap. All rights reserved.
std::cin.get()
instead. – Galilee