c - 如何让我的 "Main"函数以正确的顺序执行?

标签 c function

我正在尝试执行条件循环和 if-else 循环 然而,在这个问题上,我的主要功能没有按照我预期的顺序执行步骤。

#include<stdio.h>
    void draft(int, char);
    void main()
    {
        int age = 0;
        char sex;
        printf("How old are you?");
        scanf_s("%d", &age);
        printf("Please enter your sex.(M or F)");
        scanf_s("%c", &sex);
        draft(age,sex);
        system("pause");
        return;
    }
    void draft(int age,char sex)
    {
        if (age>= 21 && sex=="M")
        {
            printf("Congratulations son, You will be going off to Syria to fight for your country.\n");
        }
        else if (age >= 18 && sex == "M")
        {
            printf("Congratulations son, You will be going off to Vietnam to fight for your country.\n");
        }
        else if (age < 18 && sex == "M")
        {
            printf("Sorry Son you're still too young.\n");
        }
        else if (age >= 21 && sex == "F")
        {
            printf("Sorry,miss, only men can serve.\n");
        }
        else if (age >= 18 && sex == "F")
        {
            printf("Sorry,little lady, only men can serve.\n");
        }
        else if (age < 18 && sex == "F")
        {
            printf("Sorry,little girl, only men can serve.\n");
        }
        else
        {
            printf("Please enter age and sex.");
        }
        return;
    }

系统会提示用户输入年龄,但输入后会直接转到“草稿”函数的最后一个 ELSE 语句,不给用户输入性别的机会。

最佳答案

问题是您正在使用 scanf 来解析来自标准输入的输入。但是,这只是将其视为流。如果您在第一个输入中输入类似 32F 的内容,您的程序就会真正运行。

[为清楚起见进行编辑:您的程序实际上并没有奇怪地跳跃,发生的是第二个 scanf 立即返回来自标准输入的数据,而第一个扫描没有读取这些数据。在这种情况下,它是用户按下回车键时的返回字符。所以它立即返回一个不是你的'M'或'F'的值,因此它运行最后的else。没有“乱序”发生]

注意:您的 draft() 函数中还有第二个问题。您使用 "M""F" 是字符串(字符数组),而实际上您需要使用 'M''F' 是单个字符。

你想要的是一次读取一行并解析它。所以我建议进行以下更改。我已将 scanfs 替换为 fgets,它将读取一行。然后使用 sscanf 解析该行。

#include<stdio.h>

void draft(int, char);
int main()
{
    int age = 0;
    char sex;
    char line[100] = {0};

    printf("How old are you?");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%d", &age);
    printf("Please enter your sex.(M or F)");
    fgets(line, sizeof(line), stdin);
    sscanf(line, "%c", &sex);
    draft(age,sex);

    return 0;
}
void draft(int age,char sex)
{
    if (age>= 21 && sex=='M')
    {
        printf("Congratulations son, You will be going off to Syria to fight for your country.\n");
    }
    else if (age >= 18 && sex == 'M')
    {
        printf("Congratulations son, You will be going off to Vietnam to fight for your country.\n");
    }
    else if (age < 18 && sex == 'M')
    {
        printf("Sorry Son you're still too young.\n");
    }
    else if (age >= 21 && sex == 'F')
    {
        printf("Sorry,miss, only men can serve.\n");
    }
    else if (age >= 18 && sex == 'F')
    {
        printf("Sorry,little lady, only men can serve.\n");
    }
    else if (age < 18 && sex == 'F')
    {
        printf("Sorry,little girl, only men can serve.\n");
    }
    else
    {
        printf("Please enter age and sex.");
    }
    return;
}

关于c - 如何让我的 "Main"函数以正确的顺序执行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33071346/

相关文章:

c - 为 ARM 交叉编译时 header 子目录错误

c - 从字符串中提取数字 C

c - 输入某些值时如果出错则嵌套

javascript - 来自另一个外部 javascript 文件的外部 Javascript 调用函数

postgresql - Postgres 存储函数如何返回表

javascript - Keypress 上的两个功能不起作用(文本区域)

javascript - 如何通过匿名函数为对象属性赋值?

c - 列出目录中的文件并在 C/C++ 中删除它们

c - 在 STM32 - C 上处理图像

c - 使用带有动态数组的结构指针的内存分配 (realloc) 时出错