简体   繁体   English

C代码陷入无限的do-while循环

[英]C code stuck in infinite do-while loop

I'm trying to simulate a vending machine. 我正在尝试模拟自动售货机。 My program takes an inputted amount from the command line as the price and then continues to prompt until the program is aborted. 我的程序从命令行输入一个输入的金额作为价格,然后继续提示直到该程序中止。 I'm trying to achieve this through a while-do loop which I now know must be wrong. 我正在尝试通过while-do循环实现这一目标,现在我知道这肯定是错误的。 I need the program to continue to prompt to enter a coin, even after they have inserted the write amount of coins. 我需要该程序继续提示输入硬币,即使它们已插入硬币的写入量后也是如此。

An example of how the code should look like complied is this: $ - represents command line 代码看起来应像这样的示例如下:$-表示命令行

$ 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]

See how at the end it goes back to being able to enter a coin, until E is selected to exit. 看看最后如何回到可以输入硬币的状态,直到选择E退出为止。 I'm trying to do this however, my code is currently stuck in an infinite loop upon initial input of N,D, or Q. Would appreciate any help or guidance. 我正在尝试执行此操作,但是我的代码当前在初始输入N,D或Q时陷入无限循环。不胜感激任何帮助或指导。 Thank you! 谢谢!

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

In your while condition: coin != 'E' || coin != 'e' 在您的条件下: coin != 'E' || coin != 'e' coin != 'E' || coin != 'e' cannot be false , since it's at least different from one of E or e . coin != 'E' || coin != 'e'不能为false ,因为它至少与Ee之一不同。

You mean coin != 'E' && coin != 'e' , or in that particular case tolower(e)!='e' 您的意思是coin != 'E' && coin != 'e' ,或者在特定情况下要降低tolower(e)!='e'

Your printf and scanf where you prompt the user for a coin and read the value is outside of your while (cents <= price) loop which processes each coin. 您的printfscanf会提示用户输入硬币并读取该值,该值不在处理每个硬币的while (cents <= price)循环中。 That's why you get stuck in an infinite loop. 这就是为什么您陷入无限循环的原因。

You need to move these two lines inside of this loop at the start. 您首先需要在此循环内移动这两行。 Also, you probably don't even need the outer do...while loop, since there's no point in the user entering more coins after they've paid the full amount. 另外,您甚至可能不需要外部的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");

While the condition (coin != 'E' || coin != 'e') is invalid as it will always evaluate to true, that's not the source of the infinite loop. 虽然条件(coin != 'E' || coin != 'e')是无效的,因为它将始终评估为true,但这不是无限循环的根源。 As previously mentioned, you don't need that outer loop at all. 如前所述,您根本不需要该外部循环。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM