Distance formula for CS1 class

Hi, I'm just starting out programming and I have to create a program that measures distance between 2 points on a 2d grid. I think i'm pretty close but something isn't working the way it's supposed too. It doesn't print properly was wondering if anyone could help me out? I could also use help figuring out how to loop the steps in my main function, loops are e.c. we haven't gone over them at all. Thanks for any help you can provide.

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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
/*
Program prompts user to enter two points in the form (x1, y1) and (x2, y2) and finds the distance between the two points using a function.
Algorithm steps:
	1. Define a function called findDistance (...) that takes four parameters x1, y1 and x2, y2 as two points.
		a. finds the distance between them using the equation: sqrt//FIX ME sqrt symbol ((x2-x1)^2 + (y2-y1)^2)
		b. returns the calculated distance value
	2. Prompt user to enter four numbers
	3. Convert the values into float and store them into variables
	4. Call function getDistance by passing 4 entered numbers as arguments
	5. Display results with proper description.  Format output numbers to 2 decimal points.
	6. Test and validate that program output is correct for a given set of input points.
	7. Bonus - (10 points) Using a loop repeat step 2-6 until the user wants to quit.
*/


#include <iostream>
#include <cstdio>
#include <cassert>
#include <cmath>
using namespace std;

const double epsilon = 1e-6; // 0.000001 accuracy upto 6 decimal points

// function prototypes
// function that calculates the distance between two points
// x1, y1 and x2, y2 and returns the calculated value double findDistance (int, int, int, int);

// test function that runs automated testing void test();

double findDistance(int x1, int y1, int x2, int y2)
{
	cout << sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) << endl;
	//FIXME - Find the distance between (x1, y1) and (x2, y2)
	// following the algorithm step 1
	// return the calculated distance
	return 0.000000;
}

void test()
{
	assert(findDistance(4, 3, 5, 1) - 2.23607 <= epsilon);
	assert(findDistance(3, 5, 7, 0) - 6.40312 <= epsilon);
	assert(findDistance(-1, 2.03, 4, 18) - 16.73442 < epsilon);
	// FIXME - add atleast two more test cases
	cout << "all tests passed..." << endl;
}

	int main()

{
	
	int x1, y1, x2, y2; // variables to store two points (x1, y1) and (x2, y2)
	char ch;

	//FIX ME - 10 bonus points - add loop until the user wants to quit
	system("cls");
	cout << "Program calculates distance between 2 points on a 2d Coordinates." << endl;
	cout << "Enter a point in the form (x, y) : ";
	// parse the input stream
	cin >> ch >> x1 >> ch >> y1 >> ch; // value stored in ch is ignored
	printf(" (x1, y1) = (%d, %d)\n", x1, y1);

	cout << "Enter a second point in the form (x, y): ";
	cin >> ch >> x2 >> ch >> y2 >> ch; // value store in ch is ignored
	printf(" (x2, y2) = (%d, %d)\n", x2, y2);
	//FIXME - Read/parse the second point and store data into variables x2 and y2 //fixed
	test();
	//FIXME - Call test function // fixed
	findDistance(x1, y1, x2, y2);
	//FIXME - call findDistance function passing proper arguments //
	printf("The distance between the 2 points =  ", findDistance);
	//FIXME - using printf function display the returned distance with proper description

	cin.ignore(1000, '\n');
	cout << "Good Bye!" << endl;
	cin.get();
	return 0;
}
cout << sqrt((x2 - x1) ^ 2 + (y2 - y1) ^ 2) << endl;
What do you think does the ^ operator ?
raises (x2 - x1) and (y2 - y1) to the 2nd power? is that not correct?
No it doesn't.
^ is a bitwise operator(XOR)
so would sqrt(pow(x2 - x1, 2) + (y2 - y1, 2)) be correct?
Topic archived. No new replies allowed.