gpt4 book ai didi

c - 从函数传递双值时无法打印答案(C 语言)

转载 作者:行者123 更新时间:2023-11-30 20:53:25 25 4
gpt4 key购买 nike

该程序旨在接收 double 据类型值。然后使用自定义函数 get_double 验证数据类型,并使用相应函数和提供的值( double )获取面积/体积/周长。那里有一个 switch 语句,但我认为它不是导致问题的原因。

我真的不知道问题出在哪里。我尝试用两个不同的编译器进行编译,但仍然可以。

由于仅分享一小段代码,我认为不足以理解问题。我认为解决方案一定相当简单,但由于我是编程初学者,所以我看不到解决方案。

#include <stdio.h>
#include <ctype.h>

//OPTIONS
double volume (double radius, double height); //V = volume
double area (double radius); //A = area
double circumf (double radius); //C = circumference
//Q = quit
char get_option (void);
double get_double (void);

int main ()
{
int salir = 0;
char option;
double radius, area_answer, circumference_answer, height, volume_answer;

do {

// fflush(stdin);
//FFLUSH is important. It allows the buffer to be cleaned from the input entered when informing the program about the data needed (radius and height). Otherwise the DO WHILE loop is repeated additional and unnecerary times. IT CLEARS THE INPUT BUFFER!

//OPTION to FFLUSH. It can be used this loop after every scanf in the code: while ((getchar()) != '\n'); THIS LOOP CLEARS THE BUFFER!


printf("This program informs about he geometry of a circle (or a cylinder).\n");
printf("Choose an option:\n"
"\tA - Area\n\tC - Circumference\n\tV - Volume\n\tQ- Quit\n");
option = get_option ();
printf("\nThe selected option was: %c\n\n", option);

switch(option)
{
case 'a':
printf("Enter the radius of the circle (double type) to know its area:\n");
radius = get_double();
area_answer = area(radius);
printf ("The area of a circle with radius %f is: %f\n\n", radius, area_answer);
while ((getchar()) != '\n');
break;
case 'c':
printf("Enter the radius of the circle (double type) to know its circumference:\n");
radius = get_double();
circumference_answer = circumf(radius);
printf("The circumference of a circle with radius %f is: %f\n\n", radius, circumference_answer);
while ((getchar()) != '\n');
break;
case 'v':
printf("Enter the radius and height of the cylinder (double type) to know its volume:\n");
radius = get_double();
height = get_double();
volume_answer = volume (radius, height);
printf("The volume of a cylinder with radius %f and height %f is: %f\n\n", radius, height, volume_answer);
while ((getchar()) != '\n');
break;
case 'q':
salir = 1;
printf("You have quitted the program.\n");
break;
default:
printf("Uknown option. Please, try again.\n\n");
while ((getchar()) != '\n');
break;
}
} while (salir == 0);

return 0;
}


//FUNCTIONS
double volume (double radius, double height)
{
double volume, PHI = 3.14;
volume = PHI * radius * radius * height;
return volume;
}

double area (double radius)
{
double area = 0.0, PHI = 3.14;
area = PHI * radius * radius;
printf("PHI = %f\n", PHI);
printf("radius = %f\n", radius);
printf("area = %f\n\n", area);
return area;
}

double circumf (double radius)
{
double circumf, PHI = 3.14;
circumf = 2.0 * PHI * radius;
return circumf;
}

//GETTING CHAR OR DOUBLE
char get_option (void) // <------------------ this function is problematic
{
char input_char;

printf("> ");

input_char = getchar();
input_char = tolower (input_char);

return input_char;
}

double get_double (void)
{
double input_double;

while ((scanf("%lf", &input_double) != 1))
{
while (getchar() != '\n')
printf("> ");
}

printf("input_double = %f\n\n", input_double);
}

我认为问题可能出在函数 get_double 中的某个地方,但我不确定。我尝试了不同的方法来解决这个问题,但没有成功。

最佳答案

您忘记在函数末尾写入 return input_double; double get_double (void) ...因此该函数返回垃圾,因为您没有返回任何值.

尝试一下,看看是否有效。

关于c - 从函数传递双值时无法打印答案(C 语言),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48832402/

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