Clear getchar in a loop

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Oct 2003
Posts: 6
Reputation: Crowe182 is an unknown quantity at this point 
Solved Threads: 0
Crowe182 Crowe182 is offline Offline
Newbie Poster

Clear getchar in a loop

 
0
  #1
Oct 7th, 2003
Is there a way to clear the getchar variable in a loop after you go through the loop the 1st time?
Reply With Quote Quick reply to this message  
Join Date: Aug 2003
Posts: 117
Reputation: subtronic is an unknown quantity at this point 
Solved Threads: 1
subtronic's Avatar
subtronic subtronic is offline Offline
Junior Poster

Re: Clear getchar in a loop

 
0
  #2
Oct 7th, 2003
post code so I can help you
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 6
Reputation: Crowe182 is an unknown quantity at this point 
Solved Threads: 0
Crowe182 Crowe182 is offline Offline
Newbie Poster

Re: Clear getchar in a loop

 
0
  #3
Oct 7th, 2003
messed the code up, see next one. :o
Last edited by Crowe182; Oct 7th, 2003 at 10:35 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2003
Posts: 6
Reputation: Crowe182 is an unknown quantity at this point 
Solved Threads: 0
Crowe182 Crowe182 is offline Offline
Newbie Poster

Re: Clear getchar in a loop

 
0
  #4
Oct 7th, 2003
It's the getchar() for the computer value.
Thanks...
___________
  1. #include <iostream.h>
  2. #include <stdio.h>
  3.  
  4. void main()
  5. {
  6. char mon,
  7. prin,
  8. computer;
  9.  
  10.  
  11. int customer = 0,
  12. pentium = 0,
  13. pentium2 = 0,
  14. comp,
  15. response;
  16.  
  17. double s = 0, //pentium
  18. p = 0, //pentium II
  19. printer = 0,
  20. monitor = 0,
  21. rent = 0,
  22. sub_total = 0,
  23. total = 0,
  24. days = 0;
  25.  
  26. do
  27. {
  28.  
  29. s = 0,
  30. p = 0,
  31. printer = 0,
  32. monitor = 0,
  33. rent = 0,
  34. total = 0,
  35. days = 0;
  36.  
  37. printf("\nDo you want to rent a Pentium or a Pentium II system? ");
  38. printf("\nEnter 'S' for a Pentium or 'P' for a Pentium II: ");
  39. computer = getchar();
  40. getchar();
  41.  
  42. printf("\nEnter the number of days you want to rent computer: ");
  43. scanf("%lf", &days);
  44.  
  45. if ((computer == 'S') || (computer == 's')) //Pentium
  46. {
  47. printf("\nDo you want to rent a printer (y/n): ");
  48. getchar();
  49. prin = getchar();
  50.  
  51. if ((prin == 'Y') || (prin == 'y')) //Printer
  52. {
  53. printf("\nDo you want to rent a monitor (y/n): ");
  54. getchar();
  55. mon = getchar();
  56.  
  57. if ((mon == 'Y') || (mon == 'y')) //Monitor
  58. {
  59. monitor += 15.00;
  60. rent = monitor * days;
  61. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  62. }
  63. else //No Monitor
  64. {
  65. printer += 13.50;
  66. rent = printer * days;
  67. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  68. }
  69. }
  70. else //No Printer
  71. {
  72. printf("\nDo you want to rent a monitor (y/n): ");
  73. getchar();
  74. mon = getchar();
  75.  
  76. if ((mon == 'Y') || (mon == 'y')) //Monitor or Not
  77. {
  78. monitor = 12.50;
  79. rent = monitor * days;
  80. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  81. }
  82. else //No Monitor
  83. {
  84. p += 10.50;
  85. rent = p * days;
  86. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  87. }
  88. }
  89. ++pentium;
  90. }
  91. else // Pentium II
  92. {
  93. printf("\nDo you want to rent a printer (y/n): ");
  94. getchar();
  95. prin = getchar();
  96.  
  97. if ((prin == 'Y') || (prin == 'y')) //Printer
  98. {
  99. printf("\nDo you want to rent a monitor (y/n): ");
  100. getchar();
  101. mon = getchar();
  102.  
  103. if ((mon == 'Y') || (mon == 'y')) //Monitor
  104. {
  105. monitor += 21.00;
  106. rent = monitor * days;
  107. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  108. }
  109. else //No Monitor
  110. {
  111. printer += 19.00;
  112. rent = printer * days;
  113. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  114. }
  115. }
  116. else //No Printer
  117. {
  118. printf("\nDo you want to rent a monitor (y/n): ");
  119. getchar();
  120. mon = getchar();
  121.  
  122. if ((mon == 'Y') || (mon == 'y')) //Monitor or Not
  123. {
  124. monitor = 18.50;
  125. rent = monitor * days;
  126. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  127. }
  128. else //No Monitor
  129. {
  130. p += 15.50;
  131. rent = p * days;
  132. printf("\nYour rental charge for this computer is $ %5.2f", rent);
  133. }
  134. }
  135. ++pentium2;
  136. }
  137. sub_total += rent;
  138. printf("\n-----------------------------------------------");
  139. printf("\nYour subtotal is: $ %5.2f\n", sub_total);
  140. printf("\nDo you want to rent another computer?(y/n) ");
  141. getchar();
  142. response = getchar();
  143. total = sub_total;
  144. rent = 0;
  145. ++customer;
  146. }
  147. while ((response == 'Y') || (response == 'y'));
  148. printf("\nThe total number of total computers rented: %d", customer);
  149. printf("\nThe total number of Pentium's rented: %d", pentium);
  150. printf("\nThe total number of Pentium II's rented: %d", pentium2);
  151. printf("\nThe total rental charges for all the computers: $%6.2f\n", total);
  152. }
Last edited by Crowe182; Oct 7th, 2003 at 10:36 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC