So I understand how this program is suppose to work; however, the commented part of "Notes" isn't clear for me. Under "test if new entry is now the largest," how do I record and remember the largest number entered by the user, and make it show the largest entered by the end?
Demonstrate how to use an event-controlled loop to read
input and find (remember) the largest value entered and
number of values that were entered.
NOTE: This program uses a constant defined in C++ called
INT_MIN. This constant represents the smallest integer
that C++ can handle. When we initialize the variable for
the largest value to this it means any int read after that
will be larger (by definition). Think about why this is
necessary. What would happen if we initialize largest to 0
and then the user enters a negative number. The 0 would be
bigger, but the user never really entered a 0...
*/
#include<iostream>
usingnamespace std;
int main()
{
char goAgain; // variable to control event loop
int latestInput; // input values
int largestSoFar = INT_MIN; // variable to remember largest value
int count = 0; // variable to count loop iterations
cout << "The program will remember the largest integer value entered.\n";
// LOOP CONTROL: initialize loop event
cout << "Would you like to begin (y or n): ";
cin >> goAgain;
cout << endl;
// TODO, LOOP CONTROL: remove comment symbols on the while
// line below and code the text expression between ( and ).
while((goAgain == 'Y') || (goAgain == 'y'))
{
// TODO, LOOP PROCESS: prompt the user and get an input value
cout << "\nEnter a whole number: ";
cin >> latestInput;
// TODO, LOOP PROCESS: test if new entry is now the largest seen
// TODO, LOOP PROCESS: update iteration counter
count++;
// TODO, LOOP CONTROL: ask the user if they want to enter another and
// read a new value into the loop control variable
cout << "Enter another (Y or N): ";
cin >> goAgain;
} // end of loop body
// LOOP EXIT: display status of loop count and largest value
cout << "You entered " << count << " values." << endl;
if (count > 0)
cout << "The largest value entered was " << largestSoFar << endl;
cout << "\nEnd Program - ";
return 0;
}
/* Sample Interaction and Output
Test #1:
=======================================================
The program will remember the largest integer value entered.
Would you like to begin (y or n): n
You entered 0 values.
End Program - Press any key to continue . . .
Test #2:
=======================================================
The program will remember the largest integer value entered.
Would you like to begin (y or n): Y
Enter a whole number: -19
Enter another (Y or N): y
Enter a whole number: 99
Enter another (Y or N): Y
Enter a whole number: 7
Enter another (Y or N): n
You entered 3 values.
The largest value entered was 99
*/
The variable largestSoFar is meant to contain the largest value you've seen so far. So each time the user enters a value, compare it to largestSoFar. If the value entered is bigger than largestSoFar, then assign that value to largestSoFar.