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.
#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";
elseif (firstNumber > secondNumber) // no ; here
cout << "second Number is less then first Number";
elseif (secondNumber > firstNumber) // no ; here
cout << "first Number is less then second Number";
}
#include <iostream>
usingnamespace 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";
elseif (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;
}
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.