2017-03-18 151 views
-1

我正在写一个程序,要求飞行员输入坐标。我的void函数返回一个值并且不返回到主函数。

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. 

这可能是什么原因造成的?我一字一句地检查了我的程序,但是找不到原因呢?我错过了什么?

非常感谢您提前!

+0

@Schwern我没有在main中声明它们。我之前宣布过他们。我只是在主 – Huzo

+1

中调用它们“*我希望程序返回到主函数并继续执行其他函数。”这些不是函数调用,它们是前向声明。 – Schwern

回答

1

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

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

+0

我试着做void plane_checker(),但它仍然返回输入的z值。关于指针,我没有学会他们,但会检查出来!谢谢 – Huzo

+0

您是否删除了退货;从最后? – Goran

+0

哦,我没有看到你删除了那个。我删除它,现在它工作!顺便说一句,谢谢 – Huzo

2

打开警告(-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.

+0

是的,我是初学者级别的编码员。据我了解,当我在我的主函数中调用函数时,我不应该声明它是什么类型的函数? – Huzo

+0

@Huzo再说一遍,你的代码有可能出错,我建议你退后一步,通过一个教程。我发现[Learn C The Hard Way](https://learncodethehardway.org/c/)非常好,但是我已经有了一些C语言经验和编程经验。 [TutorialsPoint上的C教程](https://www.tutorialspoint.com/cprogramming/index.htm)可能会有所帮助。 – Schwern

+1

@Huzo函数声明在函数之外(最好在头文件中)。函数调用是你在'main'中需要的。 –