2013-08-19 60 views
-1

編寫一個程序,該程序將提示用戶輸入物品的價格和定價代碼。您的計劃應計算新的折扣價格並驗證輸入。從具有價格代碼的文章中找到折扣價格

本代碼中的一切都是正確的,但是當我運行編譯器時,我輸入了100的價格,但是然後這個CURSOR沒有停止在定價代碼上,我不能輸入我想要的定價代碼?爲什麼在'定價代碼'之後CURSOR不停止?

#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); 
} 
} 

回答

0

您遇到的問題是scanf()只能從輸入緩衝區中抓取單個字符。這將是罰款,但還是有一個換行符在起因於打你輸入後,「ENTER」鍵輸入緩衝區離開..你需要使用scanf()這樣後清除輸入緩衝區:

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

或閃存緩衝區

相關問題