I'm trying this new project, like this.

Write a program which will use functions to compute the distance between 2 points on a plane given their coordinates. If one point is located at (x1,y1) and the other is located at (x2,y2) then the formula for computing the distance is

sqrt(sqr(x2-x1) + sqr(y2-y1))

Create your own data file for this exercise the coordinates should be arranged in the text file as follows;

5 -2 3 1
4 4 12 2
0 1 1 0

all parameters and variables will be of type float or double (display to 3 decimal places) the program should use 3 functions
1. getdata -- will read and echo the input
2. calculate -- will compute the distance between the points
3. print -- will print the results of the calculations to the screen

I ahve sorted it out into functions, but I am not sure how to isolate the first 4 points so I can make
x1 = 5
y1 = -2
x2 = 3
y2 = 1

and so on for the remainder lines.
What I have so far is:

// Excercise 2 no.3.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
using namespace std;

void getData(double&);
void doCalc();
void printScreen();
void printClosing();

ifstream infile;

void main()
{	// start main

	double n;			// input file values
	float x1 = 0;		// x coordinate 1
	float x2 = 0;		// x coordinate 2
	float y1 = 0;		// y coordinate 1
	float y2 = 0;		// y coordinate 2

// ***** open stream file *****
	infile.open ("ergo.txt");	// get data from file

	if(infile)
		cout << "Stream ok.\n\n";
	else
		cout << "Stream error.\n\n";

// ***** take input from file *****
	getData(n);

// ***** do calculations from input file *****
	doCalc();

// ***** print results to screen *****
	printScreen();

// ***** print closing message *****
	printClosing();

}	// end main

void getData(double& n)
{	// start getData

	while(infile)
	{	// start while loop
		infile >> n;					// print loop from file.
		cout << "\nnumber =\t" << n;
	}	// end while loop

	cout << endl;

}	// end getData

void doCalc()
{	// start doCalc

}	// end doCalc

void printScreen()
{	// start printScreen

}	// end printScreen

void printClosing()
{	// start printClosing

	int wait;

	cout << "\nEnter any key to terminate program.\n";
	cin >> wait;

}	// end printClosing

The result is:
[IMG]http://i2.photobucket.com/albums/y45/cynixx/console002.jpg[/IMG]

I was given the equation:
sqrt(sqr(x2-x1) + sqr(y2-y1))
but am not sure where to use it because I still have not figures out how to isolate each value from the txt file.

>I am not sure how to isolate the first 4 points so I can

How about changing getdata just a wee bit.

void getData(double& n)
{
   if ( infile >> n )
   {
      cout << "number = " << n << endl;
   }
}

And then in main actually passing the parameters you want to get (once you've made a slight change) to this function.

double x1, x2, y1, y2;		
   // ...
   while ( infile )
   {
      getData(x1);
      getData(x2);
      getData(y1);
      getData(y2);

      doCalc();
   }

I have done what you had suggested and all worked out fine, but it's still just outputting the samething to console, which is good. But I need to assign every fourth digit x1, and so on. Sorry for the fuss, I do appreciate your help :cheesy:

>But I need to assign every fourth digit x1, and so on.

Uh, this is being done in the code I posted. (I should have done better error checking, but I didn't want to confuse you further.)

You weren't expecting us to fill in the blanks for you on the remaining functions, were you? I showed how the doCalc ought to move into this loop, thinking you could pick up on it, fill in some of that code, and take the initiative to put the printScreen function there as well.

Sorry, my bad, I'm pretty clueless sometimes. But yeah, I'm pretty sure I got it now. Thanks for all the help. Much appreciated.

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.