How can I make it so the user doesn't have to press enter when entering something on a console program? Like for example, the program says "Press Q to quit", the user only has to press "Q", and not "Q" than ENTER? I read something about it before, but I didn't pay much attention to it before. Could someone explain how to do it or post a reference that explains it?
I don't think there's a way to do this with standard C++, but there are various methods to get keypresses - on windows you could use the WinAPI:
1 2 3 4 5 6 7 8 9 10 11
#include <iostream>
#include <windows.h>
int main()
{
std::cout<<"Press Q to exit"<<std::endl;
while(!(GetASyncKeyState(VK_Q)>0));
return 0;
}
/*not 100% sure if it was like that, has been a while since I last used the WinAPI
and I don't have access to windows right now*/
It's Async.
Using that function isn't all that much of a good idea, since it registers key presses even outside the window.
That's not the expected behavior and some antivirus software doesn't like programs that use the function either.
(and without a "Sleep" it would burn all CPU time it can get until someone finally hits a key)