Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > stuck

Reply
Thread Tools

stuck

 
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2011
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().

Bill


 
Reply With Quote
 
 
 
 
Ian Collins
Guest
Posts: n/a
 
      01-17-2011
On 01/18/11 08:12 AM, Bill Cunningham 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?


What do you expect if you call printf twice?

3/10.

--
Ian Collins
 
Reply With Quote
 
 
 
 
steve
Guest
Posts: n/a
 
      01-17-2011
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
 
Reply With Quote
 
Pavel Borzenkov
Guest
Posts: n/a
 
      01-17-2011
Hi,

On 2011-01-17, Bill Cunningham <> 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;
> }


This function will return the vop's return value (in case the supported
operation is passed in the 'c' variable).

>
> double vop(double oper)
> {
> return printf("%.2f\n",oper);
> }


This function will return the printf's return value (number of successfully
written characters), not the oper's value.

>
> 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);
> }


The 'a' variable will contain the oper's return value (number of characters
written by printf). Try call like oper('s', 9, 2); and you will see that the
number has changed.

-P

>
> 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().
>
> Bill
>
>

 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2011
steve wrote:

[snip]

> Of course, you're going to get 2 numbers. You call vop() twice.


How am I doing that? Do you mean in the oper() and in main() I am
calling vop?

> 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);
> }


Yes I see what you're doing above but it is defeating the purpose I
started out with. I want vop() or oper() to call the other to printf. I
don't just want to stick printf into main(). I hope I'm clear.

Bill


 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      01-17-2011
"Bill Cunningham" <> writes:

> I have these two functions one calls the other one. I have tried
> everything to debug them except a damn debugger.


Might be worth a try then.

> 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;


This last line serves no purpose.

> }
>
> double vop(double oper)
> {
> return printf("%.2f\n",oper);


What value does printf return? Is that really the value you want vop
(and hence) oper to return?

> }
>
> 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);


Do you really want the result of vop to the return value from main?
Looks unlikely to me.

> }
>
> Everything works like I want it to except I am getting two numbers.


You get two numbers printed because there are two calls to vop: one in
main and one from the call to oper in main.

> The
> second number is always the same whatever I pass to oper().


Not if you change the call to oper('s', 50, 2) or to oper('s', 2, 5) or,
indeed, to lots of other things that give a different second number.

--
Ben.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2011
Pavel Borzenkov wrote:
> Hi,
>
> On 2011-01-17, Bill Cunningham <> 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;
>> }

>
> This function will return the vop's return value (in case the
> supported operation is passed in the 'c' variable).


Ok maybe that's the mistake. I've never really worked in this manner
before with returns. I thought I was sending to vop(). So what I probably
need then is to use return oper() does that sound right. Boy I feel like a
big fool now but this one really stuck me.
>>
>> double vop(double oper)
>> {
>> return printf("%.2f\n",oper);
>> }

>
> This function will return the printf's return value (number of
> successfully written characters), not the oper's value.


I'm not sure I understand here.

>> 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);
>> }

>
> The 'a' variable will contain the oper's return value (number of
> characters written by printf). Try call like oper('s', 9, 2); and you
> will see that the number has changed.
>
> -P
>
>>
>> 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().
>>
>> Bill



 
Reply With Quote
 
Niklas Holsti
Guest
Posts: n/a
 
      01-17-2011
Bill Cunningham 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?


Whether the code is wrong or right depends on what you want it to do,
which you haven't said -- yet.

> 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;


That "return 0" is unreachable, so I would say it is "wrong".

> double vop(double oper)
> {
> return printf("%.2f\n",oper);


Since I have never seen a function that was expected to return the
return value of a printf() call, I *suspect* that that is "wrong".
Perhaps you meant:

printf("%.2f\n",oper);
return 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);


The first number is printed in vop() that is called from oper(). It
prints the number 3 (5-2) in the form 3.00.

