944,035 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 2109
  • C++ RSS
You are currently viewing page 2 of this multi-page discussion thread; Jump to the first page
Nov 4th, 2009
0
Re: Void Functions
Ah. Its a simple error on our hand.
We forgot that floats cannot be compared directly
0.33333 = 0.33 will not return true always and even it does, change in the execution environmet(compiler, hardware, etc) will behave unpredictably.
try :
C++ Syntax (Toggle Plain Text)
  1. void diviGame(const double result, const double answer,
  2. int & diviWins, int & diviLosses, int & diviTotal)
  3. {
  4. if (fabs(answer - result) < 0.01 ) //decide winning
  5. diviWins++;
  6. else //or loss
  7. diviLosses++;
  8.  
  9. diviTotal++;
  10.  
  11. }

and see if it works now....
Also read this: http://www.cygnus-software.com/paper...ringfloats.htm

Hope that solves the problem.
Cheers
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 4th, 2009
0
Re: Void Functions
Yea with that it gets decimals but it wont get fractions and when I put a fractions the game terminates and shows the results it doesn't even ask if the user wants to keep playing.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ASFtlink is offline Offline
14 posts
since Oct 2009
Nov 4th, 2009
0
Re: Void Functions
Help please?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ASFtlink is offline Offline
14 posts
since Oct 2009
Nov 5th, 2009
0
Re: Void Functions
Ah. Floats cant be compared directly. And even if you get a result, the same code might not work on different runtime environments(cmpiler, hardware, etc). Therefore u need to set a margin.
Improved code:
C++ Syntax (Toggle Plain Text)
  1. void diviGame(const double result, const double answer,
  2. int & diviWins, int & diviLosses, int & diviTotal)
  3. {
  4.  
  5. if (fabs(answer - result) < 0.01 ) //decide winning
  6. diviWins++;
  7. else //or loss
  8. diviLosses++;
  9.  
  10. diviTotal++;
  11. }

Hope this helps.

Cheers
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 5th, 2009
0
Re: Void Functions
oh. please ignore the above post
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 5th, 2009
0
Re: Void Functions
See, if u want to compare fractions, why not covert the fraction into string and then compare the strings(the answer and the result)
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007
Nov 5th, 2009
0
Re: Void Functions
Never mind got it.
Thanks a lot for your help! =)
Last edited by ASFtlink; Nov 5th, 2009 at 12:59 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
ASFtlink is offline Offline
14 posts
since Oct 2009
Nov 5th, 2009
0
Re: Void Functions
no probs.
Reputation Points: 11
Solved Threads: 7
Junior Poster
abhi_elementx is offline Offline
118 posts
since Dec 2007

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Associating an icon with a custom file type
Next Thread in C++ Forum Timeline: Design problems, help!





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC