Listing 11, SentinelLoop02
/*File: SentinelLoop02.cpp
This C++ program illustrates the use of a do-while loop to
implement a sentinel loop. Typical screen output from this
program follows:
Enter a grade or -1 to quit.
20
Enter a grade or -1 to quit.
40
Enter a grade or -1 to quit.
60
Enter a grade or -1 to quit.
-1
Average = 40
**********************************************************/
#include <iostream>
using namespace std;
class SentinelLoop02{
public:
static void classMain(){
SentinelLoop02* ptrToObject = new SentinelLoop02();
ptrToObject -> doSomething();
}//End classMain function
//-----------------------------------------------------//
//An instance function of the SentinelLoop02 class
void doSomething(){
int count = -1;
double sum = 0;
double temp = 0;
do{
count = count + 1;
sum = sum + temp;
cout << "Enter a grade or -1 to quit." << endl;
cin >> temp;
}while(temp != -1.0);
if(count > 0){
cout << "Average = " << sum/count << endl;
}//end if
}//end doSomething function
};//End SentinelLoop02 class
//-------------------------------------------------------//
int main(){
SentinelLoop02::classMain();
return 0;
}//end main
Listing 11
|