/********************************************************************************************
02-B.cpp
Jim Millard
for CO211/Fall 1999
Program that finds the largest and smallest value from a set of inputs, then
echos those values to the user.
********************************************************************************************/
#include <iostream>
#include <string>
using namespace std;
void DisplayResults(float smallest, float largest, int count);
void DoWork(float &smallest, float &largest, int &count);
int main()
{
float smallest, largest;
int count = 0;
//inform and prompt the user
cout << endl << "This program will take a set of numbers as input and " << endl
<< "return value of the largest and smallest values in the set." << endl
<< "The program will return the results when you enter [Ctrl-D] as input." << endl;
DoWork(smallest, largest, count);
DisplayResults(smallest, largest, count);
return 0;
}
void DoWork(float& smallest, float& largest, int & count)
{
//Prime the Pump
float current;
cout << "Enter a set of numbers: ";
if (cin >> current) // check for a valid first entry
{
//force-fit the first value as both smallest and largest
count = 1;
largest = current;
smallest = current;
//prompt and extract the rest of the set
cout << "Enter a set of numbers: ";
while (cin >> current)
{
count++; //we've got valid input, so increment the count
//compare to currently held values
if (current > largest)
largest = current;
else if (current < smallest)
smallest = current;
} //while
} //if
return;
}
void DisplayResults(float smallest, float largest, int count)
{
//output the results
cout << endl;
if (count > 1)
{
cout << "\nResults:" << endl;
cout << "\tSmallest: " << smallest << endl;
cout << "\tLargest: " << largest << endl;
}
else if (count == 1)
{
cout << "You only entered one value, " << smallest << ", which by definition is both\n";
cout << "smallest and largest at the same time." << endl;
}
else
cout << "No valid input was received; therefore, there are no results." << endl;
}