With your implementation of vop(), the variable "a" will always have the
return value of printf() which normally is the number of characters
printed. This number depends on the magnitude of the parameter oper in
vop(); here oper equals 3.0 so the printed string is "3.00\n" which is 5
characters long (under Linux, where \n is a single character).
Therefore, "a" is 5.0.

> return vop(a);


The second number is printed in this call of vop() and is 5.00.

By the way, I would call this "return" statement "wrong" because it
tries to return a double value from main(), instead of an int.

> }
>
> 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().


Try oper('s', 5000.0, 2.0). The second printed number should then be
larger than 5 because the first printf() prints the longer string
4998.00\n (which is 8 characters under Linux).

--
Niklas Holsti
Tidorum Ltd
niklas holsti tidorum fi
. @ .
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2011
Ben Bacarisse wrote:
> "Bill Cunningham" <> writes:
>
>> I have these two functions one calls the other one. I have tried
>> everything to debug them except a damn debugger.

>
> Might be worth a try then.
>
>> 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;

>
> This last line serves no purpose.
>
>> }
>>
>> double vop(double oper)
>> {
>> return printf("%.2f\n",oper);

>
> What value does printf return? Is that really the value you want vop
> (and hence) oper to return?


Ok no no. I want printf to return vop() then. Ok thanks much for clearing me
up. I will try this again maybe from scratch. I want the answer of oper() to
be passed to vop() which is called from main and it prints what oper()
returns.

>> }
>>
>> 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);

>
> Do you really want the result of vop to the return value from main?
> Looks unlikely to me.
>
>> }
>>
>> Everything works like I want it to except I am getting two numbers.

>
> You get two numbers printed because there are two calls to vop: one in
> main and one from the call to oper in main.
>
>> The
>> second number is always the same whatever I pass to oper().

>
> Not if you change the call to oper('s', 50, 2) or to oper('s', 2, 5)
> or, indeed, to lots of other things that give a different second
> number.





 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      01-17-2011
Niklas Holsti wrote:
> Bill Cunningham 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?

>
> Whether the code is wrong or right depends on what you want it to do,
> which you haven't said -- yet.
>
>> 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;

>
> That "return 0" is unreachable, so I would say it is "wrong".


I wondered if that return was needed or not. I seemed like it should be
there.

>> double vop(double oper)
>> {
>> return printf("%.2f\n",oper);

>
> Since I have never seen a function that was expected to return the
> return value of a printf() call, I *suspect* that that is "wrong".


I have seen it. I don't know if it's good code or not though.

> Perhaps you meant:
>
> printf("%.2f\n",oper);
> return 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);

>
> The first number is printed in vop() that is called from oper(). It
> prints the number 3 (5-2) in the form 3.00.
>
> With your implementation of vop(), the variable "a" will always have
> the return value of printf() which normally is the number of
> characters printed. This number depends on the magnitude of the
> parameter oper in vop(); here oper equals 3.0 so the printed string
> is "3.00\n" which is 5 characters long (under Linux, where \n is a
> single character). Therefore, "a" is 5.0.
>
>> return vop(a);

>
> The second number is printed in this call of vop() and is 5.00.
>
> By the way, I would call this "return" statement "wrong" because it
> tries to return a double value from main(), instead of an int.
>
>> }
>>
>> 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().

>
> Try oper('s', 5000.0, 2.0). The second printed number should then be
> larger than 5 because the first printf() prints the longer string
> 4998.00\n (which is 8 characters under Linux).


Interesting.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Dlink di-784 freezing / lights stuck on markm75c@msn.com Wireless Networking 6 07-05-2006 03:36 PM
Wireless Network Connection Stuck on "Acquiring Network Address" =?Utf-8?B?bmlja3Nwb29u?= Wireless Networking 4 10-20-2005 07:31 AM
Clearing stuck dialog in Firefox Splibbilla Firefox 0 04-09-2005 09:34 AM
stuck sending message SightSeer Firefox 4 02-07-2005 12:19 PM
Mozilla stuck in German! RHertz Firefox 2 07-27-2004 12:54 AM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57