function brings up random letters and numbers?

I am writing a program to compute the distance formula. It runs smoothly until the solution, that's when it brings up a string of random numbers and letters. I don't see a problem with the code, and my compiler (visual 2012) doesn't bring up any warnings.

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
#include <iostream>
#include <math.h>
using namespace std;

float num1 = 0;
float num2 = 0;
float num3 = 0;
float num4 = 0;

int distance_formula()
{
	float first_pair = (num4 - num2);
	float second_pair = (num3 - num1);
	float firstpairsolution = (first_pair * first_pair);
	float secondpairsolution = (second_pair * second_pair);
	float solution = (firstpairsolution + secondpairsolution);
	float overall_solution = sqrt(solution);
	cout << overall_solution << endl;
	return 0;
}


int main()
{

	cout << "Enter two numbers: " << endl;
	cin >> num1;
	cin >> num2;

	cout << "Enter two more numbers: " << endl;
	cin >> num3;
	cin >> num4;

	cout << "The distance between these two pairs of numbers are: " << distance_formula << endl;

	return 0;
}
Last edited on
Enter two numbers:
3
2
Enter two more numbers:
6
4
The distance between these two pairs of numbers are: 0031E10D2
Press any key to continue . . .
Last edited on
cout << "The distance between these two pairs of numbers are: " << distance_formula << endl;

Put in the parentheses to call the function.
cout << "The distance between these two pairs of numbers are (is?): " << distance_formula() << endl;

Instead of using global variables, you can declare the variables local to main and just pass their values to the function.
Two (three? four?) things:
-- To call your distance_formula, you need to put parentheses after the function name: distance_formula()
-- I would change distance_formula so that it returns the solution (as a float) instead of outputting the solution inside the function and then returning 0. Otherwise, you'll be outputting both the solution (inside the function, on line 17) and 0 (the return value, on line 33).

Also, global variables are generally frowned upon, so try to avoid using them when possible.
I would have written your code as
1
2
3
4
5
6
7
8
9
10
11
12
13
float distance_formula(float num1, float num2, float num3, float num4)
{
    // ...
    return solution;
}

int main()
{
    float num1, num2, num3, num4;
    // ...
    cout << "The distance between these two pairs of numbers are: "
         << distance_formula(num1, num2, num3, num4) << endl;
}

On a side note, since this is C++, I would advise using #include <cmath> instead of #include <math.h> .
In this case, however, since you don't actually use any of the functions inside that header, you don't even need it at all. (Basic math operations like addition, subtraction, multiplication, division, and (integer) modulus are built-in operators -- you don't need to #include anything for those.)
Topic archived. No new replies allowed.