I have been reading various source of information about priming read with while loop when reading multiple sets of date with c++ from an external file.
Although I have successfully been able to implement a code using this concept, I have no clue and did not understand the explanation of my teacher on how it works and why it is necessary.
Tnx for the help
1 Answer
-
Well, a prime read is just a way to control repetition. Basically, a prime read is a data input, before the loop statement, that allows the first actual data value to be entered so it can be checked in the loop statement.
The variable that is inputted by the user and being tested by the expression in the loop is the prime read; the value of the prime read is what you would normally call the sentinel value.
Example:
int NumPeople = 0;
int SumHeights = 0;
int Height;
cin >> Height ;
while (Height != 0 ) {
NumPeople++;
SumHeights = SumHeights + Height;
cin >> Height ;
}
float AvgHeight = SumHeights / NumPeople;
The first cin is a priming read. Note the positioning of the other read. The last height read (sentinel 0) will not be processed like the other data items.