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.