Else If just doesn't work.

Please see codes below. What an easy concept but I can't get it right.
When you type number 1, it outputs English word one. So on. Please help!

int main()
{
int n;
cin >> n;
cin.ignore(numeric_limits<streamsize>::max(), '\n');

if (n = 2)
{
cout << "Two";
}
else
{
if (n = 1){
cout << "One";
}
else{
cout << "Invalid";
}

}


return 0;

}
In the code if (n = 2) , n = 2 sets the value of n to 2.

Did you mean if (n == 2) , which compares n with 2 ?
1
2
3
4
n = 2  // assign value '2' to variable 'n'. Always true
n == 2 // true, if variable 'n' already has value 2
2 == n // true, if variable 'n' already has value 2
2 = n  // error 
Yes.... double equal sign, not single. Thank you!
Topic archived. No new replies allowed.