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

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
Please format your code.
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.
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.
Much thanks for the tip. Just wondering if you would know how to fix this code?
What things does your program calculate?
Can you give me some formula if there's any?
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!
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.
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



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.
Also remove the semicolon in line 44.
Topic archived. No new replies allowed.