no error but only 0?

hey guys, the given program I wrote gives no errors but when I run it, I only get 0s whats going on?

#include <cstdlib>
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

double distanceTwo(double xone, double xtwo, double yone, double ytwo);
double result;
double xone, xtwo, yone, ytwo;

int main()
{

//Get the numbers
cout << "Please enter the X and Y of the first point" << endl;
cin >> xone >> yone;

cout <<endl << "Please enter the X and Y of the second point" << endl;
cin >> xtwo >> ytwo;


cout <<endl << "the distance of " << xone << "," << yone << " and " << xtwo << ","
<< ytwo << " is " << result << "." << endl <<endl ;

result = distanceTwo(xone, xtwo, yone, ytwo);

system("PAUSE");
return EXIT_SUCCESS;
}

double distanceTwo(double xone, double xtwo, double yone, double ytwo)
{
double r;
r = sqrt((pow((xone-xtwo),2))+(pow((yone-ytwo),2)));
return result;
}

thanks in advance!


Your first problem is you are printing out the result before you have gone to the function to calculate it, you need to call
result = distanceTwo(xone, xtwo, yone, ytwo);
before your cout statements.

The second problem is you are setting a double 'r' in your function equal to an equation. Then you are returning the variable 'result' which hasn't been given a value at that point.

You will need to change it to
1
2
3
double r;
r = sqrt((pow((xone-xtwo),2))+(pow((yone-ytwo),2)));
return r;
Hey James,
thanks so much for your reply! I really appreciate it!
I made the given fixes to my code which when I think about it does make sense,
but unfortunately I still receive 0 for any type of number i put in when I run it.

here is the fixed version:
#include <cstdlib>
#include <iostream>
#include <cmath>
#include <string>

using namespace std;

double distanceTwo(double xone, double xtwo, double yone, double ytwo);
double result;
double xone, xtwo, yone, ytwo;

int main()
{

//Get the numbers

result = distanceTwo(xone, xtwo, yone, ytwo);
cout << "Please enter the X and Y of the first point" << endl;
cin >> xone >> yone;

cout <<endl << "Please enter the X and Y of the second point" << endl;
cin >> xtwo >> ytwo;


cout <<endl << "the distance of " << xone << "," << yone << " and " << xtwo << ","
<< ytwo << " is " << result << "." << endl <<endl;

system("PAUSE");
return EXIT_SUCCESS;
}

double distanceTwo(double xone, double xtwo, double yone, double ytwo)
{
double r;
r = sqrt((pow((xone-xtwo),2))+(pow((yone-ytwo),2)));
return r;
}
I should of been clearer when I said put your result before the cout statements. Seeing as C++ reads from top to bottom it needs to be placed after the user enters in xone/yone etc so that the function knows the values for those variables, but before you print out the final result.

1
2
3
4
5
6
7
8
9
10
cout << "Please enter the X and Y of the first point" << endl;
cin >> xone >> yone;

cout <<endl << "Please enter the X and Y of the second point" << endl;
cin >> xtwo >> ytwo;

result = distanceTwo(xone, xtwo, yone, ytwo);

cout <<endl << "the distance of " << xone << "," << yone << " and " << xtwo << ","
<< ytwo << " is " << result << "." << endl <<endl;
OOOOOOOOOOOHHH jeez, thank you so much!! Learned something new today!!
I was able to get the program to work and now im getting the correct answers!!!

Much appreciated!!
Topic archived. No new replies allowed.