2016-08-19 41 views
-4

標題提到了問題。我的程序中的scanfs和/或ifs有問題(咖啡廳)

#include<stdio.h> 
#include<conio.h> 
main(){ 
char order, coffee, size, affirm; 
float price; 
clrscr(); 
printf("Hello, welcome to C Coffee Shop. How may I help you?\n"); 
printf(">Buy = B\n>Nothing = N\n"); /* Choose action */ 
scanf("%c",&order); 
if(order == 'B'){ /* Decided to buy coffee */ 
    printf("What coffee would you want for today?\n"); /* Decide coffee type */ 
    printf(">Espresso = E\n>Americano = A\n>Latte = L\n"); 
    scanf("%c",&coffee); 
    printf("How large would your drink be?\n"); /* Decide coffee size */ 
    printf(">Petite = P\n>Regular = R\n>Tall = T\n"); 
    scanf("%c",&size); 
    if((coffee == 'E') && (size == 'P')){ /* Petite Espresso */ 
    price = 35; 
    } 
    else if((coffee == 'E') && (size == 'R')){ /* Regular Espresso */ 
    price = 50; 
    } 
    else if((coffee == 'E') && (size == 'T')){ /* Tall Espresso */ 
    price = 75; 
    } 
    else if((coffee == 'A') && (size == 'P')){ /* Petite Americano */ 
    price = 45; 
    } 
    else if((coffee == 'A') && (size == 'R')){ /*Regular Americano */ 
    price = 65; 
    } 
    else if((coffee == 'A') && (size == 'T')){ /* Tall Americano */ 
    price = 90; 
    } 
    else if((coffee == 'L') && (size == 'P')){ /* Petite Latte */ 
    price = 60; 
    } 
    else if((coffee == 'L') && (size == 'R')){ /* Regular Latte */ 
    price = 85; 
    } 
    else if((coffee == 'L') && (size == 'T')){ /* Tall Latte */ 
    price = 110; 
    } 
    printf("To clarify, your order is %c %c.\nThat would be %0.2f pesos.\n", size, coffee, price); /* Verify order */ 
    printf(">OK = O\n"); /* Affirm */ 
    scanf("%c",&affirm); 
    if(affirm == 'O'){ /* Accept order */ 
    printf("Processing order...\n"); 
    } 
    else{ /* Discard order */ 
    printf("Discarding order...\n"); 
    } 
    printf("Thank you! Please come again."); 
    } 
else if(order == 'N'){ /* Decided not to buy coffee */ 
    printf("Thank you! Please come again."); 
    } 
getche(); 
return 0; 
} 

當我嘗試運行該程序時,第11-16行出現沒有中斷,使我無法執行其他命令。我認爲這是在我的scanf()或if-else語句的錯誤應用中。我不知道如何解決它。請幫助?

if(order == 'B'){ 
    printf("What coffee would you want for today?\n"); 
    printf(">Espresso = E\n>Americano = A\n>Latte = L\n"); 
    scanf("%c",&coffee); 
    printf("How large would your drink be?\n"); 
    printf(">Petite = P\n>Regular = R\n>Tall = T\n"); 
    scanf("%c",&size); 

另請告訴我,如果在我的代碼中有任何其他錯誤。 Tysm那些誰可以幫助:>

+0

請給出您失敗的樣本輸入數據。 – Shahid

+1

[Scanf在C語言中跳過每一個while循環]的可能的重複(http://stackoverflow.com/questions/1669821/scanf-skips-every-other-while-loop-in-c) – melpomene

+1

你真的應該做一個矩陣的價格... –

回答

1

只要你有一個字符輸入如

scanf("%c",&coffee); 

放在格式規範前面的空間,像

scanf(" %c",&coffee); 

這將導致以往任何輸入緩衝區中留有空白,例如newline之前的輸入後,將被跳過,而不是被讀取爲char

0

當您在每個輸入字符後按回車時,新行將被計爲輸入字符。這意味着你必須避免使用這些換行符。您可以通過以下方式執行此操作:

  • 每掃描一次後包含getchar()

  • 或者,將scanf重寫爲scanf("%c%*c",&order)

這是爲了避免將新行字符視爲輸入。

+0

我其實是C的新手......感謝您的幫助! '%* c'確實有效,但它是如何工作的? 另外,如何顯示他們的訂單,以便當他們選擇'美國高'時,下一個'printf'語句將是: '爲了說明,您的訂單是一個高美式。 這將是90比索.' 而不是: '爲了澄清,您的訂單是答: 這將是90比索。 –