As in, if you had an if statement, for example:

if(endProgramChooseType != 'y' || 'Y' || 'N' || 'n'){
(code for exiting program)
}

How would you do that in a switch statement? without doing

case 'a':
case 'A':
case 'b':
case 'B':
case 'c':

etc.

Recommended Answers

All 8 Replies

switch(endProgramChooseType)
{
    case 'y':
    case 'Y':
    // Code for either y or Y goes here
    break;
    case 'n':
    case 'N':
    // Code for either n or N goes here
    break;
    case 'z':
    // z-code here
    break;
    default:
    // if nothing else fits do this
    // ...
}

Your if() is wrong. Would be
x != 'y' || x != 'Y' || x != 'N' ...

commented: thx +1

Yeah just as you posted, just before I looked, I remembered about the default.. i'm such an idiot, lol.. And really? It worked before. I had that if() before, but am deciding to practice switch statements because apparently they're more widely used for larger stuff.
ah well, thanks.

Yeah just as you posted, just before I looked, I remembered about the default.. i'm such an idiot, lol.. And really? It worked before. I had that if() before, but am deciding to practice switch statements because apparently they're more widely used for larger stuff.
ah well, thanks.

Writing the comparison for your if that way is technically legal syntax, so it does "work". The problem is it does not work like you assume.

You need to remember that any value can be used as a boolean value. If the value equates/evaluates to (0), the bool equivalent is false, any other value is considered true. Thus when you write:

if(endProgramChooseType != 'y' || 'Y' || 'N' || 'n')

as your comparison, what the compiler actually sees is:

if(endProgramChooseType != 'y' || true || true || true)

This causes the expression to always evaluate to a true result, regardless of the value the user enters.

Writing the comparison for your if that way is technically legal syntax, so it does "work". The problem is it does not work like you assume.

So would it be..

if(endProgramChooseType != 'y' || != 'Y' || != 'N' || != 'n')

?

thanks for this, I actually love when people explain it to me personally.

So would it be..

if(endProgramChooseType != 'y' || != 'Y' || != 'N' || != 'n')

?

thanks for this, I actually love when people explain it to me personally.

No.
It would be

if(endProgramChooseType != 'y' || endProgramChooseType != 'Y' || endProgramChooseType != 'N' || endProgramChooseType != 'n')

But this has the same problem, it ALWAYS evaluates to true, regardless of input.

What are you trying to accomplish? What is the prompt your user is responding to? I'm 99% sure this is not the solution to your problem.

Well, what I used before was:

if(endProgramChooseType != 'y' || 'Y' || 'N' || 'n'){
		cout << endl << "That is not a valid selection." << endl << endl;
		cout << "Please select again." << endl << endl;
		system("cls");
		endProgram();
	    return 0;
}

Which seemed to work. But now im getting rid of this if and changing all of it to a switch statement. this is just a personal practice project.

This is the end of the program, and im asking whether they want to restart the program or just end it. N for end it, Y for restart.

This is from a program that I completed quite a while ago:

int main() {
  char playAgain = 'y', //...

  //misc. initializations and set-up...

  //begin main application loop
  while (playAgain == 'y' || playAgain == 'Y') {

    //majority of executable code...


    cout << "Would you like to play again (y/n)? ";
    cin.get(playAgain);

  }	//end main application loop

  //misc. clean-up...

  return EXIT_SUCCESS;

}  //end main()

This code assumes that any input other than 'y' or 'Y' is a "no" response. The file these few lines are taken from is only about 70 lines long. The program, however, is a little over 1,500 lines. I know you don't need to know that, but I mention it to show that it doesn't take much code to accomplish what you want to. This example is only 4 lines, don't make it more difficult than necessary; to accomplish the same with a switch will require at least 8 lines.

If you want an explicit "no" you'll have to expand it a little. Also, don't forget about the AND operator '&&'. Sometimes, changing an OR ( '||' ) to an AND is all it takes to fix a comparison that's not behaving as expected.

Thanks for that. Ill keep this in mind when i'll be using an OR statement next.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.