First I actually thought there was something wrong with my programming skills. But after I compiled the same code using Visual Studio, it worked. So here's the code that doesn't seem to be working right with gcc compilers, but they do compile. Note I'm using the Cygwin 3.4.4 version of gcc and 4.5.1 version of gcc from MinGW.
This is the first one. It compiles with GCC but the output doesn't seem to be what I'm expecting. (MinGW gcc (GCC) 4.5.1)
The part "5.000000 to the power of 0.000000 = 0.000000" doesn't seem to be right. But on the other hand, when I compiled it using Visual Studio, it worked very well.
I looked for a solution in the internet and I found sscanf. I tried to modified the code, it still compiles with gcc but yielding the same results.
Using the gcc compiler of MinGW this is the result: (MinGW gcc (GCC) 4.5.1)
The previous yields the same result as before where the part "5.000000 to the power of 0.000000 = 0.000000" is not working as intended. If I use the compiler from Visual Studio 2008 C++, this is the result:
Code:
#include <stdio.h>
#include <math.h>
int main()
{
long double r = 0;
long double p = 0;
printf ("Enter a number: ");
scanf ("%lf", &r);
printf ("Enter its power: ");
scanf ("%lf", &p);
printf ("%lf to the power of %lf = %lf\n", r, p, pow(r, p));
printf ("7 ^ 3 = %lf\n", pow (7.0,3.0));
printf ("4.73 ^ 12 = %lf\n", pow (4.73,12.0));
printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
return 0;
}
Code:
Enter a number: 5 Enter its power: 3 [B]5.000000 to the power of 0.000000 = 0.000000[/B] 7 ^ 3 = 343.000000 4.73 ^ 12 = 125410439.217423 32.01 ^ 1.54 = 208.036691
Code:
Enter a number: 5 Enter its power: 3 5.000000 to the power of 3.000000 = 125.000000 7 ^ 3 = 343.000000 4.73 ^ 12 = 125410439.217423 32.01 ^ 1.54 = 208.036691
Code:
#include <stdio.h>
#include <math.h>
int main()
{
char line[100];
long double r = 0;
long double p = 0;
printf ("Enter a number: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%lf",&r);
printf ("Enter its power: ");
fgets(line,sizeof(line),stdin);
sscanf(line,"%lf",&p);
printf ("%lf to the power of %lf = %lf\n", r, p, pow(r, p));
printf ("7 ^ 3 = %lf\n", pow (7.0,3.0));
printf ("4.73 ^ 12 = %lf\n", pow (4.73,12.0));
printf ("32.01 ^ 1.54 = %lf\n", pow (32.01,1.54));
return 0;
}
Code:
Enter a number: 5 Enter its power: 3 5.000000 to the power of 0.000000 = 0.000000 7 ^ 3 = 343.000000 4.73 ^ 12 = 125410439.217423 32.01 ^ 1.54 = 208.036691
Code:
Enter a number: 5 Enter its power: 3 5.000000 to the power of 3.000000 = 125.000000 7 ^ 3 = 343.000000 4.73 ^ 12 = 125410439.217423 32.01 ^ 1.54 = 208.036691
Comment