从函数传递double值时无法打印出答案(C语言)

时间:2018-02-16 17:55:44

标签: c printf 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中,但我不确定。我尝试了不同的方法来解决问题。

1 个答案:

答案 0 :(得分:2)

你忘了在函数return input_double;的末尾写double get_double (void) ...所以这个函数返回垃圾,因为你没有返回任何值。

试一试,看看它是否有效。