so I have this function in C to calculate power and I use visual C ++ 2010
power.h
void power();
float get_power(float a, int n);
power.c
void power()
{
float a, r;
int n;
printf("-POWER-\n");
printf("The base: ");
scanf("%f", &a);
n = -1;
while (n < 0)
{
printf("The power: ");
scanf("%d", &n);
if (n < 0)
{
printf("Power must be equal or larger than 0!\n");
}
else
{
r = get_power(a, n);
printf("%.2f ^ %d = %.2f", a, n, r);
}
};
}
float get_power(float a, int n)
{
if (n == 0)
{
return 1;
}
return a * get_power(a, n-1);
}
Not the best way to do this, I know, but itβs not him when I debug it, the values ββare scanned correctly (that is, the values ββare correct until the function is called), but then when entering the function a becomes 0 and n becomes 1074790400, and you can guess what will happen next ... the
first function is called from the main file, I included the full code, because I really donβt know what can happen, and I canβt even think about how to do this for Google ...
weird I wrote a function in a single file and it works fine, but it should definitely work in both directions.
any idea why this is happening?