if / else problem!

Hi all. I'm having a bit of trouble executing a if / else statement with multiple if's. I think its just a problem with the way the brackets are set up. Here's my code:

If I set it up this way, I get the output I'm looking for for the first if statement (that is if I put in the same line twice and it tells me that there is no slope):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
    if (x1 == x2 && y1 == y2)
    {
    cout << " You've entered the same point twice. No slope, no line. " << endl;       
    if (x1 == x2 && y1 != y2)
    {
    cout << "The line is vertical and intersects the x-axis at " << x1 << "." << endl;
    if (y1 == y2 && x1 != x2) 
    {
    cout << " The line is horizontal and intersects the y-axis at: " << y1 << "." << endl;
    }}}
    else 
    {
    cout << " The equation of the line is y = " << slope << "x + " << intersect << endl;
    cout << " The distance between the 2 points is " << distance << endl;      
    }


The problem with this is that if I try to make the second or third if statements true (entering two points with either the same x or y values), I dont get the desired output but instead I get the else's output.

I'm not sure if what I even want to accomplish is possible using the if / else setup I have right now. The one thing is that this assignment is required to use if statements, so I wont be able to use any more advanced techniques quite yet. But if it isnt, please let me know and point me in the right direction!

Any help would be appreciated!
The else statement (line 11) belongs to the first if statement (line 1) so in case that:

1) the first if is not true - then the else stametent is executed
2) the first if is true - the other conditions are evaluated which is quite useless because they have to be false in any case

To make your program working use else if clause:

e.g.
1
2
3
4
if (x==1) y=1;
else if (x==2) y=2;
else if (x==3) y=3;
else x=0;
Instead of using nested if you need to use the else-if ladder, the code will look like below:

if (x1 == x2 && y1 == y2)
{
cout << " You've entered the same point twice. No slope, no line. " << endl;
}
else if (x1 == x2 && y1 != y2)
{
cout << "The line is vertical and intersects the x-axis at " << x1 << "." << endl;
}
else if (y1 == y2 && x1 != x2)
{
cout << " The line is horizontal and intersects the y-axis at: " << y1 << "." << endl;
}
else
{
cout << " The equation of the line is y = " << slope << "x + " << intersect << endl;
cout << " The distance between the 2 points is " << distance << endl;
}
Thanks for the help guys. Setting it up using the else if structure worked perfectly.
Topic archived. No new replies allowed.