Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
Sentinel-controlled while loop example

% cat sentinel.cpp
// while loop with sentinel to terminate iterations
// program gives the total of the scores entered

#include <iostream>

using namespace std;

int main()
{
  int HIGHSCORE = 100;
  double score = 0, total= 0;

  cout << "\nTo stop entering scores, type in any number";
  cout << "\n greater than 100.\n\n";

  while (score <= HIGHSCORE)
  {
    total = total + score;
    cout << "Enter a score: ";
    cin  >> score;
  }

  cout << "\nThe total of the scores is " << total << endl;

  return 0;
}
% g++ sentinel.cpp
% a.out

To stop entering scores, type in any number
 greater than 100.

Enter a score: 88
Enter a score: 92
Enter a score: 77
Enter a score: 46
Enter a score: 999

The total of the scores is 303


 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming