|
|
|
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
|
|