Hey guys! I can't figure out where or what to initialize min and max to in order for this program to work with any text document. Help would be much appreciated. Thank you so much for your time.
#include <iostream>
#include <fstream>
#include <iomanip>
usingnamespace std;
int main()
{
char choice;
int reader , max, min, numberValues = 0, average = 0;
double sumValues = 0;
ifstream inputFile("random.txt");
if (inputFile)
{
do
{
cout << "Make a slection from the list\n"
<< "A. Get the largest value\n"
<< "B. Get the smallest value\n"
<< "C. Get the sum of the values\n"
<< "D. Get the average\n"
<< "E. Get the number of values entered\n"
<< "F. End this program\n\n"
<< "Enter your choice --> ";
cin >> choice;
while (inputFile >> reader)
{
if (max < reader)
max = reader;
if (min > reader)
min = reader;
numberValues++;
sumValues += reader;
}
if (numberValues > 0)
{
average = sumValues / numberValues;
}
switch (choice)
{
case'A':
case'a': cout << "\nThe largest value is " << max << endl << endl;
break;
case'B':
case'b': cout << "\nThe smallest value is " << min << endl << endl;
break;
case'C':
case'c': cout << "\nThe sum of the values entered is " << sumValues << endl << endl;
break;
case'D':
case'd': cout << "\nThe average of the value entered is " << average << endl << endl;
break;
case'E':
case'e': cout << "\nThe number of values entered is " << numberValues << endl << endl;
case'F':
case'f':
{
cout << "\nProgram ending\n\n" << "Press Enter to end --> ";
cin.ignore();
cin.ignore();
return 0;
}
default: cout << endl << choice << " was not a valid option" << endl << endl;
break;
}
} while (choice != 'F' && choice != 'f');
}
else
{
cout << "The program could not open the file\n";
}
return 0;
}
int min; // Set to BIGGEST possible value.
int max; // Set to SMALLEST possible value.
There seems to be a logic error here. These lines need to be moved outside the do-while loop.
1 2 3 4 5 6 7 8 9 10 11 12 13 14
while (inputFile >> reader)
{
if (max < reader)
max = reader;
if (min > reader)
min = reader;
numberValues++;
sumValues += reader;
}
if (numberValues > 0)
{
average = sumValues / numberValues;
}