如何使“Main”功能按正确的顺序执行?

时间:2022-01-27 01:13:15

I'm trying to do a conditional and if-else loop on this problem however, my main function isn't executing the steps in the order I had intended.

我试图在这个问题上做一个条件和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;
    }

The user is prompted for his age but after that is entered it will go directly to the last ELSE statement of the "draft" function without giving the user the opportunity to enter the sex.

用户被提示他的年龄,但在输入之后,它将直接进入“草稿”功能的最后一个ELSE语句,而不给用户输入性别的机会。

3 个解决方案

#1


1  

The problem is that you are using scanf to parse input from stdin. However this just treats it as a stream. Your program actually works if you enter something like 32F at the first input.

问题是你使用scanf来解析stdin的输入。然而,这只是将其视为流。如果您在第一次输入时输入类似32F的内容,您的程序实际上是有效的。

[Edit for clarity: You program is not actually jumping oddly, what is happening is that the second scanf returns immediately with data from stdin which wasn't read with the first one. In this case it is the return character from when user pressed enter key. So it returns immediately with a value that is not your 'M' or 'F', hence it runs the final else. There is no "out of order" happening]

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

Note: There is a second problem in your draft() function. You use "M" and "F" which are strings (character arrays), whereas you actually need to use 'M' and 'F' which are single characters.

注意:draft()函数中存在第二个问题。您使用“M”和“F”作为字符串(字符数组),而您实际上需要使用单个字符的“M”和“F”。

What you are wanting is to read a single line and parse it at a time. So I suggest the following change. I have replaced the scanfs with a fgets which will read a single line. Then use sscanf to parse that line.

你想要的是阅读一行并一次解析它。所以我建议进行以下更改。我用一个读取单行的fgets替换了scanfs。然后使用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;
}

#2


1  

Two issues:

两个问题:

After the first call scanf_s, a newline is left in the buffer. That newline is immediately picked up by the second scanf_s. You need to change the pattern to match and discard any newlines by putting a space before %d and %c. Also, you should check the return value to ensure that a value matching the pattern was read:

在第一次调用scanf_s之后,在缓冲区中留下换行符。第二个scanf_s会立即拾取该换行符。您需要通过在%d和%c之前放置一个空格来更改模式以匹配并丢弃任何换行符。此外,您应检查返回值以确保读取与模式匹配的值:

printf("How old are you?");
if (scanf_s(" %d", &age) != 1) {
    printf("You must enter a value age.\n");
    exit(1);
}
printf("Please enter your sex.(M or F)");
if (scanf_s(" %c", &sex) != 1) {
    printf("You must enter a value age.\n");
    exit(1);
}

In draft, you're using double quotes instead of single quotes for a character literal:

在草稿中,您使用双引号而不是单引号作为字符文字:

if (age>= 21 && sex=="M")

Here, "M" is a string containing one character plus a NULL terminator. What you want is 'M', which is the character M. So change the above line to:

这里,“M”是包含一个字符加上NULL终止符的字符串。你想要的是'M',这是角色M.所以将上面的行改为:

if (age>= 21 && sex=='M')

And make a similar fix to the five other lines with the same issue.

并对具有相同问题的其他五条线进行类似的修复。

#3


0  

Well, this is a problem:

嗯,这是一个问题:

scanf_s("%c", &sex);

