Working on a project where I need to fill a file with numbers and use a getline to read those numbers line by line and then display the total, average, max, and min from each line. Everything's in place except my getline doesn't seem to be working as the output for the total, average, etc, is always 0. Any help at all is appreciated. Thanks!
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main() {
ofstream myfile;
myfile.open("file.txt");
if(!myfile)
{
cout << "Unable to open file\n";
return 0;
}
if(myfile.is_open())
{
myfile << "Numbers: \n";
myfile << "90 63 84 52 21 93 77 46\n";
myfile << "90 22 26 34 39 44 75 98\n";
myfile << "28 28 85 57 28 33 66 100\n";
myfile << "16 80 74 62 42 84 42 56\n";
myfile << "85 44 76 97 16 64 80 14\n";
myfile << "41 85 13 88 78 8 18 38\n";
myfile << "53 49 71 79 75 57 93 62\n";
fstream infile;
infile.open("file.txt");
int total, average, max=0, min=0, num;
while(!infile.eof())
{
string line;
getline(infile, line);
int a, b, c, d, e, f, g, h, i;
infile >> a >> b >> c >> d >> e >> f >> g >> h >> i;
total = a+b+c+d+e+f+g+h+i;
average = total/7;
while(infile>>num)
{
max = num;
min = num;
if(max<num)
{
max = num;
}
if(min > num)
{
min = num;
}
}
myfile << " TOTAL AVERAGE MAX MIN\n";
myfile << "90 63 84 52 21 93 77 46 " << total << " " << average << " " << max << " " << min << endl;
myfile << "90 22 26 34 39 44 75 98\n";
myfile << "28 28 85 57 28 33 66 100\n";
myfile << "16 80 74 62 42 84 42 56\n";
myfile << "85 44 76 97 16 64 80 14\n";
myfile << "41 85 13 88 78 8 18 38\n";
myfile << "53 49 71 79 75 57 93 62\n";
}
infile.close();
}
myfile.close();
return 0;
}