Below is my input. Everything works how I want it to, except the last number (369), gets repeated on the last sequence, as shown below. Would appreciate if someone could steer me in the right direction...Thanks.
Also, just to add, in the future, I will no doubt employ stringstream, as well as arrays (when needed) when processing lines. It's just that I need to not use them for just this time.
346 130 982 90 656 117 595
Total is 2916.
Average is 416.
Lowest number is 90
Largest number is 982
415 948 126 4 558 571 87
Total is 2709.
Average is 387.
Lowest number is 4
Largest number is 948
42 360 412 721 463 47 119
Total is 2164.
Average is 309.
Lowest number is 42
Largest number is 721
441 190 985 214 509 2 571
Total is 2912.
Average is 416.
Lowest number is 2
Largest number is 985
77 81 681 651 995 93 74
Total is 2652.
Average is 378.
Lowest number is 74
Largest number is 995
310 9 995 561 92 14 288
Total is 2269.
Average is 324.
Lowest number is 9
Largest number is 995
466 664 892 8 766 34 639
Total is 3469.
Average is 495.
Lowest number is 8
Largest number is 892
151 64 98 813 67 834 369
Total is 2396.
Average is 342.
Lowest number is 64
Largest number is 834
369 369 369 369 369 369 369
Total is 2583.
Average is 369.
Lowest number is 369
Largest number is 369
#include <iostream>
#include <fstream>
usingnamespace std;
constint NumPerLine=7;
int main()
{
int num = 0;
int total = 0;
float average = 0;
int min = 0;
int max = 0;
ifstream fileIn;
fileIn.open("File2.txt");
ofstream fileOut("Output.txt");
//Ensure that the file opens. If it does not, display error message.
if (!fileIn) {
cout << "\nError opening file...Closing program. \n";
exit(1);
}
else {
while (fileIn) {
cout << endl;
int total = 0;
if(fileIn){
for (int k = 0; k < NumPerLine; k++) {
fileIn >> num;
if (k == 0)
{
max = num;
min = num;
}
total += num;
fileOut << num << " ";
if (num>max)
max=num;
if (num<min)
min=num;
}
average = total / NumPerLine;
fileOut << "\n\nTotal is " << total << "." << endl;
fileOut << "Average is " << average << "." << endl;
fileOut << "Lowest number is " << min << endl;
fileOut << "Largest number is " << max << endl << endl;
}
}
fileIn.close();
fileOut.close();
cout << "\nData has been processed, and copied to the file, \"Output.txt\"." << endl << endl;
}
return 0;
}
Also, for what it's worth, on line 37, if I do something like, "while(fileIn >> num),"
I don't get the extraneous code, but my output skips the first number.
The problem here, as is so often the case, is that the status of the file is checked before reading from it, rather than afterwards. Thus the check if(fileIn){ at line 37 is pretty much useless, in part because the while condition at line 31 has already ensured that condition is true.
What you need to do is to check each time an attempt is made to read a number from the, whether or not it succeeded.
You might for example do something like this, though I'm pretty sure there are neater ways, this is more of a quick fix:
int total = 0;
int count = 0;
for (int k = 0; k < NumPerLine; k++)
{
fileIn >> num;
if (!fileIn) // was the number read from the file successfully?
break; // no - then exit from the for loop
count++;
if (k == 0)
{
max = num;
min = num;
}
total += num;
fileOut << num << " ";
if (num > max)
max = num;
if (num < min)
min = num;
}
if (count > 0)
{
average = total / count;
fileOut << "\n\nTotal is " << total << "." << endl;
fileOut << "Average is " << average << "." << endl;
fileOut << "Lowest number is " << min << endl;
fileOut << "Largest number is " << max << endl << endl;
}
Thanks, man. I used your code and still got 369, although I only got it one time (as shown below). You were definitely right about line 37. I figured out as you did, that it is useless in this code.
369
Total is 369.
Average is 52.
Lowest number is 369
Largest number is 369