Slope calculator with multiple functions

Hi, I'm having some difficulties getting this program to run. I don't think I'm using the functions correctly. Any help or hints would be much appreciated!

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
double inf = std::numeric_limits<double>::infinity();

using namespace std;

int main ()
{
	float x1, y1, x2, y2;
	string clause;
	
	cout << setprecision(3);
	
	cout << "Enter x1: ";
	cin >> x1;
	cout << "Enter y1: ";
	cin >> y1;
	cout << "Enter x2: ";
	cin >> x2;
	cout << "Enter y2: ";
	cin >> y2;
	
	float distance = distance (float x1,y1,x2,y2);
	
	cout << "The distance is " << distance << ".";
	
	float slope = slope (float x1,y1,x2,y2);	
	
	if (slope == 0) {
		clause = "\nThe line is horizontal.";
		cout << clause << endl;
	}
	
	else if (slope == inf) {
		clause = "\nThe line is vertical.";
		cout << clause << endl;
	}
	else { (slope  <0 || slope > 0) ; 
		clause = "neither vertical nor horizontal.";
		cout << "\nThe slope is " << slope << ", " << clause << endl;
	}
	
}
	
int distance (float x1,y1,x2,y2) {
	distance = sqrt(pow((x2-x1) ,2) + pow((y2 - y1) ,2));
	
	cout << "The distance is " << distance << ".";
	
}
	
int slope (float x1,y1,x2,y2) {
slope = ((y1-y2)/(x1-x2));
	
}	
	
Line 21,25: float does not belong in the argument list.

Line 21,25: You do not have function prototypes for slope and distance.

Line 21,25: It's a poor idea to name your variables the same as your functions.

Line 43,50: Slope and distance are int functions. You probably want these to return doubles.

Line 43,50: Each argument must have a type.

Lines 47, 52: These functions do not return a result.


Ok I've implemented what you suggested but I'm still getting "not declared" errors in the compiler. Thanks for your help so far!

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

double inf = std::numeric_limits<double>::infinity();
using namespace std;

int distance ();

int slope ();

int main ()
{
	float x1i, y1i, x2i, y2i;
	string clause;
	
	cout << setprecision(3);
	
	cout << "Enter x1: ";
	cin >> x1i;
	cout << "Enter y1: ";
	cin >> y1i;
	cout << "Enter x2: ";
	cin >> x2i;
	cout << "Enter y2: ";
	cin >> y2i;
	
	float distance = distance (x1i,y1i,x2i,y2i);
	
	cout << "The distance is " << distance << ".";
	
	float slope = slope (x1i,y1i,x2i,y2i);	
	
	if (slope == 0) {
		clause = "\nThe line is horizontal.";
		cout << clause << endl;
	}
	
	else if (slope == inf) {
		clause = "\nThe line is vertical.";
		cout << clause << endl;
	}
	else { (slope  <0 || slope > 0) ; 
		clause = "neither vertical nor horizontal.";
		cout << "\nThe slope is " << slope << ", " << clause << endl;
	}
	
}
	
double distance (x1,y1,x2,y2) {
	distance = sqrt(pow((x2-x1) ,2) + pow((y2 - y1) ,2));
	cout << "The distance is " << distance << ".";
	return distance;
}
	
double slope (x1,y1,x2,y2) {
slope = ((y1-y2)/(x1-x2));
return slope;
	
}	
Lines 5, 7: Your function prototypes do not agree with the implementation of those functions. They must match exactly.

Lines 47,53: Each argument must have a type.

Line 44: You have no variable named distance.

Line 54: You have no variable named slope.

Lines 25,29: You ignored my advice about naming your variables the same as your functions.


Last edited on
Topic archived. No new replies allowed.