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