fork download
  1. #include <iostream> /// cin, cout
  2. #include <climits> /// INT_MAX
  3.  
  4. using namespace std;
  5.  
  6. /// test the get(c) stream member function
  7. void get0()
  8. {
  9. char c;
  10.  
  11. cout << "Please type some characters: ";
  12. cin.get(c);
  13.  
  14. cout << "the 1st "
  15. << " character typed: "
  16. << c << endl << endl;
  17. }
  18.  
  19.  
  20. /// test the get(p, n) stream member function
  21. void get1()
  22. {
  23. char word[10];
  24. int n = 5;
  25.  
  26. /// Skip the rest of the buffer.
  27. cin.ignore(INT_MAX);
  28.  
  29. cout << "Please type some characters: " << endl;
  30. cin.get(word, n);
  31.  
  32. cout << "the 1st " << n - 1
  33. << " characters typed: "
  34. << word << endl << endl;
  35. }
  36.  
  37.  
  38. int main()
  39. {
  40. get0();
  41. get1();
  42. }
Success #stdin #stdout 0s 16064KB
stdin
123456789
abcdefg

stdout
Please type some characters: the 1st  character typed: 1

Please type some characters: 
the 1st 4 characters typed: