I'm trying to understand better the std::cin behavior.
Let's see the following code:
#include <iostream>
int main()
{
char c, cin {'8'};
std::cin >> c; // enter '.'
std::cin.putback(c);
double d {88};
std::cin >> d; // expected to fail.
if (std::cin)
std::cout << "double d = " << d << '\n';
else
{
std::cin >> cin; // doesn't prompt user for entry neither put c into cin.
std::cout << "cin = " << cin << '\n';
}
std::cout << "d = " << d << '\n'; // just to check.
}
I wonder to get what explain the fact of std::cin >> cin; doesn't prompt for user entry neither attribute the value of c that was previously putted back into the std::cin stream to the variable cin.
Note:
- It's known that
std:: cin >>skips whitespace (space, newline, tab, etc.)
Aucun commentaire:
Enregistrer un commentaire