Reputation: 19
I'm a beginner learning code, and I was copying what a YouTube video for programming teaching said. But when I wrote the code, it resulted in some errors.
Here's the code:
#include <iostream>
using namespace std;
int main()
{
int num1 , num2;
cout<< " Enter number 1 and number 2 \n";
cin>> num1 >> num2;
if (num1 == num2);
cout<< "The both numbers are equal \n";
else if (num1> num2)
cout<< "Number 1 is greater than number 2 \n";
else (num1< num2)
cout<< "Number 2 is greater than number 1 \n";
return 0;
}
Upvotes: 1
Views: 339
Reputation: 15824
You dont need ;
after if
condition check
If you want to do a condition check, you should use else if
, in that case else
is not enough:
#include <iostream>
using namespace std;
int main()
{
int num1 , num2;
cout<< " Enter number 1 and number 2 \n";
cin>> num1 >> num2;
if (num1 == num2)
cout<< "The both numbers are equal \n";
else if (num1> num2)
cout<< "Number 1 is greater than number 2 \n";
else if (num1< num2)
cout<< "Number 2 is greater than number 1 \n";
return 0;
}
Upvotes: 1
Reputation: 1102
; is not placed after `if` condition
Moreover, else does not get a condition... it always checks the negation of its corresponding if.
In fact if the condition of if
does not hold the code in the block of else
is executed.... by changing
else (num1< num2)
cout<< "Number 2 is greater than number 1 \n";
to
else
cout<< "Number 2 is greater than number 1 \n";
your problem will get solved.
Upvotes: 1
Reputation: 172964
Note that ;
means the expression ends, so you should change
if (num1 == num2);
to
if (num1 == num2)
And else
doesn't need condition, so change
else (num1< num2)
to
else
Upvotes: 8