c - Calculation function returns 0 whatever the user puts in after creating header files -
im buisy learning myself c more or less not having previous programming exprerience. have made simple program in c , worked more or less. not perfect or anywhere near fine learning.
but got bug in program , can't find nor know how bug track such thing (yet).
i use github learn , perhaps guys can see did wrong in function or headers. (the thing try understand how header files work in c.) , teaches me make propper commits :/ .....
https://github.com/greendweller/myfirstprogram
the bug program wil not calculate anymore. accepts user input shows 0 result , cant figure out why. calclation functions in ../circle/circlefunctions.c
i hope enough information or need post code here. please let me know.
edit okay have post code directly, no problem:
circlefunctions.c
#include "circlefunctions.h" #define pi 3.14 float diameter; double radius; double surface; double outline; void circle_functions() { radius = diameter / 2; surface = pi * (radius * radius); outline = 2 * pi * radius; }
circlefunctions.h
extern float diameter; extern double radius; extern double surface; extern double outline; void circle_functions();
main.c
#include <stdio.h> #include "menu/menu.h" #include "circle/circlemenu.h" #include "circle/circlefunctions.h" #include "input/input.h" int main(void) { while(1) { menu(); switch(menu_user_input()) { case 1: info_top(); cir_user_input(); circle_functions(); info_bottom(); break; case 2: system("cls"); case 3: system("cls"); case 9: system("cls"); break; case 0: return(0); } } return 0; }
input.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; }
input.h
int menu_user_input(); float cir_user_input();
i can post rest if needed guess enough?
edit2
i think found a/the solution:
i set variable diameter 2 times. in input.c , circlecalculations.c , resets value think. or left blank input.h never included in circlecalculations.c.
you using global variables, (almost) bad design. current calculation function:
void circle_functions() { radius = diameter / 2; surface = pi * (radius * radius); outline = 2 * pi * radius; }
much better pass in needed inputs , return result:
double circle_functions(double diameter) { double radius = diameter / 2; double surface = pi * (radius * radius); double outline = 2 * pi * radius; return outline; }
Comments
Post a Comment