【问题标题】:Defining functions that return a char used later in the main program定义返回 char 的函数,稍后在主程序中使用
【发布时间】:2014-12-03 20:45:43
【问题描述】:

我在调用函数和稍后在主程序中再次使用它们时遇到了很多麻烦。我还没有找到深入的答案来解释为什么它不运行。我知道参数属于被调用函数括号内,但我希望用户输入在被调用程序中开始。这甚至可能吗?从理论上讲,该函数会询问用户一年,检查它是否在某些参数范围内,然后将其返回到我希望最终能够将其存储在数组中的主函数。现在,有人可以告诉我如何在这个初级程序中完成这项工作吗?提前谢谢!

#include <stdio.h>

char year_info();

int main(void)
{
    int menu_selection;
    char year;

    printf("Please choose from the following menu: \n1. Insert a new movie\n2. Show movie\n3. List all\n4. Exit\n");
    scanf("%i", &menu_selection);
    switch (menu_selection)
    {
        case 1: year = year_info();
                printf("%c", year);
                break;
    }
}

char year_info()
{
    int year_input;
    printf("\nYear: ");
    scanf("%i", &year_input);
    if (year_input > 2016 || year_input < 1920)
    {
        printf("Sorry, I do not recognize this command. Please try again.\n");
    } 
    else
    {
        int year = year_input;
        return year;
    }
}

【问题讨论】:

  • A char (通常)是一个 8 位整数。根据编译器的不同,它可以取 -128 到 127 或 0 到 255 之间的值。scanf 格式 "%i" 读入 int 变量,int 通常是 32位宽,从负 20 亿到正 20 亿。当然,您会发现尝试将 1 或 2000 之类的值放入无法容纳那么大值的变量时会出现一些问题。
  • char year_info(); --> int year_info();, char year_imput; --> int year_imput;, scanf("%i", year_imput); --> scanf("%i", &amp;year_imput); 并且存在不返回值的路径。
  • 那么我应该改用 %d 吗?
  • 除了上述问题(和其他问题)之外,在调用函数之前声明原型是可以的,只要您在程序中的其他位置定义函数。

标签: c function parameters return-value command-line-arguments


【解决方案1】:

它没有运行,因为您传递了scanf 变量,但您应该传递变量的地址,即使用:

scanf("%i", &something);

而不是scanf("%i", something);


另外,正如其他人指出的那样,您将charint 混用得太随意了,所以它不会按预期工作。

yearyear_imput 不能是字符,因为它们的值不够大,您至少需要一个短字符。

【讨论】:

    【解决方案2】:

    您有 2 个错误。

    scanf("%i", &menu_selection);
    
    
    scanf("%i", &year_imput);
    

    您需要使用 & 将变量的地址传递给scanf()

    编辑:但是,我会使用整数,因为scanf("%c", &amp;something) 只会识别您输入的第一个字符,而不是整个字符串,即使发生这种情况您也可以'不要在字符串之间做if (year_imput &gt; 2016 || year_imput &lt; 1920),你可以用字符来做,但同样,它们只能存储一个字符,所以我会像这样完成你的程序。

    #include <stdio.h>
    
    int year_info();
    
    int main() {
        int menu_selection;
        int year;
    
        printf("Please choose from the following menu: \n1. Insert a new movie\n2. Show movie\n3. List all\n4. Exit\n");
        scanf("%i", &menu_selection);
    
        switch (menu_selection) {
            case 1:
                    year = year_info();
                    printf("%i", year);
                    break;
            default:
                    break;
        }
    
        return 0;
    }
    
    int year_info() {
        int year_imput;
        printf("\nYear: ");
        scanf("%i", &year_imput);
    
        if (year_imput > 2016 || year_imput < 1920) {
            printf("Sorry, I do not recognize this command. Please try again.\n");
            return 0;
        }
        else
            return year_imput;
    }
    

    【讨论】:

    • 我需要将年份存储为字符并最终将它们放入数组中。这将如何在这个程序中工作?函数返回类型是否可以是 char,然后在程序的“else”部分将 int 转换为 char?
    • 那么你应该将年份存储为String,它基本上是一个字符数组。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-01-11
    • 2011-01-27
    • 2021-12-05
    • 2022-09-29
    • 1970-01-01
    • 2019-06-09
    • 2013-11-25
    相关资源
    最近更新 更多