Reputation: 5
My program inputs numbers from a text file only once. I also noticed that last line of given numbers was used in calculations. Is it bad if I'm trying to do that in a while loop? If so, how else could I do that?
The number are:
5
100 1 20
120 1 45
90 1 05
105 1 25
100 1 35
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
double d,a,b,c,g=0;
ifstream file("Text.txt");
if(file.is_open())
{
file >> d;
cout << d << endl;
while(file >> a >> b >> c)
{
cout << left << setw(3) << a << " " << b << " " << c << endl;
g=a/(b*60+c);
ofstream files("Result.txt");
files << "Speed: " << g << " m/s" << endl;
}
}
return 0;
}
Upvotes: 0
Views: 1048
Reputation: 92381
There is a problem with this line inside the loop
ofstream files("Result.txt");
It creates a new file each time you read from the input. So it will only contain the output for the last input.
If you move that line to just before the while
, you will get all your output in the same file.
Upvotes: 1