fork download
  1. #include <algorithm>
  2. #include <iomanip>
  3. #include <ios>
  4. #include <iostream>
  5. #include <string>
  6. #include <vector>
  7. #include <sstream>
  8.  
  9. using std::cin; using std::setprecision;
  10. using std::cout; using std::string;
  11. using std::endl; using std::streamsize;
  12. using std::sort; using std::vector;
  13.  
  14. int main() {
  15. vector<string> names;
  16. vector<double> finals;
  17. typedef vector<double>::size_type vec_sz;
  18. vec_sz sizeStud;
  19. string name;
  20. double sum, grade;
  21. int counter;
  22. std::istringstream cin("fred sally joe end-of-input 10 20 end-of-input 30 end-of-input");
  23. cout<<"Please enter the student's names \"end-of-input\" to end:\t";
  24. while(cin>>name&&name!="end-of-input") {
  25. names.push_back(name);
  26. }
  27. sizeStud = names.size();
  28. if(sizeStud == 0) {
  29. cout<<"No students entered! Please try again."<<endl;
  30. return 1;
  31. }
  32.  
  33. for(int i = 0; i < sizeStud; i++) {
  34. sum=0;
  35. counter=0;
  36. cout<<"Please enter the grades for "<<names[i]
  37. <<" \"end-of-input\" to end:\t";
  38. while(cin>>grade) {
  39. counter++;
  40. sum+=grade;
  41. }
  42. if(counter==0) {
  43. cout<<"No grades entered! Please try again."<<endl;
  44. }
  45. else {
  46. cout<<endl;
  47. finals.push_back(sum/counter);
  48. }
  49. }
  50. for(int i =0 ; i < sizeStud; i++) {
  51. streamsize prec = cout.precision();
  52. cout<<names[i]<<"'s final:\t\t"<<setprecision(4)
  53. <<finals[i]<<setprecision(prec)<<endl;
  54. }
  55.  
  56. return 0;
  57. }
Success #stdin #stdout 0s 3432KB
stdin
Standard input is empty
stdout
Please enter the student's names "end-of-input" to end:	Please enter the grades for fred "end-of-input" to end:	
Please enter the grades for sally "end-of-input" to end:	No grades entered! Please try again.
Please enter the grades for joe "end-of-input" to end:	No grades entered! Please try again.
fred's final:		15
sally's final:		1.719e-312
joe's final:		1.316e-312