IF statement inside a switch

Whenever I run my program no matter what my input is it will always continue.

case 'w': case 'W':
cout << "You have chosen to Withdraw from your account" << endl;
cout << "Do you wish to proceede? (y/n)" << endl;
char acceptance1;
cin >> acceptance1;
if (acceptance1 == 'y' or 'Y')
{
cout << "Enter amount to Withdraw" << endl;
int wa;
cin >> wa;
if( wa <= balence )
{
cout << "New balence after withdraw=" << bal$
break;
}
else
{
cout << "You have insufficient funds" << end$
break;
}
break;
}

The output is

You have chosen to Withdraw from your account
Do you wish to proceede? (y/n)
n
Enter amount to Withdraw

so the if statement isnt working. Any ideas why?
The if condition is always true: acceptance1 == 'y' or 'Y'

or is an operator that takes two arguments. If at least one of the arguments is true it will return true. For non boolean values anything that is not zero is treated as true. 'Y' is not zero so the loop condition is equivalent to acceptance1 == 'y' or true, which means that it will always return true.
EDIT: Peter87 beat me, by a whole two minutes.

if (acceptance1 == 'y' or 'Y')

This means "if (acceptance1 == 'y') is not zero, OR ('Y') is not zero". Since 'Y' is not zero, the if statement is always true. You want to write this:

if (acceptance1 == 'y' or acceptance1 == 'Y')

By the way, the or keyword is generally only used on systems where || cannot be represented. It is traditional to use || and && rather than or and and.
Last edited on
How do I get the if statement to accept both 'y' and 'Y' then? is it similar to how I did the case for the switch?
See my post that wasn't on your page when you viewed Peter87's post.
Topic archived. No new replies allowed.