@naz: Since we are close to solving your fundamental problem, I looked at your calculation code.
In your original post, you were trying to calculate the slope. As keskiverto pointed out, your slope calculation (a division) uses integer arithmatic. So if your input was 1 1 3 2, your slope calculation would result in (2-1)/(3-1)==(1/2)==0. This is because 2 goes into 1 0 times with a remainder of 1. You could cast one of the variables to double or float. You would have to use the appropriate C++ cast. I am not sure which one that is but I would guess static_cast<double>. TheIdeasMan and giblit warn you that if slope is a double or float, you have to deal with the "approximation" issues of doubles and floats.
As pointed out by keskiverto in his first post, if there is no change in X (dx==0), there will be a division by zero error.
But as giblit points out in his first message and confirmed by keskiverto, you do not need to calculate the slope. You could just look at the differences (deltas) dx and dy. In fact, your problem statement does not require displaying the slope. Given the problems listed above not calculating the slope would be a good approach for you with your level of experience.
As pointed out by cire, your problem statement is misleading. There are two values of slope that are at a 45 degree angle to the x-axis: 1 and -1. As pointed out by keskiverto, this could be handled by using the absolute value function [abs()]. But if you are not comfortable yet using functions, we could handle this by additional cases in the if statements. That would complicate the six cases listed by keskiverto. I think it makes them at least 9 cases.
Or you could just calculate the absolute value directly. For example,
1 2 3 4 5 6 7 8 9 10 11
|
int dx;
int absodx;
...
if (dx<0)
{
absodx = -dx;
}
else
{
absodx = dx;
}
|
Now absodx is the absolute value of dx.
Also as keskiverto points out in his first message, you would be better to use an if ... else if ... else if ... else type chain. Have you ever seen code like this? This has the advantage that if your at the third or fourth else if, you know all the if conditions above it are false.
keskiverto points out latter that you need to deal with the case where dx and dx are both zero. Your assignment does not define what you are supposed to display for that case. For example with the input 3 3 3 3, what should be output? You may want add a message about both points being the same.
So to summarize,
1) remove the slope calculation,
2) use the dx and dy and possibly their absolute values for making the decisions,
3) make sure to handle cases with negative slopes,
4) consider using a if ... else if ... else if ... else chain for simplifying you decisions.
To get you started:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
|
if ( (dx==0) && (dy==0) )
{
cout<<"Both points are the same"<<endl;
}
else if ( dx==0 )
{
cout<<"Line is parallel to Y-Axis"<<endl;
}
else if ( dy==0 )
{
cout<<"Line is parallel to X-Axis"<<endl;
}
else if ( /* next condition and you know it is not verticle or horizontal */ ) {
// The conditions compare dx and dy and/or absodx and absody.
// Output message for this case.
}
// Insert whatever cases need to be handled here.
// Do not be afraid to have two different cases use the same output message if that is more readable.
else
{
// Output message for whatever case is left.
}
|
I hope this helps. In the future, pay close attention to the input you receive. If you do not understand it, ask questions about what they said.