A very common usage of loops is the repetitive reading ofdata . This reading may be from thekeyboard or from an existing data file which contains a long list ofnumbers. In either case we need amethod for signaling the end of the process.A simple way is to use an unnatural value for the data entered, whichwill then be picked up by our loop condition ( or another if statement) . For example if we are entering scores ofexams, it is safe to choose -1 as this unnatural score, the sentinel value,which will terminate the loop. Hencethe user of the program may be instructed to enter the value -1 after all ofthe actual scores have been entered via the keyboard. Or, the sentinel value -1 may be placed at the end of our datafile.
The following is an annotated example for scores entered viathe keyboard. It uses the concept ofsentinel value.
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
Initial comments
/* Loops while( condition ) { statements }
<![if !supportEmptyParas]><![endif]>
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
___________________________________
<![if !supportEmptyParas]><![endif]>
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.
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
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:
<![if !supportEmptyParas]><![endif]>
sum = sum + score or equivalently sum += score
<![if !supportEmptyParas]><![endif]>
As the scores are entered an integer named count is increased
so that the number of entries is registered.
<![if !supportEmptyParas]><![endif]>
Finally we calculate average = sum / count and it is outputted.
*/
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
The fstream.h header can support both file and default I/O
<![if !supportEmptyParas]><![endif]>
The stream report is created
#include<iostream.h> // header which contains the functions
// needed for default input and output
<![if !supportEmptyParas]><![endif]>
#include<fstream.h> // header needed for file input output
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
void main()
{
ofstream report ( "3ex1report.txt", ios :: out);
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
Variables are declared and initialized
<![if !supportEmptyParas]><![endif]>
The first score is entered
The heading of the list is created
float score ; // the test score entered
float sum = 0;
float average; // the average score
<![if !supportEmptyParas]><![endif]>
int count = 0; // counter of scores entered . It starts as zero,
// ends as the total number of scores.
<![if !supportEmptyParas]><![endif]>
cout<<" \n\n Enter the first score (-1 to terminate) " ;
cin >> score;
report << "\n\n This is the list of scores entered \n\n";
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
The while loop. It will run as long as the scores entered are nonnegative.
<![if !supportEmptyParas]><![endif]>
Note that the reading of the next score is done as the last statement
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 !supportEmptyParas]><![endif]>
}
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
The if structure in this part makes sure that the count and average are not outputted to the file if the user terminates the loop at the very start ( avoids division by zero as well).
if ( count > 0 )
{
average = sum / count;
report << " \n\n number of scores entered : "<< count ;
report << ". The average score is "<< average ;
}
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
The results are sent to the report file. We output the Done! Statement to the screen so that the user is informed. We could have given the name of the report file (better ?)
cout << " \n \n Done! " ;
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
The output to the default window and the contents of the report file are illustrated
<![if !supportEmptyParas]><![endif]>
cout << " \n\n Enter e(exit) to exit ...";
char hold;
cin >> hold;
<![if !supportEmptyParas]><![endif]>
}
<![if !supportEmptyParas]><![endif]>
/*
<![if !supportEmptyParas]><![endif]>
THIS IS THE OUTPUT TO THE DEFAULT WINDOW:
<![if !supportEmptyParas]><![endif]>
Enter the first score (-1 to terminate) 97
<![if !supportEmptyParas]><![endif]>
Enter the next score ( -1 terminates) 83
<![if !supportEmptyParas]><![endif]>
Enter the next score ( -1 terminates) 76
<![if !supportEmptyParas]><![endif]>
Enter the next score ( -1 terminates) 95
<![if !supportEmptyParas]><![endif]>
Enter the next score ( -1 terminates) 88
<![if !supportEmptyParas]><![endif]>
Enter the next score ( -1 terminates) 34
<![if !supportEmptyParas]><![endif]>
Enter the next score ( -1 terminates) 56
<![if !supportEmptyParas]><![endif]>
Enter the next score ( -1 terminates) -1
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
Done!
<![if !supportEmptyParas]><![endif]>
Enter e(exit) to exit ...
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
THE CORRESPONDING FILE IS :
<![if !supportEmptyParas]><![endif]>
<![if !supportEmptyParas]><![endif]>
This is the list of scores entered
<![if !supportEmptyParas]><![endif]>
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
<![if !supportEmptyParas]><![endif]>
*/