This program is first forked then run by execlp, it's calling program passes in two numbers, a power and a base.
int main(int argc, char *argv[])
{
int pid = getpid();
printf("Calculator Process[%d]: started\n",pid);
double base, power;
sscanf(argv[1],"%d",&base);
sscanf(argv[2],"%d",&power);
double number = pow(base,power);
printf("Calculator Process[%d]: %d ^^ %d == %d\n",pid,base,power,number);
printf("Calculator Process[%d]: exiting\n",pid);
return 1;
}
Lets say I pass into it base 3, power 5. This is what I get:
base = 4263 -- this also happens to be the PID. power = -1 raised to power: 714477568
Calling line:
execlp("./calculator","./calculator",argv[1],argv[2],(char*)0);
When I print the argvs, I get their value (as a char*, but casting fails).
Any ideas why I can't get the values to be correctly read in?
Either read double:
double base, power;
sscanf(argv[1],"%lf",&base);
sscanf(argv[2],"%lf",&power);
Or scan into integers:
int base, power;
sscanf(argv[1],"%d",&base);
sscanf(argv[2],"%d",&power);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With