Answers for "peak in c++"

C++
0

peak in c++

// 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;
}
Posted by: Guest on May-09-2020

Browse Popular Code Answers by Language