0

I’m having a bit of a problem in C++. When I wrote this:

int a = ‘:‘;
cout << a;

This printed out 58. It checks out with the ASCII table.

But if I write this:

int a;
cin >> a;
//i type in “:”
cout << a;

This will print out 0. It seems like if I put in any non-numeric input, a will be 0. I expected it to print out the equivalent ASCII number.

Can someone explain this for me? Thank you!

1
  • 1
    Did cin >> a; successfully scan an int? Commented Jan 12, 2021 at 10:39

3 Answers 3

4

There are two things at work here.

First, ':' is a char, and although a char looks like a piece of text in your source code, it's really just a number (typically, an index into ASCII). This number can be assigned to other numeric types, such as int.

However, to deal with this oddity in a useful way, the IOStreams library treats char specially, for a numeric type. When you insert an int into a stream using formatted insertion (e.g. cout << 42), it automatically generates a string that looks like that number; but, when you insert a char into a stream using formatted extraction (e.g. cout << ';'), it does not do that.

Similarly, when you do formatted extraction, extracting into an int will interpret the user's input string as a number. Forgetting the char oddity, : in a more general sense is not a number, so your cin >> a does not succeed, as there is no string that looks like a number to interpret. (If a were a char, this "decoding" would again be disabled, and the task would succeed by simply copying the character from the user input.)

It can be confusing, but you're working in two separate data domains: user input as interpreted by IOStreams, and C++ data types. What is true for one, is not necessarily true for the other.

Sign up to request clarification or add additional context in comments.

Comments

2

You're declaring a as an int, then the operator>> expects digits, but you give a punctuation, which makes extraction fails. As the result, since C++11, a is set to 0; before C++11 a won't be modified.

If extraction fails (e.g. if a letter was entered where a digit is expected), value is left unmodified and failbit is set. (until C++11)

If extraction fails, zero is written to value and failbit is set. (since C++11)

And

I expected it to print out the equivalent ASCII number.

No, even for valid digits, e.g. if you input 1, a will be set with value 1, but not its ASCII number, i.e. 49.

Comments

1

This will print out 0. It seems like if I put in any non-numeric input, a will be 0. I expected it to print out the equivalent ASCII number.

Since C++11 when extraction fails 0 will be automatically assigned.

However, there is a way where you can take a char input from std::cin and then print its ASCII value. It is called type-casting.

Here is an example:

#include <iostream>

int main()
{
    char c;
    std::cin >> c;
    std::cout << int(c);
    return 0;
}

Output:

:
58

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.