Im writing a program to calulate the distance between two points unsing structures and functions. I have everthing working perfectly except that I cant get it to repeat for a second set of points. My input file has 10 points in it bit it stops after it reads the first two, can somebody please help me with this? here is what i have:

#include <iostream>
#include <fstream>
#include <cmath>
using namespace std;
struct point
{
	double X1, Y1, X2, Y2;
};
double distance( double X1, double Y1, double X2, double Y2 )
{
	double d, t, u;
	t = pow((X2-X1),2);
	u = pow((Y2-Y1),2);
	d = sqrt(t+u);
	return(d);
}
void main(void)
{
	point x;
	double d;
	ifstream inf("F://value.txt");			
	inf>>x.X1;			
	inf>>x.Y1;
	inf>>x.X2;			
	inf>>x.Y2;
	d = distance ( x.X1, x.Y1, x.X2, x.Y2 );
	ofstream outf("F://distance.txt");
	outf<<d;
}

thanks in advance for the help!

Recommended Answers

All 6 Replies

Something like

ifstream inf("F://value.txt");
ofstream outf("F://distance.txt"); 
while ( inf>>x.X1>> x.Y1 >> x.X2 >> x.Y2 ) {
    d = distance ( x.X1, x.Y1, x.X2, x.Y2 ); 
    outf<<d; 
}

Something like

ifstream inf("F://value.txt");
ofstream outf("F://distance.txt"); 
while ( inf>>x.X1>> x.Y1 >> x.X2 >> x.Y2 ) {
    d = distance ( x.X1, x.Y1, x.X2, x.Y2 ); 
    outf<<d; 
}

When i put that into the program i think it is reading the values from the file differently because it is giving me the wrong answers now?
Is there any other option i could try to make it repeat itself? thanks

Well you said 10 points in your input file, which doesn't make a lot of sense since they seem to be read 4 at a time.

Are you suggesting that you need to calculate P1->P2 then P2->P3 then P3->P4 etc?

yes here is what my input file looks like:
0.0 0.0 2.0 1.8
1.0 -3.0 2.8 3.5
2.2 4.4 1.1 6.8
1.0 -6.0 7.0 7.2
5.4 2.4 -9.0 -4.6
with each line reading X1, Y1, X2, Y2 giving 2 points per line.

OK, which is what I originally posted would cope with.
So how are the results different from what you expect?

For some reason when it is reading the input it will read the first four numbers but will then skip one number snd proceed. So it will read 0.0, 0.0, 2.0 and 1.8 but will skip 1.0 and start the next four numbers at .3.0? for some reason i cant figure so thanks for helping me.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.