Sentinel Loops:

A very common usage of loops is the repetitive reading of data. This reading may be from the keyboard or from an existing data file which contains a long list of numbers.  In either case we need a method for signaling the end of the process.  A simple way is to use an unnatural value for the data entered, which will then be picked up by our loop condition ( or another if statement) .  For example if we are entering scores of exams, it is safe to choose -1 as this unnatural score, the sentinel value, which will terminate the loop.  Hence the user of the program may be instructed to enter the value -1 after all of the actual scores have been entered via the keyboard.  Or, the sentinel value -1 may be placed at the end of our data file. 
 
Notes:
 
1. Structure your program in a way that the sentinel value is NOT processed as regular data. For example the do…..while(  cond. )  loop should not be used since it has to run at least one time. Another example is that the reading statement should be placed at the end of the while loop statements.
 
2. In the case of reading from a data file, an alternative approach is to let the program read to the end of the file.  We will discuss this later under input failure.
 
3. An alternative way of terminating data entry is to specify the number of data to be entered (if known ahead of time ) and structure the program as a counting loop.  We will discuss this later as well.

 
The following is an annotated example for scores entered via the keyboard.  It uses the concept of sentinel value.

Place your mouse in the areas below to see comments about the corresponding sections of code  
Code
 
/* Loops   while( condition ) {  statements   }
  file: 3ex1.cpp  (or used as 3ex1.appended)
FALL 1998
___________________________________
Jacob Y. Kazakia   jyk0
September 12, 1998
Programming example  1 of unit #3
Recitation Instructor: J.Y.Kazakia
Recitation Section  01 ___________________________________
  Purpose: This program reads a sequence of  test scores a, b, c, etc... and calculates the average of them.  The sequence is terminated when -1 is inserted as a test score. The scores entered as well as the average are outputted to a file called 3ex1report.txt with an appropriate heading.
 
 
Algorithm: The score entered is stored in the variable float score. Then the variable sum (which is float and initialized at the beginning to zero) is increased by the score:
 
           sum = sum + score    or equivalently   sum += score
 
           As the scores are entered an integer named count is increased so that the number of entries is registered.
 
           Finally we calculate: average = sum / count   and it is outputted.
                                        */
 
#include<iostream.h>   // header which contains the functions
                       // needed for default input and output
 
#include<fstream.h>    // header needed for file input output
 
 
void main()
{
ofstream report ( "3ex1report.txt", ios :: out);
 
float score ;   // the test score entered
float sum = 0;
float average;  // the average score
 
int count = 0;  // counter of scores entered . It starts as zero,
                // ends as the total number of scores.
 
cout<<" \n\n  Enter the first score  (-1 to terminate)   " ;
cin >> score;
 
report << "\n\n This is the list of scores entered \n\n";
 
while ( score >= 0)             // read the scores and add them up
  {
   count = count + 1;
   report << " score # " << count << "  is  " << score << endl;
   sum = sum + score;
   cout << "  \n Enter  the next score ( -1 terminates)   " ;
   cin >> score;
 
   }
 
if ( count > 0 )
   {
     average = sum / count;
     report << " \n\n number of scores entered : "<< count ;
     report << ". The average score is  "<< average ;
   }
 
cout << " \n \n  Done! " ;
 
cout << " \n\n  Enter e(exit) to exit    ...";
char hold;
cin >> hold;
 
}
 
/*
 
    THIS IS THE OUTPUT TO THE DEFAULT WINDOW:
 
 Enter the first score  (-1 to terminate)   97
 
 Enter  the next score ( -1 terminates)   83
 
 Enter  the next score ( -1 terminates)   76
 
 Enter  the next score ( -1 terminates)   95
 
 Enter  the next score ( -1 terminates)   88
 
 Enter  the next score ( -1 terminates)   34
 
 Enter  the next score ( -1 terminates)   56
 
 Enter  the next score ( -1 terminates)   -1
 
 
  Done!
 
  Enter e(exit) to exit    ...
 
 
  THE CORRESPONDING FILE IS :
 
     
 
  This is the list of scores entered
 
 score # 1  is  97
 score # 2  is  83
 score # 3  is  76
 score # 4  is  95
 score # 5  is  88
 score # 6  is  34
 score # 7  is  56
 
 
 number of scores entered : 7. The average score is  75.5714
 
 */

(text file of the code)

(printable file)

(another example)

© 2001 J.Y. Kazakia. All rights reserved