Unlike conversion specifiers like %s and %d, the %c specifier does not cause scanf (and, I'm assuming, scanf_s) to skip over any leading whitespace in the input stream. So it immediately picks up the newline character after your first input, hence why it doesn't wait for you to enter the sex.

与%s和%d等转换说明符不同,%c说明符不会导致scanf(我假设,scanf_s)跳过输入流中的任何前导空格。因此它会在您第一次输入后立即获取换行符,因此它不会等待您输入性别。

To fix that, put a blank space before the conversion specifier:

要解决此问题,请在转换说明符前添加一个空格:

scanf_s(" %c", &sex );

Another problem is your comparisons sex == "M" and sex == "F"; sex is a single char, but "M" and "F" are character strings. Use the comparisons sex == 'M' and sex == 'F' (single quotes, not double quotes).

另一个问题是你的比较性别==“M”和性别==“F”;性是一个字符,但“M”和“F”是字符串。使用比较性别=='M'和性别=='F'(单引号,而不是双引号)。

#1


1  

The problem is that you are using scanf to parse input from stdin. However this just treats it as a stream. Your program actually works if you enter something like 32F at the first input.

问题是你使用scanf来解析stdin的输入。然而,这只是将其视为流。如果您在第一次输入时输入类似32F的内容,您的程序实际上是有效的。

[Edit for clarity: You program is not actually jumping oddly, what is happening is that the second scanf returns immediately with data from stdin which wasn't read with the first one. In this case it is the return character from when user pressed enter key. So it returns immediately with a value that is not your 'M' or 'F', hence it runs the final else. There is no "out of order" happening]

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

Note: There is a second problem in your draft() function. You use "M" and "F" which are strings (character arrays), whereas you actually need to use 'M' and 'F' which are single characters.

注意:draft()函数中存在第二个问题。您使用“M”和“F”作为字符串(字符数组),而您实际上需要使用单个字符的“M”和“F”。

What you are wanting is to read a single line and parse it at a time. So I suggest the following change. I have replaced the scanfs with a fgets which will read a single line. Then use sscanf to parse that line.

你想要的是阅读一行并一次解析它。所以我建议进行以下更改。我用一个读取单行的fgets替换了scanfs。然后使用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;
}

#2


1  

Two issues:

两个问题:

After the first call scanf_s, a newline is left in the buffer. That newline is immediately picked up by the second scanf_s. You need to change the pattern to match and discard any newlines by putting a space before %d and %c. Also, you should check the return value to ensure that a value matching the pattern was read:

在第一次调用scanf_s之后,在缓冲区中留下换行符。第二个scanf_s会立即拾取该换行符。您需要通过在%d和%c之前放置一个空格来更改模式以匹配并丢弃任何换行符。此外,您应检查返回值以确保读取与模式匹配的值:

printf("How old are you?");
if (scanf_s(" %d", &age) != 1) {
    printf("You must enter a value age.\n");
    exit(1);
}
printf("Please enter your sex.(M or F)");
if (scanf_s(" %c", &sex) != 1) {
    printf("You must enter a value age.\n");
    exit(1);
}

In draft, you're using double quotes instead of single quotes for a character literal:

在草稿中,您使用双引号而不是单引号作为字符文字:

if (age>= 21 && sex=="M")

Here, "M" is a string containing one character plus a NULL terminator. What you want is 'M', which is the character M. So change the above line to:

这里,“M”是包含一个字符加上NULL终止符的字符串。你想要的是'M',这是角色M.所以将上面的行改为:

if (age>= 21 && sex=='M')

And make a similar fix to the five other lines with the same issue.

并对具有相同问题的其他五条线进行类似的修复。

#3


0  

Well, this is a problem:

嗯,这是一个问题:

scanf_s("%c", &sex);

Unlike conversion specifiers like %s and %d, the %c specifier does not cause scanf (and, I'm assuming, scanf_s) to skip over any leading whitespace in the input stream. So it immediately picks up the newline character after your first input, hence why it doesn't wait for you to enter the sex.

与%s和%d等转换说明符不同,%c说明符不会导致scanf(我假设,scanf_s)跳过输入流中的任何前导空格。因此它会在您第一次输入后立即获取换行符,因此它不会等待您输入性别。

To fix that, put a blank space before the conversion specifier:

要解决此问题,请在转换说明符前添加一个空格:

scanf_s(" %c", &sex );

Another problem is your comparisons sex == "M" and sex == "F"; sex is a single char, but "M" and "F" are character strings. Use the comparisons sex == 'M' and sex == 'F' (single quotes, not double quotes).

另一个问题是你的比较性别==“M”和性别==“F”;性是一个字符,但“M”和“F”是字符串。使用比较性别=='M'和性别=='F'(单引号,而不是双引号)。