while c++

closed account (ozyMoG1T)
Write your question here.



Whenever I put a lowercase 'c', the program doesn't exit. I don't know why this happen.
Last edited on
closed account (48T7M4Gy)
dont you mean choice != 'C' || choice != 'c' ?
closed account (ozyMoG1T)
It didn't work if I putchoice != 'C' || choice != 'c' .

But when I use choice != 'C' && choice != 'c' . It works when I put uppercase 'C' (it exits the program), but doesn't work when I put lowercase 'c'.
Last edited on
closed account (48T7M4Gy)
Yeah, you're right about &&

Your compound if's and duplicate cin >> choice aren't helping. Why not use a switch structure combined with making use of uppercasing choice?

Modify line 65 to

else if (choice != 'C' && choice != 'c')

A hint: Remove line 68.

Another hint: Variable choice will be accessed having an undefined value when entering the loop. Use f.e. a do-while loop.
Hi mwu,

This is really the same subject as your other post, and we are now all giving the same advice all over again.

I am sure I mentioned in one of your other posts, about what to do with upper & lower case input. That is use either the toupper or tolower functions, thus cutting the logic in half - only test for one not both.

Then you won't have to have ugly, UGLY, UGLY constructs like this:

while (choice != 'C' && choice != 'c')

Sorry tcs, I am sure you understand that I am mortally opposed to constructs like that :+D lol

Did I (& others) mention using a switch for this as well ? There is no need to have the choice of C as a condition in the loop. Instead use a bool quit variable, which is set inside the case: 'C' , now the end condition for the while loop uses the bool variable:

1
2
3
4
5
6
bool quit = false;
while (!quit) 
{

  // switch statement
}


Also, each case: should call a function - that makes it much easier to understand, primarily because the whole switch becomes shorter. Don't forget the default case to catch bad input.
Last edited on
Topic archived. No new replies allowed.