1

in a program that takes a character and prints the equivalent ASCII value.

ASCII value for the space bar is: '32'.

but if i press the space bar then the enter key, program still waits for an input, and doesn't get the space bar character recognized.

#include <iostream>

using namespace std;

int main()
{
    char ch;


    cout << "Enter the character: ";
    cin >> ch;

    cout << "ASCII code is: " <<(int)ch << endl;


    return 0;

 }
Ezzat
  • 35
  • 1
  • 7

3 Answers3

8

By default, the "formatted input" functions of std::istream begin by skipping all whitespace characters.

If you just want the very next character whether it's whitespace or not, you can use the unformatted input function get:

std::cin.get(ch);

It's also possible to modify the stream so that it will never skip whitespace, using std::noskipws:

std::cin >> std::noskipws >> ch;

But note that unless you then reset that flag to its old value, the flag will will stay set on the std::cin object, which could mess up other functions that try to use it.

aschepler
  • 70,891
  • 9
  • 107
  • 161
  • Ahh there's the page I was looking for. I'll have to save that. +1 – scohe001 Sep 26 '17 at 22:53
  • 1
    @scohe001 In my humble opinion, it helps to start from cppreference.com, and never from cplusplus.com. – aschepler Sep 26 '17 at 22:58
  • And low and behold it was on the cppreference page for the extraction operator did specify behavior with whitespace. My eyes have been opened! – scohe001 Sep 26 '17 at 23:10
2

I can't actually seem to find it written anywhere in the docs, but the extraction operator (<<) ignores leading whitespace and reads as many characters as it can until it finds a whitespace character.

Edit:

Looks like the cppreference page for the extraction operator actually does specify the behaviour with regard to whitespace: "After constructing and checking the sentry object, which may skip leading whitespace, extracts a character and stores it"

So for you, the space and newline get eaten and the extraction operator continues waiting, since it hasn't read any non-whitespace characters.

If you want to read a single character (whitespace or not), use get. ie:

char ch;

cout << "Enter the character: ";
cin.get(ch); //can also be called ch = cin.get();
cout << "ASCII code is: " << static_cast<int>(ch) << endl;

(also as an aside--since this is c++, you should be using static_cast instead of c-style casting. See my code above for an example)

scohe001
  • 15,110
  • 2
  • 31
  • 51
0

If you want to detect a arrow key you can do something like this:

  1. Write the header file.
  2. Type : #define KEY_UP 72
  3. You can do something like this in your main function or whatever function you're putting it in:
    char c;
    switch ((c = _getch())) {
    case KEY_UP:
        return 1;
        break;
    case KEY_DOWN:
        return 2;
        break;
    case KEY_LEFT:
        return 3;
        break;
    case KEY_RIGHT:
        return 4;
        break;
    }

This version worked for me with Visual Studio's C++ compiler, but what I'm basically saying is that all you need to do is to define the ASCII value which is what I did, and then you can use a "case" statement, with a getch to do the rest. BTW: if you having problems finding ASCII values for shapes then tap this link: https://www.google.com/search?q=ascii+table+c%2B%2B&rlz=1C1CHBF_enUS913US913&sxsrf=ALeKk02T5LwRBRagCAbo0EoLJyxN3du6IQ:1606516668152&source=lnms&tbm=isch&sa=X&ved=2ahUKEwjxvNL65KPtAhVHXq0KHcNNAyoQ_AUoAXoECAQQAw&biw=1536&bih=754