From: Peter on
Hi All,

I have as part of my program the following do while loop to display a
menu.

char menuChoice;
do
{
printf("Main Menu\n");
printf("a. Log in.\n");
printf("b. Create new profile.\n");
printf("q. Quit\n");
printf("> ");

// Take action based on the user's choice.
scanf("%c", &menuChoice);
scanf("%*[^\n]");
scanf("%*c");
switch (menuChoice) {
case 'a':
printf("Not Implemented.\n");
break;
case 'b':
printf("Not Implemented.\n");
break;
case 'c':
case 'q':
printf("\nQuitting...\n\n");
break;
default:
printf("\nInvalid Option...\n");
}
} while (menuChoice != 'q' || menuChoice != 'c');

However when I try to quit from the menu using 'q' or 'c' the menu
continues to display and I have to use Ctrl-C to exit the program.

If I just put at the end:
} while (menuChoice != 'q')

then I am able to quit if I enter 'q'

Can someone please tell me why?

Many thanks,

Peter.
From: Ralph D. Ungermann on
Peter wrote:
> do
> {
[...]
> } while (menuChoice != 'q' || menuChoice != 'c');

quit on q || c <==> continue on !( q || c) <==> !q && !c

-- ralph

From: Peter on
On Feb 1, 10:11 pm, "Ralph D. Ungermann" <use...(a)mloge-ungermann.de>
wrote:
> Peter wrote:
> > do
> > {
> [...]
> > } while (menuChoice != 'q' || menuChoice != 'c');
>
> quit on q || c <==> continue on !( q || c) <==> !q && !c
>
> -- ralph

Sorry ralph I have no idea what you mean, can you please elaborate?
From: Daniel T. on
Peter <Peter.Edwin.Dennis(a)gmail.com> wrote:

> I have as part of my program the following do while loop to display a
> menu.
>
> char menuChoice;
> do
> {
> printf("Main Menu\n");
> printf("a. Log in.\n");
> printf("b. Create new profile.\n");
> printf("q. Quit\n");
> printf("> ");
>
> // Take action based on the user's choice.
> scanf("%c", &menuChoice);
> scanf("%*[^\n]");
> scanf("%*c");
> switch (menuChoice) {
> case 'a':
> printf("Not Implemented.\n");
> break;
> case 'b':
> printf("Not Implemented.\n");
> break;
> case 'c':
> case 'q':
> printf("\nQuitting...\n\n");
> break;
> default:
> printf("\nInvalid Option...\n");
> }
> } while (menuChoice != 'q' || menuChoice != 'c');
>
> However when I try to quit from the menu using 'q' or 'c' the menu
> continues to display and I have to use Ctrl-C to exit the program.
>
> If I just put at the end:
> } while (menuChoice != 'q')
>
> then I am able to quit if I enter 'q'
>
> Can someone please tell me why?

The loop will continue as long as menuChoice does not equal 'q' or
menuChoice does not equal 'c'.

The way || works is: The compiler will check the left side, if that is
true the expression is true. If the first part is false, then the
compiler will check the right side, if that is true then the expression
is true, if it is false the expression is false.

So, if menuChoice == 'q', the first part is false but the second part is
true, therefore the expression is true.

If menuChoice == 'c', then the first part is true, therefore the
expression is true.
From: Quique on

In conclusion:

because of the propositional logic that Daniel and Ralph explained,
while (menuChoice != 'q' || menuChoice != 'c');
should actually be
while (menuChoice != 'q' && menuChoice != 'c');

I think you should make sure you understand this stuff. Our language
is sometimes sloppy, but programming cannot be so.
 |  Next  |  Last
Pages: 1 2
Prev: Segmentation fault :(
Next: urgent help