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
End Program - Press any key to continue . . .
*/
Sorry, I'm having trouble to how to test if new entry is the current largest. Everything is set up however, I don't seem to understand on using the "largestSoFar" variable to keep track of the largest value entered and to remember it.