small problem with finding equation of line when user enters negative points

I created a program which calculates the equation of a line for the two points that the user inputs for one of my computer programming assignments, however sometimes when i enter negative numbers of the x or y values i get weird/wrong answers for the slope and y intercept of the equation. i believe that my mistake is when declaring the m (slope) and b (y-intercept) and the equations i am giving the compiler to calculate those values. Does anyone know how i can fix this small problem?
here is my source code

#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;

int main()
{
cout <<"Enter the first point: ";
int c, d;
char punct;

cin >> punct >> c >> punct >> d >> punct;

cout <<"Enter the second point: ";
int e, f;

cin >> punct >> e >> punct >> f >> punct;

double m=((f-d)/(e-c))*1.0; **//this is where i believe the problem is
double b=(d-m*c)*1.0; **//this is where i believe the problem is


cout <<"The equation of the line through ("
<< c <<"," << d <<")
and (" << e <<"," << f <<")
is y=" <<m <<"x" <<showpos <<b <<endl;

system("pause");
return 0;
}


Thanks!!!!
Last edited on
closed account (48T7M4Gy)
What is punct and what is it there for?
closed account (48T7M4Gy)
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
#include<iostream>
#include<cstdlib>
#include<iomanip>
using namespace std;

int main()
{
	cout << "Enter the first point: ";
	int c, d;

	cin >> c >> d;

	cout << "Enter the second point: ";
	int e, f;

	cin >> e >> f;

	double m = ((f - d) / (e - c))*1.0;//this is where i believe the problem is <=No its not!!
	double b = (d - m*c)*1.0;//this is where i believe the problem is <= Nope

	cout << "The equation of the line through (" << c << "," << d << ")and(" << e << ", " << f << ") is y = " << m << "x" << b << endl;

	system("pause");
	return 0;
}
the reason why i declared the character punct is becasue my professor wants the user to input the point with a parenthesis and a comma like this: (x,y).
all you did was take of the character punct so that would still give me the same answers, the problem is the compiler is not calculating the negative numbers the correct way.
double m = ((f - d) / (e - c))*1.0;


f, d, e, and c are all integers.

This means you have:

 
((int - int) / (int - int)) * double

which will get evaluated as:
 
( int / int ) * double


This has 2 consequences:

1: Your *1.0 is doing nothing and is completely pointless
2: You are still doing integer division, so your division will be truncated.


If you want to do floating point division you will need to convert one (or both) of your values to a double before you divide. Doing so after you divide is worthless.



EDIT:

Also... c,d,e,f are confusing names. It's hard to keep straight which is which. x1,y1,x2,y2 would have been a better choice.
Last edited on
closed account (48T7M4Gy)
the reason why i declared the character punct is becasue my professor wants the user to input the point with a parenthesis and a comma like this: (x,y).

As a suggestion you need to incorporate that information in your comments and especially in your instructions to the end user. The end user otherwise has no idea what the format of the data input is supposed to be.

punct is not initialized. You should say something like char punct = '0';

In any case it does not solve the problem. You need to do get the end user to type the input as a string and then parse it to get the numerical values if you have to comply literally with your profs requirements. Either that or tell the user what to type in when they reach the '<< punct' point and then, by all means, ignore it in subsequent calculations.

all you did was take of the character punct so that would still give me the same answers, the problem is the compiler is not calculating the negative numbers the correct way.

I didn't check the calculation but, with all due respect, the compiler has only done what you told it to do. In other words your equation is wrong, not the compiler or more than likely any other part of your machine. Take good notice of Disch's comments. Good luck :)
Topic archived. No new replies allowed.