我对 C 语言相当陌生。这里我的问题是,函数 getDiscount(int ch, float Total)
中的 switch
case 仅读取默认情况并执行代码。我无法识别任何错误,因为它运行正确,但生成了错误的答案。这是我的完整代码。我的 scanf
语句是否获取其 ASCII 值并执行?我只是不知道。
#include<stdio.h>
float getTotalPrice(char type, int qty);
float getDiscount(char mode, float total);
void getGift(float total);
int main(void)
{
int quantity;
float total;
char garment, method;
printf("\nAvailable Garment Types:\n");
printf("Garment Type\tPrice\\u\n");
printf("T-Shirt - 't'\tRs. 1500.00\n");
printf("Frock - 'f'\tRs.3500.00\n");
printf("Skirts - 's'\tRs.2000.00\n");
printf("\nEnter Your Choice: ");
scanf("%c", &garment);
printf("Enter the quantity: ");
scanf("%d", &quantity);
printf("\nAvailable payment methods are:\n");
printf("Cash - 'c'\nCredit Card - 'd'\nOther - 'o'\n");
printf("\nEnter Your Payment Method: ");
scanf("%c", &method);
total = getTotalPrice(garment,quantity);
total = getDiscount(method,total);
getGift(total);
return 0;
}
float getTotalPrice(char type, int qty)
{
float total=0;
switch(type)
{
case 't':
case 'T':
total = 1500*qty;
break;
case 'f':
case 'F':
total = 3500*qty;
break;
case 's':
case 'S':
total = 2000*qty;
break;
default:
printf("\nError: Enter a valid choice\n");
break;
}
return total;
}
float getDiscount(char mode, float total)
{
switch(mode)
{
case 'c':
case 'C':
total = (total - (total*15/100));
break;
case 'd':
case 'D':
total = (total - (total*10/100));
break;
case 'o':
case 'O':
total = (total - (total*5/100));
break;
default:
printf("\nError: Enter a valid payment method\n");
break;
}
return total;
}
void getGift(float total)
{
float gift;
if(total >= 10000)
{
gift = 3000;
}
else if((total >= 5000)&&(total <= 9999))
{
gift = 2000;
}
else if((total < 4999)&&(total > 1))
{
gift = 1000;
}
else
{
printf("\nError: Invalid inputs\n");
}
printf("\nTotal Price = Rs. %.f\n", total);
printf("\nYour gift voucher value = Rs. %.f\n", gift);
}
最佳答案
在此处的 scanf 中添加空格 scanf("%c", &method);
这样它就会变成 scanf("%c", &method);
为什么会发生这种情况?您总是使用前面的 scanf()
的输入来输入 getdiscount
函数,这会破坏您的输入, %c
之前的空格将忽略该输入,从而让您选择正确的字符 - c、d、d 或 o
关于c - 切换大小写菜单未执行,仅读取默认大小写,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43961942/