#include <stdio.h>
#include <iostream>
using namespace std;
float cost, total;
bool loop(char item){
switch (toupper(item)) {
case 'A':
cost = 4.25;
return true;
case 'B':
cost = 5.57;
return true;
case 'C':
cost = 5.25;
return true;
case 'D':
cost = 3.75;
return true;
case 'T':
return false;
}
return true;
}
int main(){
char item;
do {
printf("\nEnter Item Ordered [A/B/C/D] or T to calculate total:");
scanf("%c", &item);
total = total + cost;
} while (loop(item));
printf("Total Cost: $%f\n", total);
}
让我输出过程:
$ ./Case3.o
Enter Item Ordered [A/B/C/D] or T to calculate total:a
Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:b
Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:a
Enter Item Ordered [A/B/C/D] or T to calculate total:
Enter Item Ordered [A/B/C/D] or T to calculate total:t
Total Cost: $28.139999
为什么它在第一次 printf 之后打印printf
两次但第一次跳过我的输入。那么如何计算 5.24+5.57+5.24 等于 28.14?