-1

Write a program that will prompt the user to enter the price of an article and a pricing code. Your program should calculate the new discounted price and validate the input.

EVERYTHING IN THIS CODE IS CORRECT BUT WHEN I RUN THE COMPILER, I ENTER 100 FOR THE PRICE, BUT THEN THE CURSOR DOESNT STOP ON THE PRICING CODE AND I AM THEN INCAPABLE OF ENTERING IN MY DESIRED PRICING CODE? WHY DOES THE CURSOR NOT STOP AFTER 'PRICING CODE' ???

#include <stdio.h>
main()
{
float price, discp;
char code;
printf("Price Calculator");
printf("\n\n========================");
printf("\n\nEnter the price: $ ");
scanf("%f", &price);
printf("\n\nEnter the pricing code:  ");
scanf("%c", &code);
if ((code=='C')||(code=='c'))
{
    discp=price-0.3*price;
    printf("\n\n\nNew discounted price is $%.2f\n\n\n\n\n\n\n", discp);
}
if ((code=='A')||(code=='a'))
{
    discp=price-0.5*price;
    printf("\n\n\nNew discounted price is $%.2f\n\n\n\n\n\n\n", discp);
}
if ((code=='B')||(code=='b'))
{
    discp=price-0.4*price;
    printf("\n\n\nNew discounted price is $%.2f\n\n\n\n\n\n\n", discp);
}
if ((code=='D')||(code=='d'))
{
    discp=price-0.1*price;
    printf("\n\n\nNew discounted price is $%.2f\n\n\n\n\n\n\n", discp);
}
if ((code=='E')||(code=='e'))
{
    discp=price;
    printf("\n\n\nNew discounted price is $%.2f\n\n\n\n\n\n\n", discp);
}
else 
{
    printf("\n\n\nInvalid Pricing Code\n\n\n\n\n\n\n", discp);
}
}
gadoora
  • 1
  • 1
  • 1

1 Answers1

0

The problem that you're experiencing is that scanf() only grabs a single character from the input buffer. This would be fine, except that there's still a newline left in the input buffer that resulted from hitting 'enter' after your input.. You need to clear the input buffer after you use scanf() like this:

do

scanf("%c", &code);
while(code == '\n'){scanf("%c", &code);}

or flash the buffer

No Idea For Name
  • 11,411
  • 10
  • 42
  • 70