C代码卡在无限的do-while循环中

时间:2017-04-04 22:04:23

标签: c debugging while-loop do-while

我正在尝试模拟自动售货机。我的程序从命令行输入一个输入的金额作为价格,然后继续提示,直到程序中止。我试图通过一个while-do循环来实现这个目标,我现在知道它一定是错的。我需要程序继续提示输入硬币,即使他们已经插入了硬币的写入金额。

代码看起来如何编译的示例是:  $ - 表示命令行

$ pop 225
Price must be from 10 to 100 cents
$ pop 86
Price must be a multiple of 5.
$ pop 50
Welcome to my Vending Machine!
Pop is 50 cents. Please insert nickels, dimes, or quarters.

Enter coin [NDQ]:d
You have inserted a dime
Please insert 40 more cents.
Enter coin [NDQ]:d
You have inserted a dime
Please insert 30 more cents.
Enter coin [NDQ]:d
You have inserted a dime
Please insert 20 more cents.
Enter coin [NDQ]:d
You have inserted a dime
Please insert 10 more cents.
Enter coin [NDQ]:d
You have inserted a dime
Pop is dispensed. Thank you for you business! Please come again.
Enter coin [NDQ]

看看最后它如何能够输入硬币,直到E被选中退出。我试图这样做,但是我的代码目前在初始输入N,D或Q时陷入无限循环。希望得到任何帮助或指导。谢谢!

#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#define NI 5
#define DI 10
#define QU 25



bool isValid(int num) {
    return (num >= 10 && num <= 100 );
}

bool isMultiple(int num) {
        return (num % 5 == 0);
}

int 
main (int argc, char *argv[])
{  for (int i = 1; i != argc; ++i) {
         int price = atoi(argv[i]);
        if (!isValid(price)) {
            printf("Price muse be from 10 cents to 100 cents.\n");
            break;
        } else if (!isMultiple(price)) {
                printf("Price must be a multiple of 5.\n");
                break;
        } else {
                printf(" Welcome to my Vending Machine!\n");
                printf("Pop is %d cents. Please enter nickels, dimes, or quarters\n", price);


                char coin;

                do
                {
                    printf(" PLease enter a coin [NDQR]\n");
                     scanf (" %c", &coin);


                        int cents = 0;

                        while (cents <= price) {


                        if (coin == 'N'|| coin == 'n') {
                                cents = cents + NI;
                                printf(" You have inserted 5 cents\n");
                        }
                        else if (coin == 'd' || coin == 'D') {
                                cents = cents + DI;
                                printf("You have inserted 10 cents\n");
                        }
                        else if (coin == 'Q' || coin == 'q') {
                                cents = cents + QU;
                                printf("You have entered 25 cents\n");


                        } else {
                                printf("Unknown coin. Rejected.\n");
                        }

                        int balance = price - cents;
                        printf("You have entered a total of %d cents\n", cents);

                        if (balance > 0) {
                        printf("You must enter %d more cents\n", balance);
                    } else {


                         int change = cents - price;
                         int dimes = change/10;
                         int remainder = change % 10;
                         int nickles = remainder/5;
                         int remainder2= nickles % 5;

                        printf("Change returned. %d nickles and %d dimes",nickles, dimes);
                    }

                    }
                } while (coin != 'E' && coin != 'e');


               printf("DONE!\n");
                return 0;
}
}
}

2 个答案:

答案 0 :(得分:2)

在您的状态:coin != 'E' || coin != 'e'不能是false,因为它至少与Ee不同。

您的意思是coin != 'E' && coin != 'e',或者在特定情况下tolower(e)!='e'

答案 1 :(得分:0)

您的printfscanf,其中提示用户输入硬币并读取值{em>在处理每个硬币的while (cents <= price)循环。这就是你陷入无限循环的原因。

您需要在开始时将这两行移到此循环内。此外,您可能甚至不需要外部do...while循环,因为用户在支付全额金额后输入更多硬币是没有意义的。

           printf(" Welcome to my Vending Machine!\n");
           printf("Pop is %d cents. Please enter nickels, dimes, or quarters\n", price);

           char coin;
           int cents = 0;

           while (cents <= price) {
                printf(" PLease enter a coin [NDQR]\n");
                scanf (" %c", &coin);

                ...
           }
           printf("DONE!\n");

虽然条件(coin != 'E' || coin != 'e')无效,因为它总是会计算为true,但这不是无限循环的来源。如前所述,您根本不需要外部循环。