【问题标题】:C code stuck in infinite do-while loopC 代码卡在无限的 do-while 循环中
【发布时间】:2017-04-04 22:04:22
【问题描述】:

我正在尝试模拟自动售货机。我的程序将命令行输入的金额作为价格,然后继续提示,直到程序中止。我试图通过一个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;
}
}
}

【问题讨论】:

  • coin != 'E' || coin != 'e' 始终为真。
  • 我怎样才能做到这一点,以便它循环直到输入 E?
  • 在倒数第二个printf 中,拼写错误:将“nickles”更改为“nickels”。

标签: c debugging while-loop do-while


【解决方案1】:

在您的 while 条件下:coin != 'E' || coin != 'e' 不能是 false,因为它至少不同于 Ee 之一。

你的意思是coin != 'E' &amp;&amp; coin != 'e',或者在那种特殊情况下tolower(e)!='e'

【讨论】:

  • ... 或 !(coin = 'E' || coin = 'e')
  • 或者那个,是的。或tolower(e)=='e'
【解决方案2】:

您的printfscanf 提示用户输入硬币并读取值在处理每个硬币的while (cents &lt;= 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') 是无效的,因为它总是评估为真,但这不是无限循环的来源。如前所述,您根本不需要那个外循环。

【讨论】:

  • 我需要程序能够输入更多硬币,即使他们已经支付了全部金额作为额外销售的手段
  • @JVAN 这是for循环遍历命令行输入的每个价格的结果。
  • 是的,但该循环只设置一个价格。所以基本上机器有一种流行音乐,用户可以为这种流行音乐设定价格。一旦用户设置了价格,比如说 40 美分,那么在程序中止之前,价格将始终为 40 美分。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-05
  • 1970-01-01
  • 2022-01-21
相关资源
最近更新 更多