-
Double output!
Ok...can anyone tell me why the while loop outputs the last bit of data twice?? if you can...that'd be cool...thanx
here's the code:
Code:
#include<fstream.h>
#include<iomanip.h>
void main (void)
{
ifstream inputfile;
ofstream outputfile;
int Max, Min;
float Avg, Num1, Num2, Num3, Num4, Num5, Num6, Num7;
float Total;
char Data[8];
inputfile.open("C:\\Hell\\Input.dat", ios::in);
outputfile.open("C:\\Hell\\Output.dat", ios::out);
if (!inputfile)
cout << "Your file is bad or does not exist. Please try again later.";
inputfile >> Data;
while (!inputfile.eof())
{
Max = 0;
Min = 1000;
inputfile >> Num1 >> Num2 >> Num3 >> Num4 >> Num5 >> Num6 >> Num7;
if (Num1 > Max)
Max = Num1;
if (Num2 > Max)
Max = Num2;
if (Num3 > Max)
Max = Num3;
if (Num4 > Max)
Max = Num4;
if (Num5 > Max)
Max = Num5;
if (Num6 > Max)
Max = Num6;
if (Num7 > Max)
Max = Num7;
if (Num1 < Min)
Min = Num1;
if (Num2 < Min)
Min = Num2;
if (Num3 < Min)
Min = Num3;
if (Num4 < Min)
Min = Num4;
if (Num5 < Min)
Min = Num5;
if (Num6 < Min)
Min = Num6;
if (Num7 < Min)
Min = Num7;
Total = (Num1 + Num2 + Num3 + Num4 + Num5 + Num6 + Num7);
Avg = (Total / 7);
outputfile << Num1 << " " << Num2 << " " << Num3 << " " << Num4 << " " << Num5 << " " << Num6 << " " << Num7 << " " << endl;
outputfile << "The biggest number is: " << Max << endl;
outputfile << "The smallest number is: " << Min << endl;
outputfile << "The total of those numbers is: " << setiosflags(ios::fixed) << Total << endl;
outputfile << "The average of those numbers is: " << setprecision(2) << Avg << endl;
outputfile << endl;
}
inputfile.close();
outputfile.close();
}
If you can tell me why it's being weird...that'd be cool...oh yeah...here's the data i'm using...lol
Data
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
the output goes ok...but...it does this...
The biggest number is: 834
The smallest number is: 64
The total of those numbers is: 2396
The average of those numbers is: 342.29
151 64 98 813 67 834 369
The biggest number is: 834
The smallest number is: 64
The total of those numbers is: 2396
The average of those numbers is: 342.29
WHY OH WHY DOES THE LAST ONE COME OUT TWICE! ACK!
Thanx again :-)
-
eof isn't set until the end of file is reached. Your code -
1)Test if eof has been set
2)Read from file
3)Set eof if end of file reached (implicit)
4)Loop
Therefore, before eof is set you've already read an entry.
file a:
a
first iteration -
1)test if eof has been set
No
2)Read from file
'a'
3)Set eof if end of file reached (implicit)
No 'a' was read.
4)Loop -
second iteration
1)test if eof has been set
No
2)Read from file
eof is reached (but not set yet), but file read anyway
'a'
3)Set eof if end of file reached (implicit)
Yes
4)Loop -
stop.
You have to test for eof after an attempted read not before one.
-
Code:
#include <fstream>
#include <iostream>
#include <iomanip>
int main(int argc, char *argv[])
{
using namespace std;
ifstream inputfile("C:\\Hell\\Input.dat");
ofstream outputfile("C:\\Hell\\Output.dat");
int Max, Min;
float Avg, Total = 0;
float numbers[7];
char Data[8];
if (inputfile.fail())
{
cout << "Your file is bad or does not exist. Please try again later.";
return 0;
}
inputfile >> Data;
while (!inputfile.eof())
{
Max = 0;
Min = 1000;
for (int i = 0; i < 7; i++)
{
inputfile >> numbers[i];
if (inputfile.eof())
break;
if (numbers[i] > Max)
Max = numbers[i];
else if (numbers[i] < Min)
Min = numbers[i];
Total += numbers[i];
}
Avg = (Total / i+1);
for (i = 0; i < 7; i++)
outputfile << numbers[i] << " ";
outputfile << endl;
outputfile << "The biggest number is: " << Max << endl;
outputfile << "The smallest number is: " << Min << endl;
outputfile << "The total of those numbers is: " << setiosflags(ios::fixed) << Total << endl;
outputfile << "The average of those numbers is: " << setprecision(2) << Avg << endl;
outputfile << endl;
}
inputfile.close();
outputfile.close();
return 0;
}
Try that..
-
Thanx :-)
Thanx a lot guys...my teacher wouldn't help me and i don't think that i would've gotten that...i understand now why you have to set the eof at that point...i've been programming c++ like 2 months now...so i'm very new...but anyways...peace