标题说明了问题。
#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给那些有帮助的人:&gt;
答案 0 :(得分:1)
只要您有字符输入,例如
scanf("%c",&coffee);
在格式规范前加一个空格,比如
scanf(" %c",&coffee);
这将导致输入缓冲区中留下的任何前一个空格(例如前一个输入后的newline
)被跳过,而不是被char
读取。
答案 1 :(得分:0)
当您在每个输入字符后按Enter键时,新行将被计为输入字符。这意味着你必须避免这些新的行字符。您可以通过以下方式执行此操作:
每次扫描后包含maven-antrun-plugin
。
或者,将scanf重写为getchar()
。
这样可以避免将新行字符计算为输入。