c - C 中的计算函数/程序流程中的错误

标签 c

我做了一个非常简单的计算程序来自学 C。但我现在有一个奇怪的错误,想请教一些建议。

大约一个小时前,我还在这里寻求帮助以摆脱全局变量,并得到了一些建议(再次感谢)。

现在我已经实现了这些建议,但是当我现在在计算中输入直径时,它会为所有 3 个函数返回一个非常大且奇数的数字。 对于所有 3 个函数来说,这个数字都是相同的。

我不明白该函数从哪里获取该数字,因为我首先在 cir_user_input() (input.c) 中定义直径变量,然后仅在 getRadius(floatdiameter) (circlefunctions.c) 中再次调用它。我已经尝试了一些方法,例如指针或使用 EXTERN 语句,但这并没有给我想要/预期的结果。希望有人能指出我在这里做错了什么,或者可以建议一些有关如何解决此问题的文档?

main.c

#include <stdio.h>

#include "menu/menu.h"
#include "circle/circlemenu.h"
#include "input/input.h"

int main(void)
{
    while(1)
{
    menu();
    switch(menu_user_input())
{
    case 1:

        info_top();
        cir_user_input();
        info_bottom();
        break;

    case 2:
        system("cls");
        break;

    case 3:
        system("cls");
        break;

    case 8:
        system("cls");
        break;

    case 9:
        system("cls");
        break;

    case 0:
        return(0);

    default:
        system("cls");
        printf("\n **Wrong choice try again...**\n");
        break;
}

}
 return 0;
}

圆菜单.c

#include <stdio.h>
#include "circlemenu.h"

void info_top()
{
    system("cls");
    printf(" ----------------------------------------\n");
    printf(" Typ the diameter of the circle: ");
}

void info_bottom(double diameter)
{
    printf(" ----------------------------------------\n");
    printf(" The radius = %f \n\n" , getRadius(diameter));
    printf(" The surface = %f \n\n" , getSurface(diameter));
    printf(" The outline = %f \n" , getOutline(diameter));
    printf(" ----------------------------------------\n");
}

圆函数.c

#include "circlefunctions.h"
#include "../input/input.h"
#define PI 3.14

double getRadius(float diameter)
{
  double radius = diameter / 2;
  return radius;
}

double getSurface(float diameter){
   double radius = getRadius(diameter);
   return PI * (radius * radius);
}

double getOutline(float diameter){
    double radius = getRadius(diameter);
    return 2 * PI * radius;
}

输入.c

#include <stdio.h>
#include "input.h"

int menu_user_input()
{
    int number;
    scanf(" %d", &number);
    return number;
}

float cir_user_input()
{
    float diameter;
    scanf(" %e", &diameter);
    return diameter;
}

编辑

我忘了提及,我对编程非常陌生,我只是想自己学习。有些事情可能看起来很奇怪,但我当时只是想解决和理解一个问题。

最佳答案

info_bottom(); 您应该传递直径作为参数,即 info_bottom(diameter);

关于c - C 中的计算函数/程序流程中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29194098/

相关文章:

c - 我正在尝试使用 3 个参数使用递归来获取斐波那契数列,它只给出 0000..值不递增

c - 以下 C 代码段的输出是什么?

c - 以什么方式,fork() 系统调用生成子进程?

c - 设置嵌入式系统的运行配置

c - 如何在 C 中将 unsigned int 转换或转换为 int?

c - 大输入背包算法

c - 安全存储 AES key

c++ - 我可以在 Microsoft C++ 中测量 sprintf 所需的缓冲区吗?

c - 表达式必须是可修改的左值

java - 如何在没有关联窗口的情况下使图像出现在屏幕上