【发布时间】:2014-03-31 22:19:55
【问题描述】:
我是 C 新手,很了解 python,但我在一些基本的事情上苦苦挣扎,这真的很令人沮丧,因为我似乎无法确定我的代码在哪里没有让 while 循环中断,直到它达到数字我假设与缓冲区有关,然后它会满足我程序中的每个条件,有人有一些见解吗?
这是我插入 10 时得到的输出:
Welcome to the Soda Vending Machine
All sodas cost $1.50 each
This machine accepts the following coins:
nickel = 5, dime = 10, quarter = 25, half-dollar = 50, dollar = 100
Please use the numeric value corresponding to each coin
10
Amount depositied so far: $ 32767
Amount depositied so far: $ 32777
Amount depositied so far: $ 32777
Amount depositied so far: $ 32777
Amount depositied so far: $ 32777
Invalid Coin
Dispensing soda ...
Your change is: Half dollar quarter dime nickel
int main(void)
int coin;
int totalcoin;
char *change = "Your change is:" ;
printf("Welcome to the Soda Vending Machine\n===================================\nAll sodas cost $1.50 each\nThis machine accepts the following coins:\nnickel = 5, dime = 10, quarter = 25, half-dollar = 50, dollar = 100\nPlease use the numeric value corresponding to each coin\n");
while (totalcoin < 150)
printf("Deposit a Coin: ");
scanf("%d", &coin);
if ( coin == 5)
totalcoin = totalcoin + 5;
printf("Amount depositied so far: $ %d\n", totalcoin);
if (coin == 10)
totalcoin = totalcoin + 10;
printf("Amount depositied so far: $ %d\n", totalcoin);
if (coin == 25)
totalcoin = totalcoin + 25;
printf("Amount depositied so far: $ %d\n", totalcoin);
if (coin == 50)
totalcoin = totalcoin + 50;
printf("Amount depositied so far: $ %d\n", totalcoin);
if (coin == 100)
totalcoin = totalcoin + 100;
printf("Amount depositied so far: $ %d\n", totalcoin);
if (coin != 5 || coin !=10 || coin !=25 || coin !=50 || coin !=100)
printf("Invalid Coin\n");
printf("Dispensing soda ...\n");
printf("Your change is: ");
if (totalcoin > 50)
totalcoin -= 50;
printf("Half dollar");
if (totalcoin >= 25)
totalcoin -= 25;
printf(" quarter");
if (totalcoin >= 10)
totalcoin -= 10;
printf(" dime");
if (totalcoin >= 5)
totalcoin -= 5;
printf(" nickel");
【问题讨论】:
标签: c string while-loop int main