fork download
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4.  
  5. bool readAndDisplayRecord(std::istream& is)
  6. {
  7. std::cout << "Name: ";
  8.  
  9. float grade;
  10. while (!(is >> grade)) // A name is not a number.
  11. {
  12. is.clear(); // clear the stream's error state.
  13. std::string name;
  14. if (!(is >> name))
  15. return false;
  16.  
  17. std::cout << name << ' ';
  18. }
  19.  
  20. std::cout << "\nGrades:\n\t" << grade << '\n';
  21. for (unsigned i = 0; i < 2; ++i)
  22. {
  23. if (!(is >> grade))
  24. return false;
  25.  
  26. std::cout << '\t' << grade << '\n';
  27. }
  28.  
  29. return true;
  30. }
  31.  
  32. int main()
  33. {
  34. std::istringstream input_stream(
  35. std::string(
  36. "Achmed Abdul Jonah Josh Tonn 75.5 80.0 99.9\n"
  37. "John Smith 80.0 90.9 100\n"
  38. "Samantha F Jones 70.0 79.0 75.0\n"
  39. )
  40. );
  41.  
  42.  
  43. while (readAndDisplayRecord(input_stream))
  44. std::cout << '\n' ;
  45. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
Name: Achmed Abdul Jonah Josh Tonn 
Grades:
	75.5
	80
	99.9

Name: John Smith 
Grades:
	80
	90.9
	100

Name: Samantha F Jones 
Grades:
	70
	79
	75

Name: