Do...While Demo Fragments

// To print the message HAPPY HOLIDAYS 10 times;
const char BEEP = '\a'                   // remember that '\a' sounds the beep
int counter = 0;

do
{
     cout<< "HAPPY HOLIDAYS! \n" << BEEP;
     counter++;
}
while (counter <10);



// To add a total, count, and average grades:
double total = 0;                 // be sure to initialize variables to zero to avoid ...
int counter = 0;           // ... allowing the computer to use it's "garbage".
double average = 0;
int grade;                   // Why is this variable not set equal to zero? It is filled by user.

do
{
     cout<< "Enter the grade scored. \n";
     cout<< "Enter -999 to quit. \n";
     cin>> grade;

     if (grade != -999)                // separating sections of the program 
     {                                            // by inserting an empty line helps organize
             total += grade;           // your thinking and allows for easy reading.
             counter++;
      }

}
while (grade != -999);

average = total / counter;

cout<< "\n\n The average of your " << counter << " grades is " << average << ".\n";
 

Return to Topic Menu | Computer Science Main Page | MathBits.com | Terms of Use