Good day. I study the SWITCH instruction. There was a problem, can not I force the compiler to read the space?

#include <iostream> int main() { char ab; std::cin >> ab; switch(ab) { case ' ': std::cout<<"space"<<std::endl; break; default: std::cout<<"Error"<<std::endl; break; } return 0; } 

    1 answer 1

    To read a single character, it is better to use the get function:

     cin.get(ab); 

    Just cin>> skips whitespace characters, considering them irrelevant :)

    And more ... Real reading will occur after input. Input is buffered , i.e. the actual placement of the character in ab will be after entering the entire line, after which, by the way, the '\ n' will remain in the buffer ... But for your example, this is not important yet.