files and streams issue

hey there!
I'm trying to do an assignment in files and stream
I have a file with data looks like:

1
2
3
4
5
6
    x     y
A   1.2   3.6
B   1.8   2.9
C   3.6   5.7
D   9.9   7.3
E   9.2   3.2


Is my code right? cuz i don't get a real results.

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
 #include <iostream>
#include <cmath>
#include <fstream>

using namespace std;

/* Main Function: */
int main() {

	float x0, x1, x2, x3, x4;
	float y0, y1, y2, y3, y4;
	char character;
	ifstream in;
	ofstream out;

	/* CALLING THE DATA FILE NAMED (week4Q5_input) */
	in.open("week4Q5_input");
	if (in.fail()) {
		cout << "Sorry, but the file couldn't be opened.\n\n";
		exit(1);
	}
	/* CREATE FILE TO STORE OPTAINED DATA */
	out.open("week4Q5_output");

	in >> character >> x0 >> y0;
	in >> character >> x1 >> y1;
	in >> character >> x2 >> y2;
	in >> character >> x3 >> y3;
	in >> character >> x4 >> y4;

	out << "The distance between A and B is '" << " " << sqrt(pow(x1-x0,2) + pow(y1-y0,2)) << "'." << endl;
	cout << "The distance between A and B is '" << " " << sqrt(pow(x1 - x0, 2) + pow(y1 - y0, 2)) << "'." << endl;

	out << "The average distance is '" << " " <<  "" << "'." << endl;
	cout << "The average distance is '" << " " << "" << "'." << endl;

	out << "The maximum distance is '" << " " << "" << "'." << endl;
	cout << "The maximum distance is '" << " " << "" << "'." << endl;

	out.close();
	in.close();

	system("PAUSE");
	return 0;

}


Thank you :) <3
Last edited on
Looks like your code, from the start, tries to read

character, float, float

but the first three entries in the file are

character, character, character
I fixed it thank you, it should be like that:
1
2
char x, y;    // new characters referred as x,y
in >> x >> y;


and i should add another
Topic archived. No new replies allowed.