如果我的循环语句中被跳过用C [英] If statement in my loops are being skipped in C

查看:122
本文介绍了如果我的循环语句中被跳过用C的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在功能验证,我有一个名为大小环路等同于foodSelect第3循环,只是它的工作方式不同的某些原因。它不提示我输入第一个,它会直接到如果它里面并要求什么尺寸(长 - 大,男 - 中,S - 小):请输入S,M或只有L:。它显示了第一个错误,然后再提示我输入。这很奇怪。

来源(因为它是长): http://pastebin.com/raw.php?i = KxrMAXaU

或源位置:

 的#include<&stdio.h中GT;
#包括LT&;&string.h中GT;无效菜单();
无效的问题(字符选择[]);
无效输出(字符* foodChoice,字符* foodSelect,字符* FOODSIZE,为int * foodOrderNum,浮动* foodSubtotal);
INT验证(字符*选择,字符* foodChoice);无效菜单(){
/ *
的printf(\\ n);
* /
    的printf(\\ nWelcome阳光FISH&放大器; CHIPS \\ n \\ n!);
    的printf(########鱼:黑线鳕(K)大(L)| $ 5.00 \\ n);
    的printf(#食品#大比目鱼(T)大(L)| $ 4.00 \\ n);
    的printf(########筹码:切割(C)大(L)| $ 2.00 \\ n);
    的printf(环(R)大(L)| $ 3.00 \\ n);
    的printf(| \\ n);
    的printf(##########软饮料(S)大(L)| $ 2.00 \\ n);
    的printf(##饮料咖啡(C)大(L)| $ 1.75 \\ n);
    的printf(##########茶(T)大(L)| $ 1.50 \\ n);
    的printf(--------------------------------------------- \\ n );
    的printf(注意:中等价格:80 %%的大\\ n);
    的printf(小价格:60 %%的大\\ n);
    的printf(税收是10 %% \\ n);
    的printf(超过5鱼,酒后10 %%的折扣\\ n);
    的printf(每购买10鱼,送1软饮料\\ n);
    的printf( - 饮的大小是根据鱼的\\ n大小);
    的printf(---------------------------------------------- \\ ñ\\ n);
}INT验证(字符*选择,字符* foodChoice)
{
    INT答案,RC = -1;
    如果(==选择命令)
    {
        做{
            回答=的getchar();
            如果(答案=='Y'||回答=='Y')
            {RC = 1; }
            否则,如果(回答=='N'||回答=='N')
            {RC = 0; }
            如果(RC == -1放大器;&安培;!回答= -1)
            {
                的printf(请输入y或n只有:);
                而(回答= -1&安培;!&安培;!回答='\\ n')
                回答=的getchar();
            }
        }而(RC == -1放大器;&安培;!回答= -1);
    }
    如果(选择==foodSelect)
    {
        做{
            回答=的getchar();                如果(foodChoice ==鱼)
                {
                    做{
                        回答=的getchar();
                        如果(回答=='K'||回答=='K')
                        {RC = 1; }
                        否则,如果(回答=='T'||回答=='T')
                        {RC = 0; }
                        如果(RC == -1放大器;&安培;!回答= -1)
                        {
                            的printf(请输入K或只有T:);
                            而(回答= -1&安培;!&安培;!回答='\\ n')
                            回答=的getchar();
                        }
                    }而(RC == -1放大器;&安培;!回答= -1);
                }
                如果(foodChoice ==芯片)
                {
                    做{
                        回答=的getchar();
                        如果(回答=='C'||回答=='C')
                        {RC = 1; }
                        否则,如果(回答=='R'||回答=='R')
                        {RC = 0; }
                        如果(RC == -1放大器;&安培;!回答= -1)
                        {
                            的printf(请输入C或R唯一:);
                            而(回答= -1&安培;!&安培;!回答='\\ n')
                            回答=的getchar();
                        }
                    }而(RC == -1放大器;&安培;!回答= -1);
                }
                如果(foodChoice ==饮料)
                {
                    做{
                        回答=的getchar();
                        如果(回答=='S'||回答=='S')
                        {RC = 1; }
                        否则,如果(回答=='C'||回答=='C')
                        {RC = 2; }
                        否则,如果(回答=='T'||回答=='T')
                        {RC = 3; }
                        如果(RC == -1放大器;&安培;!回答= -1)
                        {
                            的printf(请输入S,C,或只有T:);
                            而(回答= -1&安培;!&安培;!回答='\\ n')
                            回答=的getchar();
                        }
                    }而(RC == -1放大器;&安培;!回答= -1);
                }
        }而(RC == -1放大器;&安培;!回答= -1);
    }
    如果(选择==大小)
    {
        做{
            回答=的getchar();
            如果(回答=='S'||回答=='S')
            {RC = 1; }
            否则如果(回答==M||回答==M)
            {RC = 2; }
            否则,如果(回答=='L'||回答=='L')
            {RC = 3; }
            如果(RC == -1放大器;&安培;!回答= -1)
            {
                的printf(请输入S,M或L只有:);
                而(回答= -1&安培;!&安培;!回答='\\ n')
                回答=的getchar();
            }
        }而(RC == -1放大器;&安培;!回答= -1);
    }
}无效的问题(字符*选择){    字符* choiceYesNo;
    字符* foodOptions;
    字符* foodChoice;
    字符* foodSelect;
    字符* FOODSIZE;
    INT * foodOrderNum;
    浮* foodSubtotal;    开关(选项[0]){
        案例'F':
            foodChoice =鱼;
            foodOptions =(K-黑线鳕,大比目鱼T-);
            打破;
        情况下C:
            foodChoice =筹码;
            foodOptions =(C-切,R-环);
            打破;
        案D:
            foodChoice =饮料;
            foodOptions =(S-软饮料,咖啡C-,T-茶);
            打破;
    }    的printf(\\ n您订购%S(Y / N)?,foodChoice);
        验证(秩序,foodChoice);
    的printf(%的选择%S:foodChoice,foodOptions);
        验证(foodSelect,foodChoice);
    的printf(什么尺寸(长 - 大,男 - 中,S - 小):);
        验证(大小,foodChoice);
    的printf(有多少订单你要(大于= 0)?);
        scanf函数(%d个,&安培; foodOrderNum);
    输出(foodChoice,foodSelect,FOODSIZE,foodOrderNum,foodSubtotal);
}无效输出(字符* foodChoice,字符* foodSelect,字符* FOODSIZE,为int * foodOrderNum,浮动* foodSubtotal){    的printf(\\ n您下令%S:%C - SIZE:%C量下令:%d个,总价格:%.2lf \\ n \\ n
    foodChoice,foodSelect,FOODSIZE,foodOrderNum,foodSubtotal);}诠释主(){    //菜单();    问题(饮料);
}


解决方案

除了事实,你真的不应该比较字符串== (虽然在这情况下,不幸的是没有不可能的,因为你只使用字符串文字工作),你的麻烦是因为选择的进入所需要的<大骨节病>返回键是pressed。因此,当您获得来自输入缓冲器选择一个字符,还有留在这一个换行符。放置

 的printf(答案=%d个\\ N,答案);

后的第一个答案=的getchar(); 中的循环。这将告诉你,你已经获得了新行的'\\ n'(10)或者可能是一个回车'\\ r'(13),但因为以下的原因,相信这不是来自输入缓冲器的情况下。在 foodSelect 循环,你有一个答案=的getchar(); 的if语句,并且一出,立即在之前在do-whil​​e循环的开始,以便清除输入缓冲器。但在 FOODSIZE 分支你没有。

每一个选择后,清除输入缓冲区

 ,而((答案=的getchar())= -1&安培;!&安培;!回答='\\ n)的;

In function verify, I have a loop called "size" which is identical to the 3rd loop in "foodSelect" except that it works differently for some reason. It doesn't prompt me for input first, it goes straight to the if inside of it and asks What size (L - Large, M - Medium, S - Small): Please enter S, M, or L only:. It displays the error first, then prompts me again for input. It's weird.

Source (because it's long): http://pastebin.com/raw.php?i=KxrMAXaU

or source here:

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


}

解决方案

Apart from the fact that you really shouldn't compare the strings with == (although in this case it is unfortunately not unlikely to work because you're only using string literals), your trouble is because the entry of choices needs the return key to be pressed. Thus when you obtain one character for the choice from the input buffer, there is still a newline left in it. Place a

printf("answer = %d\n",answer);

after the first answer = getchar(); in the loop. That will tell you that you have gotten a newline '\n' (10) [or possibly a carriage return '\r' (13), but because of the following, I believe that's not the case] from the input buffer. In the foodSelect loop, you have an answer = getchar(); before the if-statements, and one immediately at the beginning of the do-while loops, so that clears the input buffer. But in the foodSize branch you haven't.

After each choice, clear the input buffer

while((answer = getchar()) != -1 && answer != '\n);

这篇关于如果我的循环语句中被跳过用C的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