Processing text files sequentially
Jul 14, 2016 at 8:18pm UTC
I am a beginner computer science student this summer and i have been having issues understanding what i am doing wrong. I wrote this to process the text in a file which i included in the resources file but I cannot seem to load my text file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main()
{
ifstream infile;
ofstream outfile;
string name;
double x = 0, highest = 0, lowest = 0, total = 0, average = 0;
infile.open("data.txt" );
if (!infile)
{
cout << "Cannot open file, Terminating program." << endl;
exit(1);
}
outfile.open("out.txt" );
while (infile)
{
total = 0, highest = 0, lowest = 0, average = 0;
for (int i = 0; i < 7; i++)
{
infile >> x;
total += x;
if (i == 0 || highest < x)
highest = x;
if (i == 0 || lowest > x)
lowest = x;
cout << x << endl;
}
average = total / 7;
cout << total << " \t is the total" << endl;
cout << average << "\t is the average" << endl;
cout << highest << " \t is the highest number" << endl;
cout << lowest << "\t is the lowest number" << endl;
system("pause" );
}
infile.close();
outfile.close();
return 0;
}
data file is:
1 2 3 4 5 6 7 8
346 130 982 90 656 117 595
415 948 126 4 558 571 87
42 360 412 721 463 47 119
441 190 985 214 509 2 571
77 81 681 651 995 93 74
310 9 995 561 92 14 288
466 664 892 8 766 34 639
151 64 98 813 67 834 369
Jul 14, 2016 at 9:20pm UTC
Your executable and the data file must be in the same directory.
Jul 15, 2016 at 8:06am UTC
Last edited on Jul 15, 2016 at 8:07am UTC
Topic archived. No new replies allowed.