gpt4 book ai didi

C错误导致函数常量返回值

转载 作者:太空宇宙 更新时间:2023-11-04 08:35:03 25 4
gpt4 key购买 nike

当我调用 getinfo() 时,我得到一个常量值 8 和 1 位值,9 和 2 位值,10 和 3 位值。等等。在函数中,该值按预期打印,但是当尝试在 main 方法中读取该值时,该值如上所述。

关于为什么会发生这种情况有什么想法吗?

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <conio.h>
#include <math.h>


int main(){
float radius = 0;
float height = 0;
float cylvolume = 0;
float spherevolume = 0;

displaymyinfo();

radius = getinfo();
printf("\n r = %f", radius);

height = getinfo();
printf("\n h = %f", height);



cylvolume = compute_cylinder_volume(radius, height);
spherevolume = compute_sphere_volume(radius);


printf("h = %f", height);

printf("\n A cylinder with radius %f and height %f = %f cubic inches", radius, height, cylvolume);
printf("\n Volume of sphere with radius: %f is %f cubic inches", radius, spherevolume);

_getch();
return 0;

}
int displaymyinfo(){
printf("*********************\n");
printf("*Info was here *\n");
printf("*and here*\n");
printf("*even here *\n");
printf("*********************\n");
return 0;
}

float getinfo(){
float y = 0;
do{
printf("\n Enter a number: ");
scanf("%f", &y);
} while (y <= 0);

printf("%f", y);
return (y);
}

float compute_cylinder_volume(float r,float h){
float vol = 0.0;
vol = 3.14 * r * r * h;
return vol;
}
float compute_sphere_volume(float rad){
float vol = 0.0;
vol = 4.0 / 3.0 * 3.14 * rad * rad * rad;
return vol;
}

最佳答案

线

    radius = getinfo();

出现在定义函数getinfo()之前。 C 作为有用的语言,将假设您打算定义一个返回整数的函数。您将其定义为稍后返回 float 这一事实不会阻止它坚持这一信念。

添加

float getinfo();

main() 上方的某处(或将 main() 移至底部)。

关于C错误导致函数常量返回值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599002/

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