1
我做了一个简单的程序,问你是否可以进入酒吧,如果你的年龄。然后继续问你愿意花多少钱,并在询问你第一次购买多少钱后。它继续询问您是否想要购买更多,但是如果我不想购买更多,我可以输入值“0”,但是当输入“0”时它不会关闭程序。当我按0时,为什么我的程序不能停止? C程序
的代码如下:
#include <stdio.h>
int main() {
int age, legal = 18;
float money, buy;
printf("At the bar the security must ID the customers that look underage, what is your age?: ");
scanf("%d", &age);
if(age < legal){
printf("\nYou are not allowed!\n");
}
else if(age >= legal){
printf("\nYou may enter, and buy whatever you wish!\n");
printf("\nHow much money are you willing to spend tonight?: ");
scanf("%f", &money);
printf("\nWith the %.2f dollars you want to spend tonight what is the price of food or beverage you wish to buy first?: ", money);
scanf("%f", &buy);
money = money - buy;
while(money > 0 || buy==0) {
printf("\nYou now have %.2f dollars remaining, what else would you like to buy? (Press 0 to stop if you don't wish to buy any more!!): ", money);
scanf("%f", &buy);
money = money - buy;
}
if(money == 0){
printf("\nYou have spent all the money that you wished to spend at the bar, we hope you enjoyed your stay!!\n");
}
else if(money > 0){
printf("\nYou still have %.2f dollars left, We hope you enjoyed your stay at the bar!!\n");
}
}
}
请避免使用'float'或'double'作为金钱,因为它们会给出不精确的比较。使用'int'工作在美分,格式为* dollars.cents *作为输出。拆分'.'上的任何输入字符串将其转换为分。 –