C-R
C-R

Reputation: 13

C programming error if statement

i have this program and it's working fine until the very end. When I ask the user if he/she wants to do a discount and the user enters y/n, the program just halts and doesn't go to the if statement. Any help? Thanks :)

int calculate(){

printf("Do you want to make a discount? y/n \n");
scanf("%c",&ask);

if (ask == 'y')
{
    printf("Enter the actual money that you want to make a discount\n");
    scanf("%f",&disc);
    discount= (total * (disc/100));
    net= (total - discount);
    printf("The total bill is: %.2f\n", net);


}
else
if(ask == 'n')
{
        printf("The total bill is: %.2f\n", total);


    }
//menu();
return 0;

}

Upvotes: 1

Views: 76

Answers (1)

Iharob Al Asimi
Iharob Al Asimi

Reputation: 53036

Change this

scanf("%c",&ask);

to

scanf(" %c",&ask);

to explicitly ignore any left over white space.

Upvotes: 3

Related Questions