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

C代码卡在无限do while循环中,c,debugging,while-loop,do-while,C,Debugging,While Loop,Do While,我在试着模拟自动售货机。我的程序从命令行获取输入的金额作为价格,然后继续提示,直到程序中止。我试图通过一个whiledo循环来实现这一点,我现在知道这肯定是错误的。我需要程序继续提示输入硬币,即使他们已经插入了硬币的写入量 代码的外观示例如下: $-表示命令行 $ 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 Mac

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

代码的外观示例如下: $-表示命令行

$ 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;
}
}
}
#包括
#包括
#包括
#定义镍5
#定义DI 10
#定义曲25
bool是有效的(int num){

在while条件下返回(num>=10&&num:
coin!=“E”| coin!=“E”
不能为
false
,因为它至少与
E
E
中的一个不同


你的意思是
coin!=“E”&&coin!=“E”
,或者在那种特殊情况下
tolower(E)!=“E”
在你的while条件下:
coin!=“E”=“E”
不能是
假的,因为它至少不同于
E
E
中的一个


你的意思是
coin!=“E”&&coin!=“E”
,或者在这种特殊情况下
tolower(E)!=“E”
你的
printf
scanf
提示用户输入一枚硬币,并读取的值在
之外(美分您的
printf
scanf
提示用户输入硬币并读取数值的位置在
之外,而(美分
coin!=“E”| coin!=“E”
始终为真。我如何做到这一点,使其循环直到输入E为止?请查看您倒数第二个
printf
,拼写错误:更改“镍币”到“镍币”
coin!='E'| | coin!='E'
总是正确的。我如何才能这样做,直到输入E为止循环?看看倒数第二个
printf
,拼写错误:将“镍币”更改为“镍币”…或者!(coin='E'| coin='E')或者那样,是的。或者
tolower E='E
…或者!(coin='E'| | coin='E')或者是的。或者
tolower(E)=='e'
我需要该程序能够输入更多的硬币,即使他们已经支付了全部金额,作为额外支付的手段sales@JVAN这是因为
for
循环遍历了在命令行中输入的每个价格。是的,但该循环只设置了一个价格。因此,基本上,机器有一种pop类型和用途r开始为这一次流行音乐设定价格。一旦用户设定价格,比如说40美分,那么在该程序中止之前,价格始终是40美分。我需要该程序能够输入更多的硬币,即使他们支付了全部金额,作为额外支付的手段sales@JVAN这是由于
for
循环的结果t遍历在命令行上输入的每个价格。是的,但该循环仅设置一个价格。因此,基本上机器有一种pop,用户可以为该pop设置价格。一旦用户设置价格,比如说40美分,那么价格将始终为40美分,直到程序中止。
           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");