On Jan 17, 11:12*am, "Bill Cunningham" <nos...@nspam.invalid> wrote:
> * * I have these two functions one calls the other one. I have tried
> everything to debug them except a damn debugger. I have placed puts messages
> at all the return and I still get two numbers on my screen. What's wrong
> with this code? I will post the header, object file source and main() code.
>
> #include <stdio.h>
>
> double oper(char, double, double);
> double vop(double);
>
> Ok that's my declarations
>
> #include "core.h"
>
> double oper(char c, double x, double y)
> {
> * * if (c == 'a')
> * * * * return vop(x + y);
> * * else if (c == 's') {
> * * * * return vop(x - y);
> * * } else if (c == 'm') {
> * * * * return vop(x * y);
> * * } else if (c == 'd') {
> * * * * return vop(x / y);
> * * } else {
> * * * * fputs("usage error\n", stderr);
> * * * * return -1;
> * * }
> * * return 0;
>
> }
>
> double vop(double oper)
> {
> * * return printf("%.2f\n",oper);
>
> }
>
> Above here is the main code I compile in .o file. And this is the main
> function:
>
> #include "core.h"
>
> int main()
> {
> * * double a;
> * * a = oper('s', 5, 2);
> * * return vop(a);
>
> }
>
> Everything works like I want it to except I am getting two numbers. The
> second number is always the same whatever I pass to oper().
Of course, you're going to get 2 numbers. You call vop() twice.
Have you read the documentation for printf? Try changing your
main to
int main(void)
{
double a, b;
a = oper('s', 5., 2.);
b = oper('s', 50., 2.);
printf("%f %f\n", a, b);
return (0);
}
--
steve
|