I read this link Pause Console in C++ program and I don't still get the correct answer based on my needs.
I want a function that behaves exactly the same with system("pause") but it should be portable and has no additional prints.
I also don't want std::cin.get() and getchar() because it still echoes the characters except for the enter key.
getch() still works but it is not portable.
Goal:
Will proceed after pressing any key, not only the enter key. (remember: only 1 key)
Will not use any libraries aside from the standard library.
Related
So, if you don't really know what do I mean, there's a small explanation. I'm also new in C++ so I need help with some things, so yeah. There's the explanation.
If you make a code, for example
cout << "Hello world!";```
-- I forgot how to make it fully good, but that's an example
Then it prints and closes extremelly fast. So, how can I make it so that it DOESN'T dissapear?
I am assuming you are talking about console window that closes fast.
In order to prevent that there are multiple ways of doing it. But i will write two simplest ways of doing so.
Add header at the top of your C++ program
#include <stdlib.h>
Then in the end of main function just before return 0 write this line of code
system("pause");
Another way of doing this is to get any character input before return 0. So write these lines of code
Console.WriteLine("Press any key to exit...");
Console.ReadKey();
One of the simplest solutions is to use std::getchar() or
std::cin.get(). The program will wait for user input.
However, if there are already characters in the input buffer, then the program, roughly speaking, will ignore and will not wait for input from the user.
If the program is specific to Windows, then one of the most common solutions is to use std::system("pause").
For std::system(), you must also include the cstdlib header.
But it would still be better to set a breakpoint at the end of the program or, if your IDE allows (for example Visual Studio), then enable the option that leaves the console opened even after exiting. For example, VS allows you to run a program in non-debug mode, which does not close the console. To run the program in this mode, you can press the key combination Ctrl + F5.
However, in this case, in the project settings there should be Subsystem option in the System section in the Linker. And the value of Subsystem should be /SUBSYSTEM:CONSOLE.
I'm making a simple program using the graphic mode in C++, I'm having trouble dealing with the input.
I just need a way to check the keyboard and, if any key has been pressed, store the key value on a variable,
the problem with getch() is that it waits untill any key has been pressed, and I need the programm to keep going if the user is not giving input.
I've made a research and it seems that there are hundreds of ways of doing this.
which one is the easiest?
Example: the loop begins, the program checks if there has been any input and stores it, the program works with that input and then discards it to begin the loop again. That would work perfectly with getch() isn't it? Well I want the loop to also keep going if no key has been pressed this time. A sort of no input default mode so the process doesn't stop.
(what I would really like is an equivalent to getch() that does'nt wait for user input).
Windows, Code::Blocks IDE, GCC compiler
yeah, kbhit seems to work as expected, Thanks!!
Use int _kbhit( void ); : it's a non-blocking call to getch()(works only on Windows).
It's also included in conio.h
Doc here : http://msdn.microsoft.com/en-US/en-en/library/58w7c94c.aspx
(There is also a workaround for *nix systems : http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html )
I have a homework assignment to create a game. So I have created a game that generates random numbers, displays them, then after a few seconds the numbers disappear and the user has to enter them in descending order. The game works fine and all the output is correct... but the problem is I have created different functions for creating numbers, arranging them, accept.. and I have to press the enter key a few time each time a function call comes.
Is there a way by which these functions get called without pressing any key to make the program work without pressing the enter key?
Pulled from Capture characters from standard input without waiting for enter to be pressed
That's not possible portably in pure C++, because it depends too much
on the terminal used that may be connected with stdin (they are
usually line buffered). You can, however use a library for that:
conio available with windows compilers. Use the function _getch() to
give you a character without waiting for the enter key. I'm not a
frequent windows developer, but i've seen my classmates just include
conio.h and use it. See conio.h at wikipedia. It lists getch, which
is declared deprecated in Visual C++.
curses available for linux, compatible curses implementations are
available for windows too. It has also a getch function. (try man
getch to view its manpage). See Curses at wikipedia.
I would
recommend you to use curses if you aim for cross platform
compatibility. That said, I'm sure there are functions that you can
use to switch off line buffering (i believe that's called "raw
mode", as opposed to "cooked mode" (look into man stty)). Curses
would handle that for you in a portable manner if i'm not mistaken.
I've got a C/C++ program that runs in a console window, and prompts the user to press any key to continue, or 'q' to quit. It currently uses _kbhit() to detect if a key has been pressed, and then uses _getch() to determine what character it was.
Now I want to be able to automate this program (from a C# app using a Process with RedirectStandardInput and CreateNoWindow set). Obviously I can no longer rely on _kbhit() as it uses ReadConsoleInput(), which fails when launched using my C# app. In my C# app I use process.StandardInput.Write("A") to push something onto the stream in an attempt to get my console app to continue.
In the console app I have used SetConsoleMode() to clear the ENABLE_LINE_INPUT flag so that I can use getchar() to return as soon as a character is pressed, and this works reasonably well (when I press a character key in the console window as well as when the call is made from the c# app). However, it has flaws in that it now only accepts characters keys (i.e. no F, Alt, Shift keys etc.) which isn't too big a problem, but moreso I seem to have to press return twice (and this is a key that lots of people will likely choose to press in the 'any key' situation).
Does anyone know how I can make the console app respond to a key (any a bonus, charcters and return only is acceptable) pressed ONCE, whilst still responding to a single character pushed onto the stream from my C# app?
Something I did notice, calling system("PAUSE") gives the exact behaviour I want, other than knowing what key was pressed so that I can quit on 'q'. I don't know how PAUSE does it though, and it doesn't let me use my custom message either :(.
I'm sure there's a really obvious solution, but it has been driving me mad. Any thoughts are much appreciated!
There are two issues with the resolutions:
Neither the C or C++ languages have
portable functions for waiting for a
keyboard key to be hit. It's an
operating system (platform) issue.*
C and C++ have different methods for
resolving I/O. Choose your
language, C or C++.
In my C++ console applications, I ask the user to "Press Enter to Continue" and use the ignore method of cin:
void Pause(void)
{
std::cout << "Press ENTER to continue.\n";
std::cout.flush(); // Insurance, make sure the text is displayed.
std::cin.ignore(100000, '\n'); // Ignore characters until an ENTER (newline) is received.
return;
}
I suggest you create a single file with the Pause function. You can write different versions for different platforms and let the build system select the correct file.
Not all platforms that run C or C++ are required to have keyboards. Many embedded systems don't have keyboards. Also, many windowing systems receive messages, events or signals when a key is pressed. Again, something different and not standard.
Here is a very good implementation for C++. Be sure to read over the entire tutorial, as I it may initially appear that it doesn't help you.
http://www.daniweb.com/forums/thread90228.html
Use cin.get(). This returns the appropriate key.
Thank you for all your responses, I've learned a lot about handling input!
However, I couldn't get anything to work exactly how I wanted, so I had to abandon the unified approach and put in a check to see if it was running in a window. If it is, I stick with _kbhit(), and if not I use PeekNamedPipe(), which gives me the number of characters sent from my C# app without transfering them into the stdin buffer. There's still a few issues that I have to work out to do with clearing any data that I don't want from the pipe, but it's solved my initial problem.
Thanks again for all the suggestions, they will undoubtably come in handy next time I have an input nightmare :)
I'm currently developing various console games in Windows that won't really work using regular input via cin.
How can I (In a simple way using only standard windows libraries available in MSVC):
Make the program wait for a (specific?) key press and return the key ID (It would have to work for all keys including the arrow keys)
During a real-time game check for the last pressed key of the user and if there was any key pressed since the last check.
It would really help if you could include a short example program for your solution
I've got just what you need.
Here enjoy pal:
C++ source
It's pretty much self-explanatory but if you have any doubts my email is jacobossm#gmail.com
AFAIK you can't do it using the standard C runtime. You will need to use something such as the Win32 function GetAsyncKeyState.
You want the Windows Console API, for example PeekConsoleInput.