Omnimaga

General Discussion => Computer Programming => Technology and Development => C and C++ => Topic started by: hellninjas on March 25, 2013, 08:26:52 pm

Title: Keypresses in C++
Post by: hellninjas on March 25, 2013, 08:26:52 pm
I've read up a bit through google.
getch(); from the <conio.h> seems to have a lot of hits, yet i'm not sure how to use it :c
Or are there other ways?
I'd mostly like to pause the program and wait for a keystroke, then check if the user pressed "y" or "n".
Title: Re: Keypresses in C++
Post by: shmibs on March 26, 2013, 12:56:59 am
take a look at std::cin (http://www.cplusplus.com/reference/iostream/cin/).
Title: Re: Keypresses in C++
Post by: MGOS on March 26, 2013, 09:03:05 am
getch() is exactly the right thing you need. It waits for a keypress and returns the character that was typed.

Code: [Select]
#include <conio.h>

...

char c;
do {
  c = getch();
} while (c != 'y' && c != 'n');

if (c == 'y'){
  do stuff;
} else {
  do something else;
}