gpt4 book ai didi

似乎无法在 function 和 main 之间传递信息

转载 作者:太空宇宙 更新时间:2023-11-04 04:38:36 24 4
gpt4 key购买 nike

我无法让我的代码正常运行。在这里:

#include <stdio.h>

void intro_msg( );
float compass_value( );
float direction( );

int main ( void )
{

float compass;

intro_msg( ) ;

compass_value(compass );

direction(compass );

return ( 0 ) ;

}

void intro_msg(void)
{
printf("Welcome to the Compass Program \n \n");
}

float compass_value(compass )
{
printf("Please enter a value for the compass direction. (0 - 360 degress ) : ");
scanf("%f", &compass);
printf("You entered %f degrees \n" , compass);
return(compass);
}


float direction(compass)
{
if (compass >= 354.38 && compass <= 360.00){
printf("You are heading North \n");
}
else if (compass >= 0.0 && compass <= 39.37){
printf("You are heading North \n");
}
else if (compass >= 39.38 && compass <= 84.37){
printf("You are heading Northeast \n");
}
else if (compass >= 84.38 && compass <= 129.37){
printf("You are heading East \n");
}
else if (compass >= 129.38 && compass <= 174.37){
printf("You are heading Southeast \n");
}
else if (compass >= 174.38 && compass <= 219.37){
printf("You are heading South \n");
}
else if (compass >= 219.38 && compass <= 264.37){
printf("You are heading Southwest \n");
}
else if (compass >= 264.38 && compass <= 309.37){
printf("You are heading West \n");
}
else if (compass >= 309.38 && compass <= 354.37){
printf("You are heading Northwest \n");
}
else
{
printf("You did not enter a value between 0 - 360 degrees");
}
}

我正在尝试从用户那里获得 0 - 360 度之间的度数,记录下来,然后确定他们面对的方向(北、南、东北等),但我的罗盘值从未被记录。

在向用户询问值后,我立即去打印值并得到 0.0,而不是用户输入的值。我在这里做错了什么?

最佳答案

我想您知道您实际上是按值将 compass 变量传递给 compass_value()。意味着更改不会反射(reflect)到主函数的罗盘变量中。

我看到您正在返回读取值,但您在调用主函数中的 compass_value() 时没有将其接收到任何内容。尝试接收它:

compass = compass_value(compass);

关于似乎无法在 function 和 main 之间传递信息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28642026/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com