-2
int i;
std::cin >> i; // i = 'a'

What is the reaction of std::cin when we try to do this?
As we know when the std::cin gets a value, it converts it into ASCII or something other and then stores it in a variable so what is the reaction of std::cin on this?

digito_evo
  • 3,216
  • 2
  • 14
  • 42
  • Did you try to debug it already? – RoQuOTriX Aug 24 '22 at 07:35
  • 2
    Have you tried reading some documentation about `>>` for stream input? What does your text-books say? Your tutorials? Your teachers? And while [this reference](https://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt) might be hard to read, there's a paragraph beginning with "If extraction fails (e.g. if a letter was entered where a digit is expected)," which explains exactly what happens. – Some programmer dude Aug 24 '22 at 07:40
  • [This reference](https://en.cppreference.com/w/cpp/locale/num_get/get) will provide even more details, allthough the short story is still: it fails. – Lukas-T Aug 24 '22 at 07:41

2 Answers2

3

No it doesn't store the ASCII value of the character you are entering into i. Instead, the stream will put a fail flag for the input stream meaning the reading of integer resulted in a failure. Here is the code to demonstrate that.

int i;
cout << cin.fail();     // 0 as cin doesn't failed yet
cin >> i;               // A char is entered instead of integer
cout << cin.fail();     // 1 as cin failed to read a character  
Sivaram
  • 151
  • 1
  • 6
  • 1
    You may want to add newline characters to the output, otherwise you will be mixing the input with the output on the same line. – Andreas Wenzel Aug 24 '22 at 07:55
3

You should do error checking on std::cin if the value you are reading is as you would have expected, otherwise std::cin fails and the value of i is set to 0:

#include <iostream>

int main()
{
    int i;
    if(std::cin >> i)
    {
        std::cout << i << std::endl;
    }
    else 
    {
        std::cin.clear();
        std::cout << "No integer" << std::endl;
    }

    return 0;
}

You can also handle the error detection using a while loop. An example could be like below:

#include <iostream>
#include <limits>


int main()
{
    int age { };

    // executes the loop if the input fails or is out of range (e.g., no
    // characters were read)
    while ( ( std::cout << "How old are you? " ) &&
            ( !( std::cin >> age ) || age < 1 || age > 200 ) )
    {
        std::cout << "That's not a number between 1 and 200; ";
        // clear bad input flag
        std::cin.clear();
        // discard bad input
        std::cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
    }

    std::cout << "You are " << age << " years old.\n";
}

Sample input/output:

How old are you? a
That's not a number between 1 and 200; How old are you? #12gj
That's not a number between 1 and 200; How old are you? foo
That's not a number between 1 and 200; How old are you? 84
You are 84 years old.
digito_evo
  • 3,216
  • 2
  • 14
  • 42
RoQuOTriX
  • 2,871
  • 14
  • 25