Author Topic: Keypresses in C++  (Read 1162 times)

0 Members and 1 Guest are viewing this topic.

Offline hellninjas

  • LV7 Elite (Next: 700)
  • *******
  • Posts: 625
  • Rating: +17/-0
    • View Profile
Keypresses in C++
« 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".

Offline shmibs

  • しらす丼
  • Administrator
  • LV11 Super Veteran (Next: 3000)
  • ***********
  • Posts: 2132
  • Rating: +281/-3
  • try to be ok, ok?
    • View Profile
    • shmibbles.me
Re: Keypresses in C++
« Reply #1 on: March 26, 2013, 12:56:59 am »
take a look at std::cin.

Offline MGOS

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 336
  • Rating: +95/-0
    • View Profile
Re: Keypresses in C++
« Reply #2 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;
}