0

I'm just starting out in C++, and am trying to work out how to detec what key has been pressed. I understand the ASCII code for 'a' is 97. So...

1) What is wrong with the following program?

#include <iostream>
using namespace std;

int main()
{
char thing[1];

cin >> thing;
if (thing == 97)
    cout << "You pressed 'a'!\n";
return 0;
}

2) How can I make it so that rather than having to type the letter as an input and press enter, the program just accepts the keypress immediately?

OregonGhost
  • 23,359
  • 7
  • 71
  • 108
Boris
  • 1
  • 1
  • 1

3 Answers3

4

I'm just starting out in C++

Welcome! I know you will find learning to program C++ both confusing and enjoyable. May I suggest that you first acquire a good C++ book? There is an excellent list of such books here: The Definitive C++ Book Guide and List

1) What is wrong with the following program?

#include <iostream>
using namespace std;

You shouldn't import the entire std namespace. There are a lot of symbols in that namespace, and you will almost certainly collide with one of them at some point. I know that many beginner textbooks instruct you to do this. Don't.

int main()
{
  char thing[1];

There is no reason to declare thing an array. It should be char thing;.

  cin >> thing;

Because, when you do this, you create a buffer-overflow bug. Since thing is an array, cin will treat thing as a C-style string, and happily write the entire input line to it, even if it doesn't fit.

if (thing == 97)
  cout << "You pressed 'a'!\n";
  • (Assuming your didn't fix the definition of thing), thing==97 compares the array to the constant 97. More specifically, it compares the address of the first element of the array to the constant 97. This is a bad thing, and probably won't even compile.
  • (Assuming you fixed the definition of thing), the naked constant 97 is confusing to the readers of your code, including to yourself. Instead compare thing to the equally valid integral constant 'a'.

Putting it all together:

#include <iostream>
using std::cout;
using std::cin;

int main()
{
  char thing;

  cin >> thing;
  if (thing == 'a')
    cout << "You pressed 'a'!\n";
  else
    cout << "You pressed not-'a': " << thing << "\n";
  return 0;
}

2) How can I make it so that rather than having to type the letter as an input and press enter, the program just accepts the keypress immediately?

As others have pointed out, you must use a platform-specific API for this. On Microsoft Windows, try getch().

Community
  • 1
  • 1
Robᵩ
  • 163,533
  • 20
  • 239
  • 308
1

1)

cin >> thing

is unsafe if they press more than one character before pushing enter, as you've only allocated space for 1 character.

Also, you want

if(thing[0] == 97) // ...

2) There is no standard way. The function getch() works on some platforms.

sje397
  • 41,293
  • 8
  • 87
  • 103
0

Use the function _getch() to give you a character without waiting for the enter key. Just include conio.h and use it.

It works on Windows but it is listed as deprecated for visual C++. So it is a non standard way and not portable.

#include <iostream>
#include <conio.h>

using namespace std;

int main()
{
    int character;

    character = getch_();
    if (character == 97)
        cout << "You pressed 'a'!\n";
    return 0;
}

If you want to return the character code of the key pressed and output the character pressed to the console then you can use getche_()

Alok Save
  • 202,538
  • 53
  • 430
  • 533