0

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);
}
Nisse Engström
  • 4,738
  • 23
  • 27
  • 42
  • 1
    have you tried debugging it? did you check the value of mode when entering the function? – CIsForCookies May 14 '17 at 08:38
  • it's very simple. your function gets an argument "mode". before your switch case, add a "printf("%c", mode);" (and maybe add while(1); to halt the execution) and that will show you the value... or just debug using GDB or other debugging tools – CIsForCookies May 14 '17 at 08:47
  • 1
    Possible duplicate of [Simple C scanf does not work?](http://stackoverflow.com/questions/3744776/simple-c-scanf-does-not-work) – Martin R May 14 '17 at 08:55

1 Answers1

0

add whitespace in your scanf here scanf("%c", &method); so it will become scanf(" %c", &method);

why is this happeneing? you always enter the getdiscount function with the enter of the previous scanf() and that's ruining your input, the whitespace before the %c will ignore that enter and thus let you choose the correct char - c, d,d or o

CIsForCookies
  • 12,097
  • 11
  • 59
  • 124