943,832 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6388
  • C RSS
Oct 7th, 2003
0

Clear getchar in a loop

Expand Post »
Is there a way to clear the getchar variable in a loop after you go through the loop the 1st time?
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crowe182 is offline Offline
6 posts
since Oct 2003
Oct 7th, 2003
0

Re: Clear getchar in a loop

post code so I can help you
Reputation Points: 44
Solved Threads: 1
Junior Poster
subtronic is offline Offline
117 posts
since Aug 2003
Oct 7th, 2003
0

Re: Clear getchar in a loop

messed the code up, see next one. :o
Last edited by Crowe182; Oct 7th, 2003 at 10:35 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crowe182 is offline Offline
6 posts
since Oct 2003
Oct 7th, 2003
0

Re: Clear getchar in a loop

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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crowe182 is offline Offline
6 posts
since Oct 2003

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: C Help!!!
Next Thread in C Forum Timeline: Need help with DirectX code





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


Follow us on Twitter


© 2011 DaniWeb® LLC