Pretty straightforward question here. I'm used to setting an upper and lower value for variables, and then seeing if a bunch of numbers are lower than that (like I did below). I want to know how I could do the same thing, but by assigning the min and max to the first value of the row. When I was playing around with it, I did something like min = num, and then seeing if min was greater than or less than num, but it ended up being 5 > 5, 55 > 55, etc. and I wasn't going anywhere.
My code definitely isn't perfect - For some reason, I get the output that I want for all of the rows of data that I need (I have 8 rows of numbers, with each of them having 7 numbers in that line) but I also get two additional outputs where I get 7 zeros, average = 0, etc.
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
int main()
{
int num = 0;
int count = 0;
int total = 0;
int average = 0;
ifstream fileIn;
fileIn.open("File2.txt");
//ofstream fileOut("Lab5A_Output.txt");
if (!fileIn) {
cout << "\nError opening file...Closing program. \n";
fileIn.close();
}
else {
while (fileIn >> num) {
for (int i = 0; i < '\n'; i++) {
cout << endl;
int num = 0;
int count = 0;
int total = 0;
int average = 0;
int min = 1000;
int max = 0;
for (int k = 0; k < 7; k++) {
fileIn >> num;
total += num;
cout << num << " ";
count++;
if (num < min)
min = num;
if (num > max)
max = num;
}
average = total / count;
cout << "\n\nTotal is " << total << "." << endl;
cout << "Total amount of numbers is " << count << "." << endl;
cout << "Average is " << average << "." << endl;
cout << "Lowest number is " << min << endl;
cout << "Largest number is " << max << endl;
}
fileIn.close();
}
}
//fileOut.close();
return 0;
}