Can't get my return values to properly work.

Hello There,
I am a new Computer Science student and for our first project, we are supposed to write a program that will determine if 3 given points equal a right triangle.
I have not finished writing the program, so this is just the section for calculating the distance between points.

Problem: I cannot get my return values to work properly. After calcDistance finds the distance, it should return it as a single value "a". But after the program runs, it remains zero.

Any help would be most greatly appreciated. I am new to the forum so, if there is anything wrong with any formatting, please do tell me.
Thank You!



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
#include "stdafx.h"
#include <iostream>
#include <cmath>

using namespace std;

float x1, Y1, x2, y2, x3, y3;	 float a, b, c;


float calcDistanceP1(float x2, float x1, float y2, float Y1);
float calcDistanceP2(float x2, float x1, float y2, float Y1);
float calcDistanceP3(float x3, float x2, float y3, float y2);

int main() {

	void welcomeMessage(); {
		cout << "Welcome To The Right Angle Triangle Calculator " << endl;
	}
	cout << "Please Enter The First Point (x1 Y1): ";
	cin >> x1 >> Y1;
	cout << endl;

	cout << "Now Enter The Second Point (x2 y2): ";
	cin >> x2 >> y2;
	cout << endl;

	cout << "And Finally The Third Point (x3 y3): ";
	cin >> x3 >> y3;
	cout << endl;

	float calcDistanceP1(float x2, float x1, float y2, float Y1);
	float calcDistanceP2(float x3, float x1, float y3, float y1);
	float calcDistanceP3(float x3, float x2, float y3, float y2);
	


	return 0;
}

float calcDistanceP1(float x2, float x1, float y2, float Y1){
	float a = sqrt(pow(x2 - x1, 2) + pow(y2 - Y1, 2));
	return a; // These three return values is where I am having trouble 
}

float calcDistanceP2(float x3, float x1, float y3, float y1) {
	float b = sqrt(pow(x3- x1, 2) + pow(y3 - Y1, 2));
	return b; // Here
}

float calcDistanceP3(float x3, float x2, float y3, float y2) {
	float c = sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2));
	return c; //Here
}

Looks good to me.

Put a cout statement before the return and see what value you get before it exits the function.
I did have one there before. It remains at zero.
the code below is your code I had changed. It works as expected.

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
  1 //#include "stdafx.h"
  2 #include <iostream>
  3 #include <cmath>
  4 
  5 using namespace std;
  6 
  7 //float x1, Y1, x2, y2, x3, y3;  float a, b, c;  
  8 //the definition here has a problem during compiling
  9 
 10 float calcDistanceP1(float , float , float , float );
 11 //float calcDistanceP2(float x2, float x1, float y2, float Y1);
 12 //float calcDistanceP3(float x3, float x2, float y3, float y2);
 13 void welcomeMessage(){
 14         cout<<"welcome to the Right Angle Triangle caculator"<<endl;
 15 }
 16 int main() {
 17         float x1,y1,x2,y2,x3,y3;float a,b,c;
 18         welcomeMessage();
 19         cout << "Please Enter The First Point (x1 Y1): ";
 20         cin >> x1 >> y1;
 21         cout << endl;
 22 
 23         cout << "Now Enter The Second Point (x2 y2): ";
 24         cin >> x2 >> y2;
 25         cout << endl;
 26 
 27         cout << "And Finally The Third Point (x3 y3): ";
 28         cin >> x3 >> y3;
 29         cout << endl;
 30 
 31         a=calcDistanceP1( x2,  x1,  y2,  y1);
 32         b=calcDistanceP1( x3,  x1,  y3,  y1);
 33         c=calcDistanceP1( x3,  x2,  y3,  y2);
 34         cout<<"a:"<<a<<" b:"<<b<<" c:"<<c<<endl;
 35 
 36 
 37         return 0;
 38 }
 39 
 40 float calcDistanceP1(float x_2, float x_1, float y_2, float Y_1){
 41         float distance =0;
 42         distance=(float)sqrt(pow(x_2 - x_1, 2) + pow(y_2 - Y_1, 2));
 43         return distance; // These three return values is where I am having trouble 
 44 }
 45 #if 0
 46 float calcDistanceP2(float x3, float x1, float y3, float y1) {
 47         float b = sqrt(pow(x3- x1, 2) + pow(y3 - Y1, 2));
 48         return b; // Here
 49 }
 50 
 51 float calcDistanceP3(float x3, float x2, float y3, float y2) {
 52         float c = sqrt(pow(x3 - x2, 2) + pow(y3 - y2, 2));
 53         return c; //Here
 54 }
 55 #endif
                                                                                              
 

