C++ examples for Statement:do while
Adds up grades, computes average, and determines whether you earned an A.
#include <iostream> using namespace std; #include <iomanip> int main()/*from w ww .ja v a 2 s.c om*/ { float total_grade=0.0; float grade_avg = 0.0; float grade; int grade_ctr = 0; do { cout << "What is your grade? (-1 to end) "; cin >> grade; if (grade >= 0.0) { total_grade += grade; // Add to total. grade_ctr ++; } // Add to count. } while (grade >= 0.0); // Quit when -1 entered. Control begins here if no more grades. grade_avg = (total_grade / grade_ctr); // Compute average. cout << "\nYou made a total of " << setprecision(1) << total_grade << " points.\n"; cout << "Your average was " << grade_avg << "\n"; if (total_grade >= 450.0) { cout << "You made an A!!"; } return 0; }