0 as percent

Thread Solved

Join Date: Jul 2008
Posts: 38
Reputation: plike922 is an unknown quantity at this point 
Solved Threads: 0
plike922's Avatar
plike922 plike922 is offline Offline
Light Poster

0 as percent

 
0
  #1
Jul 26th, 2008
We i compile this program is shows that percents as 0.... any suggestions.
  1. #include <stdio.h>
  2. #include "simpio.h"
  3. #include "strlib.h"
  4. #include "random.h"
  5. #include <math.h>
  6.  
  7. int main()
  8. {
  9. int g1, g2, g3, g4, w1, w2, w3, w4, p1, p2, p3, p4;
  10.  
  11. string n1;
  12. string n2;
  13. string n3;
  14. string n4;
  15.  
  16. //-------------------------------------------//
  17. printf("Enter first name: ");
  18. n1 = GetLine();
  19.  
  20. printf("Enter games played for %s: ", n1);
  21. g1 = GetInteger();
  22.  
  23. printf("Enter games won for %s: ", n1);
  24. w1 = GetInteger();
  25. //-------------------------------------------//
  26. printf("\n");
  27. //-------------------------------------------//
  28. printf("Enter second name: ");
  29. n2 = GetLine();
  30.  
  31. printf("Enter games played for %s: ", n2);
  32. g2 = GetInteger();
  33.  
  34. printf("Enter games won for %s: ", n2);
  35. w2 = GetInteger();
  36. //-------------------------------------------//
  37. printf("\n");
  38. //-------------------------------------------//
  39. printf("Enter thrid name: ");
  40. n3 = GetLine();
  41.  
  42. printf("Enter games played for %s: ", n3);
  43. g3 = GetInteger();
  44.  
  45. printf("Enter games won for %s: ", n3);
  46. w3 = GetInteger();
  47. //-------------------------------------------//
  48. printf("\n");
  49. //-------------------------------------------//
  50. printf("Enter fourth name: ");
  51. n4 = GetLine();
  52.  
  53. printf("Enter games played for %s: ", n4);
  54. g4 = GetInteger();
  55.  
  56. printf("Enter games won for %s: ", n4);
  57. w4 = GetInteger();
  58. //-------------------------------------------//
  59. printf("\n");
  60. printf("\n");
  61. //-------------------------------------------//
  62.  
  63. //---------------Calculations----------------//
  64. p1 = w1/g2*100;
  65. p2 = w2/g2*100;
  66. p3 = w3/g3*100;
  67. p4 = w4/g4*100;
  68. //-------------------------------------------//
  69.  
  70.  
  71. printf("Name Games Played Games Won Winning %\n");
  72. printf("---- ------------ --------- ---------\n");
  73. printf("%s %d %d %d\n", n1, g1, w1, p1);
  74. printf("%s %d %d %d\n", n2, g2, w2, p2);
  75. printf("%s %d %d %d\n", n3, g3, w3, p3);
  76. printf("%s %d %d %d\n", n4, g4, w4, p4);
  77. system("pause");
  78. }
_________________________
||||||||||[][][]|||||||||||||||||||
|||||||||||||||____|
||||||||||
||||||||||
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 2,030
Reputation: Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of Aia has much to be proud of 
Solved Threads: 177
Aia's Avatar
Aia Aia is offline Offline
Postaholic

Re: 0 as percent

 
0
  #2
Jul 26th, 2008
You are loosing precision. For percentages you need to use floating points.
Reply With Quote Quick reply to this message  
Join Date: Dec 2005
Posts: 5,850
Reputation: Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute Salem has a reputation beyond repute 
Solved Threads: 749
Team Colleague
Salem's Avatar
Salem Salem is offline Offline
Void main'ers are DOOMed

Re: 0 as percent

 
0
  #3
Jul 27th, 2008
> p1 = w1/g2*100;
Should this be g1 ?

Also, try
p1 = w1 * 100 / g1;
to preserve the precision.
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 2,001
Reputation: ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of ArkM has much to be proud of 
Solved Threads: 343
ArkM's Avatar
ArkM ArkM is offline Offline
Postaholic

Re: 0 as percent

 
0
  #4
Jul 27th, 2008
1. Follow Salem's advice (better use p1 = (w1 * 100) / g1 ).
2. Change the last single % in the header string literal to double %% (see printf format string specifications).
3. Better use tabs \t to adjust columns in all trailing printfs.
Last edited by Ancient Dragon; Jul 27th, 2008 at 12:28 pm. Reason: correct icode tags
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,348
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1462
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: 0 as percent

 
0
  #5
Jul 27th, 2008
>>3. Better use tabs \t to adjust columns in all trailing printfs.
That can easily screw up the spacing. Better to use printf() correctly, such as
printf("%-10.10s%15d%15d%15d\n", n1, g1, w1, p1);
The above will insure consistent spacing
Last edited by Ancient Dragon; Jul 27th, 2008 at 12:34 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Reply With Quote Quick reply to this message  
Reply

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



Similar Threads
Other Threads in the C Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC