Void Functions

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #11
30 Days Ago
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 :
  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
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: ASFtlink is an unknown quantity at this point 
Solved Threads: 0
ASFtlink ASFtlink is offline Offline
Newbie Poster
 
0
  #12
30 Days Ago
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: ASFtlink is an unknown quantity at this point 
Solved Threads: 0
ASFtlink ASFtlink is offline Offline
Newbie Poster
 
0
  #13
29 Days Ago
Help please?
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #14
29 Days Ago
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:
  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
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #15
29 Days Ago
oh. please ignore the above post
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #16
29 Days Ago
See, if u want to compare fractions, why not covert the fraction into string and then compare the strings(the answer and the result)
PEACE !
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 14
Reputation: ASFtlink is an unknown quantity at this point 
Solved Threads: 0
ASFtlink ASFtlink is offline Offline
Newbie Poster
 
0
  #17
29 Days Ago
Never mind got it.
Thanks a lot for your help! =)
Last edited by ASFtlink; 29 Days Ago at 12:59 am.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 113
Reputation: abhi_elementx is an unknown quantity at this point 
Solved Threads: 6
abhi_elementx's Avatar
abhi_elementx abhi_elementx is offline Offline
Junior Poster
 
0
  #18
29 Days Ago
no probs.
PEACE !
Reply With Quote Quick reply to this message  
Reply

Tags
functions, void

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC