peeker.cpp 1010 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. // peeker.cpp -- some istream methods
  2. #include <iostream>
  3. int main()
  4. {
  5. using std::cout;
  6. using std::cin;
  7. using std::endl;
  8. // read and echo input up to a # character
  9. char ch;
  10. while(cin.get(ch)) // terminates on EOF
  11. {
  12. if (ch != '#')
  13. cout << ch;
  14. else
  15. {
  16. cin.putback(ch); // reinsert character
  17. break;
  18. }
  19. }
  20. if (!cin.eof())
  21. {
  22. cin.get(ch);
  23. cout << endl << ch << " is next input character.\n";
  24. }
  25. else
  26. {
  27. cout << "End of file reached.\n";
  28. std::exit(0);
  29. }
  30. while(cin.peek() != '#') // look ahead
  31. {
  32. cin.get(ch);
  33. cout << ch;
  34. }
  35. if (!cin.eof())
  36. {
  37. cin.get(ch);
  38. cout << endl << ch << " is next input character.\n";
  39. }
  40. else
  41. cout << "End of file reached.\n";
  42. // keeping output window open
  43. /*
  44. cin.clear();
  45. while (cin.get() != '\n')
  46. continue;
  47. cin.get();
  48. */
  49. return 0;
  50. }