【问题标题】:My void function returns a value and does not return to the main function.我的 void 函数返回一个值并且不返回主函数。
【发布时间】:2017-08-09 23:15:53
【问题描述】:

我正在编写一个程序,要求飞行员输入坐标。然后,稍后在其他功能中使用这些坐标,例如计算平面的距离和角度

这是我的主要功能:

    int main()
{
    plane_checker();
    double angle_finder(int x, int y);
    double distance_plane(int x, int y, int z);
    void ils_conditions();
}

我的plane_checker() 函数在哪里:

plane_checker()
{
    printf("Please enter your identification code:");
    scanf("%s", &plane_name[0]);

    if( (plane_name[0]== 'j') || (plane_name[0]== 'f') || (plane_name[0]== 'm') || (plane_name[0]== 'J') || (plane_name[0]== 'F') || (plane_name[0]== 'M'))
    {
        printf("Sorry, we are not authorized to support military air vehicles.");;
    }
    else
    {
        printf("Please enter your current coordinates in x y z form:");
        scanf("%d %d %d", &x, &y, &z);

        if(z < 0)
        {
            printf("Sorry. Invalid coordinates.");
        }

    }
    return;
}

用户输入坐标后,我希望程序返回主函数并继续执行其他函数。但是,当我运行程序时,我的函数会返回输入的 z 值并结束程序。如此处所示:

Please enter your identification code:lmkng
Please enter your current coordinates in x y z form:1 2 2

Process returned 2 (0x2)   execution time : 12.063 s
Press any key to continue.

这可能是什么原因?我逐字检查我的程序,但找不到背后的原因?我错过了什么?

提前非常感谢您!

【问题讨论】:

  • @Schwern 我没有在 main.js 中声明它们。我之前声明过它们。我只是在 main 中调用了它们
  • "我希望程序返回主函数并继续执行其他函数。" 这些不是函数调用,它们是前向声明。

标签: c function return void


【解决方案1】:

打开警告 (-Wall),它会告诉你 plane_checker,因为你没有在声明中指定它,它有一个隐含的 int 返回值。

test.c:1:1: warning: type specifier missing, defaults to 'int' [-Wimplicit-int]
plane_checker()
^

您还会收到关于未声明变量的许多其他警告和错误:x、y、z 和 plane_name。把它们都修好。如果它们是全局变量,则不应如此。


“我希望程序返回主函数并继续执行其他函数。”

那些不是函数调用,它们是函数的前向声明。函数调用类似于angle_finder(x, y)

很抱歉,您的代码充满了错误。我建议您退后一步,阅读更多关于 C 编程的资料。

【讨论】:

  • 是的,我是初级程序员。据我了解,当我在我的主函数中调用一个函数时,我不应该声明它是什么类型的函数?
  • @Huzo 同样,您的代码可能有问题,我建议您退后一步,完成教程。我发现Learn C The Hard Way 非常棒,但我已经有一些 C 语言经验和很多编程经验。 C tutorial on TutorialsPoint 可能会有所帮助。
  • @Huzo 函数声明在函数之外(最好在标题中)。 main 中需要函数调用。
  • 我理解我的问题。 :) 我认为在我的主函数中调用它时我必须再次声明该函数。但事实证明,只需调用该函数就足够了,无需再次声明其类型。谢谢!
【解决方案2】:

如果你不想让你的函数返回任何东西,像这样定义它

void plane_checker()
{
    printf("Please enter your identification code:");
    scanf("%s", &plane_name[0]);

    if( (plane_name[0]== 'j') || (plane_name[0]== 'f') || (plane_name[0]== 'm') || (plane_name[0]== 'J') || (plane_name[0]== 'F') || (plane_name[0]== 'M'))
    {
        printf("Sorry, we are not authorized to support military air vehicles.");;
    }
    else
    {
        printf("Please enter your current coordinates in x y z form:");
        scanf("%d %d %d", &x, &y, &z);

        if(z < 0)
        {
            printf("Sorry. Invalid coordinates.");
        }

    }

}

但是,您将无法在 plane_checker 函数之外操作插入的数据。您应该从 plane_checker() 返回插入的数据或使用指针。 https://www.tutorialspoint.com/cprogramming/c_pointers.htm

【讨论】:

  • 我尝试做 void plane_checker() 但它仍然返回输入的 z 值。关于指针,我还没有学过它们,但会检查一下!谢谢
  • 您是否删除了退货;从头到尾?
  • 哦,我没有看到您已将其删除。我删除了它,它现在可以工作了!非常感谢
  • 顺便说一句,如果我在 int x, y, z 中指向 x y z;部分,那会解决它吗?
  • 很高兴为您提供帮助,如果对您有帮助,请将我的回答标记为正确。而且我不明白最后一个问题(顺便说一句,如果我在 int x, y, z; 部分中指向 x y z ,那会解决它吗?)
猜你喜欢
  • 2016-08-28
  • 1970-01-01
  • 1970-01-01
  • 2021-12-18
  • 2023-01-28
  • 1970-01-01
  • 2010-09-15
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多