Advertisement
crystalballin

Do While

Mar 21st, 2018
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. //////////////////////////////////////////////////////////////////////
  2. //
  3. // Name: Crystal Ballin
  4. // Date: 3/20/18
  5. // Class: CMPE 1370.02
  6. // Semester: Spring 2018
  7. // CSCI/CMPE 1370 Instructor: Gustavo Dietrich
  8. //
  9. // Use a sentinal controlled do ... while loop to find the average of
  10. // grades entered by the user. The sentinal value is -1.
  11. //
  12. //////////////////////////////////////////////////////////////////////
  13.  
  14. #include<iostream>
  15. #include<iomanip>
  16. using namespace std;
  17.  
  18. int main()
  19. {
  20.     int grade; // The grade entered by the user
  21.     int sum=0; // Sum of the grades
  22.     int count=0; // Number of grades entered
  23.     double avg=0; // Average of the grades
  24.  
  25.                 ////////////////////////////////
  26.                 // Start of your code
  27.  
  28.  
  29.                 // Use a do .. while loop to loop until the user enters -1
  30.                 // Each time through the loop, add the number input by the user
  31.                 // to the sum. Make sure not to include the sentinal value in the
  32.                 // sum or count!
  33.     do
  34.     {
  35.        
  36.        
  37.         cout << "Enter a grade, or a -1 to stop: ";
  38.             cin >> grade;
  39.  
  40.             sum+= grade;
  41.  
  42.             count++;
  43.  
  44.     }
  45.    
  46.     while (grade != -1);
  47.     {
  48.        
  49.        
  50.        
  51.     }
  52.     // Find the average of the grades entered by the user
  53.                 // and print the result
  54.  
  55.     avg = (static_cast<double>(sum) / (static_cast<double>(count)));
  56.     //avg = static_cast<double>(sum / count);
  57.     cout << fixed << setprecision(1);
  58.     cout << "The average is: " << avg << endl;
  59.  
  60.  
  61.                 // End of your code
  62.                 ////////////////////////////////
  63.  
  64.     system("pause");
  65.  
  66.     return 0;
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement