truncate.cpp 775 B

12345678910111213141516171819202122232425262728
  1. // truncate.cpp -- using get() to truncate input line, if necessary
  2. #include <iostream>
  3. const int SLEN = 10;
  4. inline void eatline() { while (std::cin.get() != '\n') continue; }
  5. int main()
  6. {
  7. using std::cin;
  8. using std::cout;
  9. using std::endl;
  10. char name[SLEN];
  11. char title[SLEN];
  12. cout << "Enter your name: ";
  13. cin.get(name,SLEN);
  14. if (cin.peek() != '\n')
  15. cout << "Sorry, we only have enough room for "
  16. << name << endl;
  17. eatline();
  18. cout << "Dear " << name << ", enter your title: \n";
  19. cin.get(title,SLEN);
  20. if (cin.peek() != '\n')
  21. cout << "We were forced to truncate your title.\n";
  22. eatline();
  23. cout << " Name: " << name
  24. << "\nTitle: " << title << endl;
  25. // cin.get();
  26. return 0;
  27. }