1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
|
// istream::peek example
#include <iostream> // std::cin, std::cout
#include <string> // std::string
#include <cctype> // std::isdigit
int main () {
std::cout << "Please, enter a number or a word: ";
std::cout.flush(); // ensure output is written
std::cin >> std::ws; // eat up any leading white spaces
int c = std::cin.peek(); // peek character
if ( c == EOF ) return 1;
if ( std::isdigit(c) )
{
int n;
std::cin >> n;
std::cout << "You entered the number: " << n << '\n';
}
else
{
std::string str;
std::cin >> str;
std::cout << "You entered the word: " << str << '\n';
}
return 0;
}
|