illegal if problem

could anyone please explain what i did wrong in this code ?
#include <iostream>
using namespace std;

int main ()
{
cout << "Please enter the first number";
int firstNumber;
cin >> firstNumber;
cout << "Please enter the second number";
int secondNumber;
cin >> secondNumber;
if (firstNumber == secondNumber)
cout << "The numbers are equal";
else (firstNumber > secondNumber);
cout << "secondNumber is less then firstNumber";
else (secondNumber > firstNumber);
cout << "firstNumber is less then secondNumber";
}
It says illegal use of if but im having trouble understanding where exactly
use else if after your first if statement. also you have extra ; after your if statements that dont belong there. also you dont really need to combine them you can just use 3 different if statements. i will keep them here though since you want to use them.

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
#include <iostream>
using std::cout;
using std::cin;


int main ()
{
    cout << "Please enter the first number : ";
    int firstNumber = 0;
    cin >> firstNumber;

    cout << "Please enter the second number : ";
    int secondNumber = 0;
    cin >> secondNumber;

    if (firstNumber == secondNumber)
        cout << "The numbers are equal";
    else if (firstNumber > secondNumber) // no ; here
        cout << "second Number is less then first Number";
    else if (secondNumber > firstNumber) // no ; here
        cout << "first Number is less then second Number";



}
Last edited on
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include <iostream>
using namespace std;

int main ()
{ 
    cout << "Please enter the first number";
    int firstNumber;
    cin >> firstNumber; 
    cout << "Please enter the second number";
    int secondNumber;
    cin >> secondNumber; 
    if (firstNumber == secondNumber) 
        cout << "The numbers are equal";
    else if (firstNumber > secondNumber) // else if here and remove the ; 
        cout << "secondNumber is less then firstNumber"; 
    else // (secondNumber > firstNumber) <- no need for condition here and remove the ; 
        cout << "firstNumber is less then secondNumber";
    return 0;
}
@acorn For the 3rd if condition, I don't think you need to use else if, else by itself should suffice.
Last edited on
else assumes any other condition. thats not really what i wanted. it doesnt evaluate second less then first. so i chose to keep it. your right it works either way. but i choose to do it this way instead of having the unforseen sorta speak cause it to launch the wrong condition.
Last edited on
closed account (z05DSL3A)
There are only three states that you can have, equal, greater than or less than, if you test for two and it is not them then is has to be the third.
you can do it your way. i dont like to assume anything at all. theres nothing wrong with the way i did it.
acorn wrote:
you can do it your way. i dont like to assume anything at all. theres nothing wrong with the way i did it.


Grey Wolf is correct. There's nothing here to assume, only three states, period.
closed account (z05DSL3A)
acorn wrote:
you can do it your way. i dont like to assume anything at all. theres nothing wrong with the way i did it.

I did not say that your way was wrong per se, I was merely clarifying the point for the OP.
ah I get it thanks for your input
Topic archived. No new replies allowed.