Super new to this, would appreciate some help from anyone (slope calculator)

Oct 5, 2016 at 2:19am
This is my code that I wrote for my class but it doesn't seem to work as intended. It compiles fine and works fine just up to the point where try to get it to solve a regular slope value that is neither horizontal nor vertical. Any help 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

#include <iostream>
#include <iomanip>
#include <math.h>
#include <limits>

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

using namespace std;

int main ()
{
	float x1, y1, x2, y2;
	float distance;
	float slope;
	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;
	
	distance = sqrt(pow((x2-x1) ,2) + pow((y2 - y1) ,2));
	
	cout << "The distance is " << distance << ".";
	
	slope = ((y1-y2)/(x1-x2));
	
	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 = "\nneither vertical nor horizontal.";
		cout << "\nThe slope is " << slope << ", " << clause << endl;
	}
				
}

Last edited on Oct 5, 2016 at 2:46am
Oct 5, 2016 at 2:36am
Please format your code.
Oct 5, 2016 at 2:38am
Hi, much appreciated quick reply! I'm not 100% sure what you mean by this. Would you like me to tidy it up for you and re post or is the formatting the problem with my code? If it's the latter I would really appreciate it if you would explain.
Oct 5, 2016 at 2:43am
Edit your post and put all the code in [code] Your code [/code] tags.

Your code will look about 300% cleaner, at least. This is the way of getting a good reply.
Oct 5, 2016 at 2:48am
Much thanks for the tip. Just wondering if you would know how to fix this code?
Oct 5, 2016 at 2:50am
What things does your program calculate?
Can you give me some formula if there's any?
Oct 5, 2016 at 2:58am
The formula for calculating the slope is given in line 33 and for the distance between plot points in line 29. The program works fine for the distance between the plot points so we can disregard that. However, for the slope it works and spits out the proper statements if the answer is horizontal or vertical. The only thing wrong is if I try to get it to calculate the slope that is not one of these special cases, it spits out slope = inf and the line is vertical which doesn't make sense. Thanks for your patience!
Oct 5, 2016 at 3:01am
I am actually poor at Math.
If you could actually give me some formula to show how they are calculated, it would be very helpful.
Oct 5, 2016 at 3:12am
slope is equal to= ((y1-y2)/(x1-x2)); y1,y2,x1,x2 being points on a given graph.

The distance between points is = sqrt(pow((x2-x1) ,2) + pow((y2 - y1) ,2)) where x and y are both points on a graph as well.


Once they are calculated, the program spits out the values. I want it to say if the slope is undefined (divided by zero) that the line is vertical. Likewise if the slope is 0, I want it to say that the line is horizontal.

The issue that I'm having is that it it saying the line is vertical when the slope has been calculated to be not undefined.

Thanks



Oct 5, 2016 at 4:42am
Check for equality on line 40. Your compiler is probably issuing a warning on compilation that you are ignoring.

== checks for equality. = is for assignment.
Oct 5, 2016 at 6:34am
Also remove the semicolon in line 44.
Topic archived. No new replies allowed.