Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Why is return from my function is nan?

Reply
Thread Tools

Why is return from my function is nan?

 
 
utab
Guest
Posts: n/a
 
      01-29-2007
Dear all, I posted this yesterday since it was a sunday, many people
missed it I guess and I could not find the answer.

I tried sth easy(actually this was an exercise) but I tried to use the
standard lib. heavily for this problem(as far as I can). There was one
point I could not figure out. The problem is :

../a.out 1.3+3.2+.1+40/3*8/7-4*5-32

The program will parse the argument and find the result of the above
expression. I have two versions(the 2nd is working , not perfect ;-}),
but the first have a return problem I guess because if I use that
version I get "nan" as the result at line 37. What is my problem?

Sorry that there are some comments and cout statement in around the
code. But I guess it could be easier to figure out.

You can find the two versions of the code here:

http://groups.google.be/group/comp.l...thread/thread/
7ac73062c56a73a2/074abc747f2964e5?hl=en#074abc747f2964e5

One gentle poster commented on that UNIX may be expanding '*' to
wildcards, I tried that it is not the problem. I quoted and supplied
the expression. Probably I am missing sth quite easy.

 
Reply With Quote
 
 
 
 
Ondra Holub
Guest
Posts: n/a
 
      01-29-2007

utab napsal:
> Dear all, I posted this yesterday since it was a sunday, many people
> missed it I guess and I could not find the answer.
>
> I tried sth easy(actually this was an exercise) but I tried to use the
> standard lib. heavily for this problem(as far as I can). There was one
> point I could not figure out. The problem is :
>
> ./a.out 1.3+3.2+.1+40/3*8/7-4*5-32
>
> The program will parse the argument and find the result of the above
> expression. I have two versions(the 2nd is working , not perfect ;-}),
> but the first have a return problem I guess because if I use that
> version I get "nan" as the result at line 37. What is my problem?
>
> Sorry that there are some comments and cout statement in around the
> code. But I guess it could be easier to figure out.
>
> You can find the two versions of the code here:
>
> http://groups.google.be/group/comp.l...thread/thread/
> 7ac73062c56a73a2/074abc747f2964e5?hl=en#074abc747f2964e5
>
> One gentle poster commented on that UNIX may be expanding '*' to
> wildcards, I tried that it is not the problem. I quoted and supplied
> the expression. Probably I am missing sth quite easy.


You should place here your code (minimized as possible, but still
showing the problem). Few people will try to resolve not-working link
(at least for me the presented link is broken).

 
Reply With Quote
 
 
 
 
utab
Guest
Posts: n/a
 
      01-29-2007

http://groups.google.be/group/comp.l...thread/thread/
7ac73062c56a73a2/074abc747f2964e5?hl=en#074abc747f2964e5

 
Reply With Quote
 
Sami Vaaraniemi
Guest
Posts: n/a
 
      01-29-2007

"utab" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> Dear all, I posted this yesterday since it was a sunday, many people
> missed it I guess and I could not find the answer.
>
> I tried sth easy(actually this was an exercise) but I tried to use the
> standard lib. heavily for this problem(as far as I can). There was one
> point I could not figure out. The problem is :
>
> ./a.out 1.3+3.2+.1+40/3*8/7-4*5-32
>
> The program will parse the argument and find the result of the above
> expression. I have two versions(the 2nd is working , not perfect ;-}),
> but the first have a return problem I guess because if I use that
> version I get "nan" as the result at line 37. What is my problem?
>
> Sorry that there are some comments and cout statement in around the
> code. But I guess it could be easier to figure out.
>
> You can find the two versions of the code here:
>
> http://groups.google.be/group/comp.l...thread/thread/
> 7ac73062c56a73a2/074abc747f2964e5?hl=en#074abc747f2964e5
>
> One gentle poster commented on that UNIX may be expanding '*' to
> wildcards, I tried that it is not the problem. I quoted and supplied
> the expression. Probably I am missing sth quite easy.
>


Compiling the code gives this warning:

": warning: 'performer' : not all control paths return a value"

That should give you a hint as to what is wrong with the code.

Regards,
Sami


 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      01-29-2007
": warning: 'performer' : not all control paths return a value"
>
> That should give you a hint as to what is wrong with the code.



But if I add a "cout << vec_d[0] << endl;" before return under "else":

I can see the output is right and there. if it is there why is it not
returned correctly?

My best

 
Reply With Quote
 
Ondra Holub
Guest
Posts: n/a
 
      01-29-2007

utab napsal:
> ": warning: 'performer' : not all control paths return a value"
> >
> > That should give you a hint as to what is wrong with the code.

>
>
> But if I add a "cout << vec_d[0] << endl;" before return under "else":
>
> I can see the output is right and there. if it is there why is it not
> returned correctly?
>
> My best


Look on my answer in your first thread. You have there something like:

if (condition)
{
// here is missing return
}
else
{
return result;
}

So although result is valid, it is not returned and return value is
undefined.

 
Reply With Quote
 
utab
Guest
Posts: n/a
 
      01-29-2007



> if (condition)
> {
> // here is missing return}else
> {
> return result;
>
> }So although result is valid, it is not returned and return value is
> undefined.


Since the function is recursive, on the next run it does not return a
value, now I got that. It was simple.

Best regards

 
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
Why is return type in getfullspec().annotations named as "return"? andrew cooke Python 1 04-03-2011 01:02 AM
why why why why why Mr. SweatyFinger ASP .Net 4 12-21-2006 01:15 PM
findcontrol("PlaceHolderPrice") why why why why why why why why why why why Mr. SweatyFinger ASP .Net 2 12-02-2006 03:46 PM
Why use "return (null);" instead of "return null;" ? Carl Java 21 08-24-2006 04:33 AM
what value does lack of return or empty "return;" return Greenhorn C Programming 15 03-06-2005 08:19 PM



Advertisments