Trouble with returning a value.

Hey so i have a code here that only will return the value that is desired within the fuction but when it gets returned to the main function it displays something else... here's the code...

NVM I FIGURED IT OUT ON MY OWN NOVEL IDEA HUH????

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

int distance (float pointX1, float pointX2, float pointY1, float pointY2);
int slope (float pointX1, float pointX2, float pointY1, float pointY2);

int main()
{
	float inputX1, inputX2, inputY1, inputY2, distanceOut, slopeOut;
	cout << fixed;
	
	cout << "Enter x1: ";
	cin >> inputX1;
	cout << "Enter y1: ";
	cin >> inputY1;
	cout << "Enter x2: ";
	cin >> inputX2;
	cout << "Enter y2: ";
	cin >> inputY2;
	
	distanceOut = distance(inputX1, inputX2, inputY1, inputY2);
	slopeOut = slope(inputX1, inputX2, inputY1, inputY2);

	cout << "The distance is " << setprecision(2) << distanceOut << endl;

	if (inputY1 == inputY2)
	{
		cout << "The line is horizontal";
	}
	else if (inputX1 == inputX2)
	{
		cout << "The line vertical.";
	}
	else
	{
		cout << "The line is neither horizontal nor vertical.\n";
		cout << "The slope is " << setprecision(2) << slopeOut << endl;
	}
	
	return 0;
	system("pause");

}

int distance (float pointX1, float pointX2, float pointY1, float pointY2)
{
	float distanceCalc;
	
	distanceCalc = sqrt(pow((pointX2-pointX1),2) + pow((pointY2-pointY1),2));

	return distanceCalc;
}

int slope (float pointX1, float pointX2, float pointY1, float pointY2)
{
	float slopeCalc;
	
	slopeCalc = ((pointY1-pointY2)/(pointX1-pointX2));
	
	return slopeCalc;
}



and heres the output... the slope should be 1.33

Enter x1: 2
Enter y1: 2
Enter x2: 5
Enter y2: 6
The distance is 5.00
The line is neither horizontal nor vertical.
The slope is 1.00




Thanks for the help. :)

pasted the working code here

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

float distance (float pointX1, float pointX2, float pointY1, float pointY2);
float slope (float pointX1, float pointX2, float pointY1, float pointY2);

int main()
{
	float inputX1, inputX2, inputY1, inputY2, distanceOut, slopeOut;
	cout << fixed;
	
	cout << "Enter x1: ";
	cin >> inputX1;
	cout << "Enter y1: ";
	cin >> inputY1;
	cout << "Enter x2: ";
	cin >> inputX2;
	cout << "Enter y2: ";
	cin >> inputY2;
	
	distanceOut = distance(inputX1, inputX2, inputY1, inputY2);
	slopeOut = slope(inputX1, inputX2, inputY1, inputY2);

	cout << "The distance is " << setprecision(2) << distanceOut << endl;

	if (inputY1 == inputY2)
	{
		cout << "The line is horizontal";
	}
	else if (inputX1 == inputX2)
	{
		cout << "The line vertical.";
	}
	else
	{
		cout << "The line is neither horizontal nor vertical.\n";
		cout << "The slope is " << setprecision(2) << slopeOut << endl;
	}
	
	return 0;
	system("pause");

}

float distance (float pointX1, float pointX2, float pointY1, float pointY2)
{
	float distanceCalc;
	
	distanceCalc = sqrt(pow((pointX2-pointX1),2) + pow((pointY2-pointY1),2));

	return distanceCalc;
}

float slope (float pointX1, float pointX2, float pointY1, float pointY2)
{
	float slopeCalc;
	
	slopeCalc = ((pointY1-pointY2)/(pointX1-pointX2));
	
	return slopeCalc;
}
Last edited on
Topic archived. No new replies allowed.