C 中循环中的 if 语句被跳过

2024-02-04

在函数验证中,我有一个名为“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 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()反而。

To clarify the above, when you press something like YEnter, you have entered two characters into standard input. The first call to getchar(), will return 'Y', and the next call will return '\n' (the newline character). Your code does not expect this following newline character and may appear to "skip" calls to getchar(), when really it is returning more characters you entered.

如果你使用fgets(),然后你得到entire用户一次性键入的行,包括换行符。您(通常)不必担心输入缓冲区中等待的额外数据。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

C 中循环中的 if 语句被跳过 的相关文章

随机推荐

  • 锦标赛分组放置算法

    给定对手种子列表 例如种子 1 到 16 我正在尝试编写一种算法 该算法将导致头号种子在该轮中对阵最低的种子 第二名种子对阵第二低的种子 依此类推 将 1 和 16 2 和 15 等分组为 比赛 相当容易 但我还需要确保较高的种子将在后续回
  • 使用C#或Powershell扫描所有可用的无线网络并连接到特定的SSID

    我正在尝试编写一个脚本来扫描所有可用的无线网络并连接到特定网络 SSID 有人已经为此编写了示例代码吗 由于某些限制 我无法安装第三方软件 托管 wifi api 查看这篇相关文章 在 C 中管理无线网络连接 https stackover
  • 未捕获的类型错误:无法解析模块说明符“firebase/app”。相对引用必须以“/”、“./”或“../”开头

    我遵循了有关 WebRTC 视频聊天的 YouTube 教程 因此我尝试编写它 在 localhost 中它可以工作 但是当我将其上传到 firebase 托管时 它就会出现此错误 我能做些什么 我是网络开发新手 所以请耐心等待 主要 ht
  • 如何从字符串中删除0

    我正在看函数trim但不幸的是 这并没有删除 0 我该如何将其添加到其中 我应该使用str replace 编辑 我要修改的字符串是一个消息编号 如下所示 00023460 功能ltrim 00023460 0 正是我需要的 显然我不想使用
  • 在 Python 中模拟导入模块

    我正在尝试对使用导入的外部对象的函数实施单元测试 例如助手 py is import os import pylons def some func arg var1 os path exist var2 os path getmtime v
  • 无法使用 OpenCV 从辅助网络摄像头的 VideoCapture 读取帧

    Code 与主网络摄像头 设备 0 完美配合的简单示例 VideoCapture cap 0 if cap isOpened std cout lt lt Unable to read stream from specified devic
  • 为什么我在 raw_input 期间无法捕获 KeyboardInterrupt?

    这是一个测试用例 try targ raw input Please enter target except KeyboardInterrupt print Cancelled print targ 当我按 ctrl c 时 我的输出如下
  • SqlAlchemy - 按关系属性过滤

    我对 SQLAlchemy 没有太多经验 但我遇到了一个无法解决的问题 我尝试搜索并尝试了很多代码 这是我的课程 简化为最重要的代码 class Patient Base tablename patients id Column Integ
  • 在 Bolts 中,如何使用 continueWith() 和 continueWithTask()?

    除了同步与异步之外 它们文档中的差异也让我感到困惑 他们的例子github页面 https github com BoltsFramework Bolts Android chaining tasks together看起来仍然是同步调用的
  • Netbeans - 从数据库生成实体类

    我使用的是 netbeans IDE 7 1 我正在尝试从数据库 sql server 生成实体类 我能够设置与此远程数据源的连接 但在数据库向导的新实体类中 表没有显示 并且在底部显示 选择至少一个表 我可以执行查询并浏览 netbean
  • 如何在Altera Quartus中生成.rbf文件?

    什么是 rbf 文件以及如何在 Windows 上从 Quartus 输出文件 sof 生成它们 An RBF is a 原始二进制文件例如 它代表原始数据 这些数据将被加载到闪存中 以便在上电时初始化 FPGA A SOF is an S
  • 获取数据访问层内的数据库上下文

    我在尝试解决 EF Core 方面的一些问题 我使用 MVC Core 应用程序中的启动代码来初始化数据库上下文 这是我的数据库上下文 public class AccountsDBContext DbContext public Acco
  • 将 UTC java.sql.Time 转换为具有正确 DST 的 java.time.localtime

    我在将从数据库获取的 java sql Time UTC 转换为 java time LocalTime GMT 1 DST 时遇到问题 总是缺少 DST 时间 因此 时间 03 00 仅转换为本地时间 04 00 而不是 05 00 Sa
  • 将 AES IV 存储在数据库中的密文前面是否安全?

    我想将 AES 加密数据存储在数据库字段中 将 AES IV 每行唯一 存储在密文前面是否安全 例如 IV 密文 两者都将以 Base64 进行编码 使用的Key不会存储在数据库中 这是安全的 初始化向量的目的是在生成的密码中插入一些随机性
  • 如何从控制台设置 parpool/matlabpool 中的最大工作人员数量?

    我知道如何使用 Matlab 中的并行首选项窗口更改最大工作人员数量 但我找不到任何有关如何从控制台 代码更改首选项的文档 特别是关于如何更改我可以的最大工作人员数量的文档在 for 循环中使用 任何帮助将不胜感激 你想要的parpool功
  • 在嵌套轨道表单上创建多对多关系

    我正在尝试同时创建组 用户和成员资格 多对多关系 人们可以在创建组时将用户添加到组中 然后我希望它能够路由到包含所有成员的组的视图 我可以将要创建的用户和当前用户的成员资格保存到数据库中 然而 我正在努力获取新创建的 User 对象的 id
  • Apache POI Excel 工作簿创建需要很长时间

    我注意到使用 Apache POI v3 10 例如 xlsx 文件的工作簿创建语句 Workbook wb WorkbookFactory create inputStream or Workbook wb new XSSFWorkboo
  • 命名空间别名的范围是什么?

    在函数定义内定义的 C 命名空间别名是否具有块 函数 文件或其他作用域 有效期 这是区块的有效期 例如 如果您按如下方式定义命名空间别名 则命名空间别名 abc 在外部无效 block namespace abc xyz abc test
  • 使用 PHP 代码的 WordPress Woocommerce 建议

    我正在使用 woo commerece 插件 我想在每个产品的标题下有一个子标题 样式和格式已排序 但我希望在子标题部分中显示特定的类别 我已经设法显示所有类别 但我想将其范围缩小到父类别下的一个类别 下面是我正在使用的代码 任何人都可以建
  • C 中循环中的 if 语句被跳过

    在函数验证中 我有一个名为 size 的循环 它与 foodSelect 中的第三个循环相同 只是由于某种原因它的工作方式不同 它不会先提示我输入 而是直接进入其中的 if 并询问What size L Large M Medium S S