在函数验证中,我有一个名为“size”的循环,它与“foodSelect”中的第三个循环相同,只是由于某些原因它的工作方式不同。它不会首先提示我输入,而是直接进入其中的 if 并询问 What size (L - Large, M - Medium, S - Small): Please enter S, M, or L only:
。它首先显示错误,然后再次提示我输入。这很奇怪。
来源(因为它很长):http://pastebin.com/raw.php?i=KxrMAXaU
或来源:
#include <stdio.h>
#include <string.h>
void menu();
void question (char choice[]);
void output(char *foodChoice, char *foodSelect, char *foodSize, int *foodOrderNum, float *foodSubtotal);
int verify(char *choice, char *foodChoice);
void menu() {
/*
printf("\n");
*/
printf("\nWelcome to Sunny FISH & CHIPS!\n\n");
printf("######## Fish : Haddock(K) Large(L) | $5.00\n");
printf("# FOOD # Halibut(T) Large(L) | $4.00\n");
printf("######## Chips: Cut(C) Large(L) | $2.00\n");
printf(" Ring(R) Large(L) | $3.00\n");
printf(" | \n");
printf("########## Soft Drinks(S) Large(L) | $2.00\n");
printf("# DRINKS # Coffee(C) Large(L) | $1.75\n");
printf("########## Tea(T) Large(L) | $1.50\n");
printf("---------------------------------------------\n");
printf("Note: Medium price: 80%% of large.\n");
printf(" Small price: 60%% of large.\n");
printf("TAX is 10%%.\n");
printf("More than 5 fish, 10%% discount on drink.\n");
printf("Every 10 fish purchased, get 1 free softdrink.\n");
printf(" - size of drink is according to size of fish\n");
printf("----------------------------------------------\n\n");
}
int verify(char *choice, char *foodChoice)
{
int answer, rc = -1;
if (choice == "order")
{
do {
answer = getchar();
if (answer == 'Y' || answer == 'y')
{ rc = 1; }
else if (answer == 'N' || answer == 'n')
{ rc = 0; }
if (rc == -1 && answer != -1)
{
printf("Please enter y or n only: ");
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
if (choice == "foodSelect")
{
do {
answer = getchar();
if (foodChoice == "Fish")
{
do {
answer = getchar();
if (answer == 'K' || answer == 'k')
{ rc = 1; }
else if (answer == 'T' || answer == 't')
{ rc = 0; }
if (rc == -1 && answer != -1)
{
printf("Please enter K or T only: ");
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
if (foodChoice == "Chips")
{
do {
answer = getchar();
if (answer == 'C' || answer == 'c')
{ rc = 1; }
else if (answer == 'R' || answer == 'r')
{ rc = 0; }
if (rc == -1 && answer != -1)
{
printf("Please enter C or R only: ");
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
if (foodChoice == "Drinks")
{
do {
answer = getchar();
if (answer == 'S' || answer == 's')
{ rc = 1; }
else if (answer == 'C' || answer == 'c')
{ rc = 2; }
else if (answer == 'T' || answer == 'T')
{ rc = 3; }
if (rc == -1 && answer != -1)
{
printf("Please enter S, C, or T only: ");
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
} while (rc == -1 && answer != -1);
}
if (choice == "size")
{
do {
answer = getchar();
if (answer == 'S' || answer == 's')
{ rc = 1; }
else if (answer == 'M' || answer == 'm')
{ rc = 2; }
else if (answer == 'L' || answer == 'l')
{ rc = 3; }
if (rc == -1 && answer != -1)
{
printf("Please enter S, M, or L only: ");
while (answer != -1 && answer != '\n')
answer = getchar();
}
} while (rc == -1 && answer != -1);
}
}
void question (char *choice) {
char *choiceYesNo;
char *foodOptions;
char *foodChoice;
char *foodSelect;
char *foodSize;
int *foodOrderNum;
float *foodSubtotal;
switch (choice[0]) {
case 'f':
foodChoice = "Fish";
foodOptions = "(K- Haddock, T- Halibut)";
break;
case 'c':
foodChoice = "Chips";
foodOptions = "(C- Cut, R- Ring)";
break;
case 'd':
foodChoice = "Drinks";
foodOptions = "(S- Softdrink, C- Coffee, T- Tea)";
break;
}
printf("\nDo you order %s? (Y/N): ", foodChoice);
verify("order", foodChoice);
printf("%s choice %s: ", foodChoice, foodOptions);
verify("foodSelect", foodChoice);
printf("What size (L - Large, M - Medium, S - Small): ");
verify("size", foodChoice);
printf("How many orders do you want? (>=0): ");
scanf("%d", &foodOrderNum);
output(foodChoice, foodSelect, foodSize, foodOrderNum, foodSubtotal);
}
void output(char *foodChoice, char *foodSelect, char *foodSize, int *foodOrderNum, float *foodSubtotal) {
printf("\nYou ordered %s: %c - SIZE: %c amount ordered: %d, subtotal price: %.2lf\n\n",
foodChoice, foodSelect, foodSize, foodOrderNum, foodSubtotal);
}
int main() {
//menu();
question("drinks");
}
最佳答案
我不建议将 getchar()
用于交互式程序。您似乎没有处理在您键入所有内容后将出现的换行符。请改用 fgets()
。
为了澄清以上内容,当您按 YEnter 之类的键时,您已经在标准输入中输入了 两个 个字符。第一次调用 getchar()
,将返回 'Y'
,下一次调用将返回 '\n'
(换行符) .您的代码不需要此后跟的换行符,并且可能看起来“跳过”对 getchar()
的调用,而实际上它返回的是您输入的更多字符。
如果您使用 fgets()
,您将一次性获取用户键入的整行,包括换行符。您(通常)不必担心输入缓冲区中等待的额外数据。
关于c - 如果我的循环中的语句在 C 中被跳过,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9880665/