i'm learning myself programming in c witch basicly no previous programming experience and now i have a weird bug and would like to ask some advice.
In the program flow some of the input data gets suddenly changed and i can't see nor reason why that happens.
1.program starts -> user gets a menu choice -> selects case 1 -> program provides and additional input posiblity.... So far everything works correctly as i could see in debug mode.
2.The user puts in some numbers and the variable gets set to that number. (this happens in input.c)
3.Then the program continues in main.c to info_bottom(). (in circlemenu.c) 4.In there the function getRadius() gets called that should calculate a radius based on the user input set in input.c in point 2.
5.That calculation function is located in circlefunctions.c
But here/there is the strange thing.
If i look in the debugger, i see that the variable diameter and radius are both changed to some weird numbers and not the number that the user specified in point 2. Somehow the data stored in the pointer gets corrupted as far as i can judge. Unfortunately im still to inexperienced with debugging to find this out on my own so hopefully someone can please tell me what is going on. This problem occured while trying to get rid of all global variables in my program.
Im beginning to get a whole new respect and understanding for professional programmers and why it can take so long sometimes to fix bugs o0.
(i also made the corresponding header files but no need to put them up i think?)
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;
}
menu.c
#include <stdio.h>
#include "menu.h"
void menu()
{
printf(" \n Made by ************* 2015.\n");
printf(" ----------------------------------------\n\n");
printf(" 1. Calculate circle measurements. \n");
printf(" 2. \n");
printf(" 3. \n");
printf(" 8. Info. \n");
printf(" 9. Clear screen. \n");
printf(" 0. Exit. \n \n");
printf(" Make your choice and press enter: ");
}
input.c
#include <stdio.h>
#include "input.h"
int menu_user_input()
{
static int number;
scanf(" %i", &number);
return number;
}
float cir_user_input()
{
static float diameter;
scanf("%f", &diameter);
return diameter;
}
circlemenu.c
#include <stdio.h>
#include "circlemenu.h"
#include "circlefunctions.h"
void info_top()
{
system("cls");
printf(" ----------------------------------------\n");
printf(" Typ the diameter of the circle: ");
return;
}
void info_bottom()
{
printf(" ----------------------------------------\n");
printf(" The radius = %f \n\n" , getRadius());
printf(" The surface = %f \n\n" , getSurface());
printf(" The outline = %f \n" , getOutline());
printf(" ----------------------------------------\n");
return;
}
circlefunctions.c
#include "circlefunctions.h"
#include "../input/input.h"
#define PI 3.14
double getRadius()
{
static diameter = (*cir_user_input);
double radius = diameter / 2;
return radius;
}
double getSurface()
{
double radius = getRadius();
return PI * (radius * radius);
}
double getOutline(){
double radius = getRadius();
return 2 * PI * radius;
}