Problem with I/O file

This is what I did for the input file

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 #include <fstream>
#include <iostream>
using namespace std;

int main( )
{
float distance1 = 2.5 ;
float distance2 = 4.3 ;
float distance3 = 6.2 ;
float distance4 = 8.1 ;
float scale = 0.0001 ;
ofstream outData;

outData.open("E:\\walk.dat");

outData << distance1 << distance2 << distance3 << distance4 << scale;

outData.close( );
system("pause");

return 0;
}


I don't know why the walk.dat was read like this and the result was like this, pls help me ):

For a measurement of 2.54 the first distance is 0 mile(s) long
For a measurement of 0.36 the second distance is 0 mile(s) long
For a measurement of 0.28 the third distance is 0 mile(s) long
For a measurement of 0.1 the fourth distance is 0 mile(s) long
Total mileage for the day is 0 miles

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
53
#include <iostream>
#include <iomanip> 
#include <fstream> 
using namespace std;
int main( )
{
float distance1; 
float distance2; 
float distance3; 
float distance4; 
float scale; 
float totMiles; 
float miles; 
ifstream inData; 
ofstream outData; 
outData<<fixed; 
outData<< showpoint; 
outData << setprecision(1);

inData.open("E:\\walk.dat");
outData.open("E:\\results.dat");

inData >> distance1 >> distance2 >> distance3 >> distance4>> scale;

totMiles = 0.0;

miles = float(int(distance1 * scale * 10.0 + 0.5)) / 10.0;
cout << "For a measurement of " << distance1
<< " the first distance is " << miles
<< " mile(s) long." << endl;
totMiles = totMiles + miles;
miles = float (int(distance2 * scale * 10.0 + 0.5)) / 10.0;
cout << "For a measurement of " << distance2
<< " the second distance is " << miles
<< " mile(s) long." << endl;
totMiles = totMiles + miles;
miles = float(int(distance3 * scale * 10.0 + 0.5)) / 10.0;
cout << "For a measurement of " << distance3
<< " the third distance is " << miles
<< " mile(s) long." << endl;
totMiles = totMiles + miles;
miles = float(int(distance4 * scale * 10.0 + 0.5)) / 10.0;
cout << "For a measurement of " << distance4
<< " the fourth distance is " << miles
<< " mile(s) long." << endl;
totMiles = totMiles + miles;

outData << endl;
cout << "Total mileage for the day is " << totMiles
<< " miles." << endl;
system("pause");
return 0;
}
Last edited on
When you create the output file, all the values are squashed up together, with nothing to separate them.
 
    outData << distance1 << distance2 << distance3 << distance4 << scale;


You could put a space, or tab, or a newline character after each value. That way, the program which reads the file will know where one number ends and the next one begins.
thank you so much
Topic archived. No new replies allowed.