I want to do a compare statement that compares what key the user has pressed,
it would look something like this:
(var) pressedkey;
cout<< "Press a key!";
(cin or get)>> pressedkey;
cout<< " You pressed "<< pressedkey << "key!";
But i dont want to compare characters, i want to compare for example did the user pressed "return(enter)" key, in scar (pascal based macro program) enter is char 13
Even when text " |use emergency over-ride key 13(execute)" appears and i press the enter (return) key it still doesnt break the loop, what am i doing wrong?
Because you're using cin.get() which doesn't work for this kind of thing.
cin.get() waits for the desired key to be pressed. It doesn't modify the contents of key, therefore it stays as whatever it was originally.
std::cin >> myKey;
std::cin.ignore(std::numeric_limits<std::streamsize>::max, '\n');
if (myKey == 13) // Enter pressed
doSomething();
I forgot about that. cin waits for enter... hm.
Try this: key = std::cin.get()
cin.get() doesn't modify the contents of key, it just waits for the character you passed as a parameter to be pressed.
In the above example, cin.get() does modify key, because key is assigned the character pressed.
I know, I'm trying to think of a way.
The only way I know of is to use getch(), but I really, really, really can't stress enough how much you should avoid using it.
Edit:
I think you'll need to use ncurses/curses. I don't know of any other way to do it, because the input is buffered so you have to press enter for the terminal to send the input to the program.
It's old, deprecated, non-standard, platform (and sometimes compiler) dependant. It's only included in some compilers because of how popular it was.
ncurses or curses will be able to do the same thing, but in a better way.
I told you a way.
Curses.
#include <curses.h>
and then you can use the getch() from curses. The other getch is from conio.h which is the one you should never use.
You'll also have to use the I/O functions included with curses.