I'm fairly new to the C language. Here my question is that the switch
case in the function getDiscount(int ch, float total)
only reads the default case and execute the code. I can't identify any errors as it runs correctly, but is generating a wrong answer. Here's my complete code. Does my scanf
statements take their ASCII values and execute? I just have no idea.
#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);
}