开发者

do-while stopped working

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;
}


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'));


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.


Instead of

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

write

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


val != 'X' || val != 'x'

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