peak in c 2b 2b

Solutions on MaxInterview for peak in c 2b 2b by the best coders in the world

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