tags:

views:

161

answers:

4

Back again with another rookie question. While working on a function for my homework project I found that my menu wouldn't exit when I press X, it worked just an hour ago and I haven't changed anything in main(). I commented out all the code in my functions just to be sure that nothing in a function interfered. I just can't find any problem and would be grateful for any help.

int main()
{
    char val, enter;
    int c;
/* loopa med do-while */
do
{
printf("===============================\n");
printf(" Olja i Norge AB\n");
printf("===============================\n\n");
printf("  Artikelregister\n");
printf(" A. Lista artiklar\n");
printf(" B. L\x84gg till artikel\n");
printf(" C. Radera artikel\n");
printf(" D. \x8Endra artikel\n\n");
printf("  Kundregister\n");
printf(" E. Lista kunder\n");
printf(" F. L\x84gg till kund\n");
printf(" G. Radera kund\n");
printf(" H. \x8Endra kund\n\n");
printf("  Ordrar\n");
printf(" I. Best\x84ll\n");
printf(" J. Lista ordrar\n\n");
printf(" X. Avsluta\n");
printf("\n===============================");
printf("\n===============================\n");
printf("V\x84lj: ");
scanf("%c", &val);

 do {
  c = getchar();
 } while (c != EOF && c != '\n');

 switch( val )
 {
  case 'A':
  case 'a':
   printf("\n");
   artList();
   break;

  case 'B':
  case 'b':
   printf("\n");
   artAdd();
   break;

  case 'C':
  case 'c':
   artDel();
   break;

  case 'D':
  case 'd':
   artEdit();
   break;

  case 'E':
  case 'e':
   kundList();
   break;

  case 'F':
  case 'f':
   kundAdd();
   break;

  case 'G':
  case 'g':
   kundDel();
   break;

  case 'H':
  case 'h':
   kundEdit();
   break;

  case 'I':
  case 'i':
   order();
   break;

  case 'J':
  case 'j':
   orderList();
   break;

  case 'X':
  case 'x':
   break;
 }

 printf("\nTryck <ENTER> f\x94r att forts\x84tta.");
 scanf("%c", &enter);
 system("cls");

}while(val != 'X' || val != 'x');

return 0;
}
+16  A: 
do { ... } while (val != 'X' || val != 'x');

if val is 'X' that while "converts" to

do { ... } while (0 || 1);

if val is 'x' that while "converts" to

do { ... } while (1 || 0);

if val is 'a' that while "converts" to

do { ... } while (1 || 1);

It never evaluates to false.

You need to rewrite the condition -- hint: use && :)


Edit

Oh ... that's assuming the "!= has precedence over ||". I never know what the precedence is, and I always use parenthesis in conditional expressions

do { ... } while ((val != 'X') || (val != 'x'));
pmg
Man, that's got to be one of the hardest things for new developers to learn, judging by all the and-or confusion I've seen.
David Thornley
+1 for explaining the answer not just giving a quick answer to gain rep.
Tester101
Thanks, what confused me is that it worked great for a week, and now it just started working properly i guess
Fredrik Johansson
@Frederik: if it "does loop", then it must evaluate to `true`, not to `false` (as the loop keeps going _while_ the expression evaluates to true). And the answer explains why it always evaluates to `true`, including when you type `X` or `x`.
Pavel Minaev
Pavel Minaev
pmg
+1  A: 

Don't want to help you too much, but if you look at it carefully, this condition:

while(val != 'X' || val != 'x');

Will always evaluate to true.

Eric Petroelje
+1  A: 

Instead of

while(val != 'X' || val != 'x')

write

while(val != 'X' && val != 'x')
Tatu Ulmanen
A: 
val != 'X' || val != 'x'

If val == 'x', then val != 'X', so the above is true. Similarly, the above is true when val == 'X'.

outis