the beblow is the output,you can compare with the code.And then you will find the problem you have.
zhou@ubuntu:~/zhouting$ g++ triangle_l.cpp -std=c++11
zhou@ubuntu:~/zhouting$ ./a.out
welcome to the Right Angle Triangle caculator
Please Enter The First Point (x1 Y1): 1.2 1.3

Now Enter The Second Point (x2 y2): 2.1 2.3

And Finally The Third Point (x3 y3): 3.2 3.1

a:1.34536 b:2.69072 c:1.36015
zhou@ubuntu:~/zhouting$ echo $?
0
zhou@ubuntu:~/zhouting$
Last edited on
There is some confusion over the difference between declaring a function, and calling one. At the start,
16
17
18
	void welcomeMessage(); {
		cout << "Welcome To The Right Angle Triangle Calculator " << endl;
	}

void welcomeMessage(); is declaring a function - that is it lets the compiler know that it exists somewhere (actually it doesn't exist).

The next part
1
2
3
    {
        cout << "Welcome To The Right Angle Triangle Calculator " << endl;
    }
is just normal code inside main(). The extra enclosing braces in this case don't have any particular effect, so we can ignore them for now.

Later at line 31, there are more function declarations:
 
    float calcDistanceP1(float x2, float x1, float y2, float Y1);
which don't actually cause anything to happen. To call the function, do this :
 
    a = calcDistanceP1(x2, x1, y2, Y1);

Actually you don't need three separate functions, they are all the same.

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
#include <iostream>
#include <cmath>

using namespace std;

float calcDistance(float x1, float x2, float y1, float Y2);

int main()
{   
    cout << "Welcome To The Right Angle Triangle Calculator " << endl;

    float x1, y1;
    float x2, y2;
    float x3, y3;    
    
    cout << "Please Enter The First Point (x1 y1): ";
    cin >> x1 >> y1;
    cout << endl;

    cout << "Now Enter The Second Point (x2 y2): ";
    cin >> x2 >> y2;
    cout << endl;

    cout << "And Finally The Third Point (x3 y3): ";
    cin >> x3 >> y3;
    cout << endl;

    float a = calcDistance(x1, x2, y1, y2);
    float b = calcDistance(x3, x1, y3, y1);
    float c = calcDistance(x3, x2, y3, y2);
    
    cout << "Distance P1 to P2 = " << a << '\n';
    cout << "Distance P3 to P1 = " << b << '\n';
    cout << "Distance P3 to P2 = " << c << '\n';
    
}

float calcDistance(float x1, float x2, float y1, float y2)
{
    float a = sqrt(pow(x2 - x1, 2) + pow(y2 - y1, 2));
    return a; 
}

Welcome To The Right Angle Triangle Calculator
Please Enter The First Point (x1 y1): 0 0

Now Enter The Second Point (x2 y2): 3 0

And Finally The Third Point (x3 y3): 3 4

Distance P1 to P2 = 3
Distance P3 to P1 = 5
Distance P3 to P2 = 4

Last edited on
Thank You So Much! I don't have much time to work on it right now. I have to go to work, but I will hopefully finish it tonight.. Thank you again! You guys are the greatest!
Registered users can post here. Sign in or register to post.