943,678 Members | Top Members by Rank

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

C Help!!!

Expand Post »
I am writing this program for my computer class. I am having a problem with my do/while loop. I can go through my main IF statement, but when I go through the loop a second time, I enter 'S' os 's' for computer type it skips past the main IF statement. Do i need to clear my computer variable before going through the loop again, if so how do I do that. Please help.

Thanks,

Crowe182

_________
  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. response;
  15.  
  16. double s = 0, //pentium
  17. p = 0, //pentium II
  18. printer = 0,
  19. monitor = 0,
  20. sub_total = 0,
  21. total = 0,
  22. days = 0;
  23.  
  24. do
  25. {
  26.  
  27. printf("\nDo you want to rent a Pentium or a Pentium II system? ");
  28. printf("\nEnter 'S' for a Pentium or 'P' for a Pentium II: ");
  29. getchar();
  30. computer = getchar();
  31.  
  32. printf("\nEnter the number of days you want to rent computer: ");
  33. scanf("%lf", &days);
  34.  
  35. if ((computer == 'S') || (computer == 's')) //Pentium
  36. {
  37. printf("\nDo you want to rent a printer (y/n): ");
  38. getchar();
  39. prin = getchar();
  40.  
  41. if ((prin == 'Y') || (prin == 'y')) //Printer
  42. {
  43. printf("\nDo you want to rent a monitor (y/n): ");
  44. getchar();
  45. mon = getchar();
  46.  
  47. if ((mon == 'Y') || (mon == 'y')) //Monitor
  48. {
  49. monitor += 15.00;
  50. sub_total = monitor * days;
  51. }
  52. else //No Monitor
  53. {
  54. printer += 13.50;
  55. sub_total = printer * days;
  56. }
  57. }
  58. else //No Printer
  59. {
  60. printf("\nDo you want to rent a monitor (y/n): ");
  61. getchar();
  62. mon = getchar();
  63.  
  64. if ((mon == 'Y') || (mon == 'y')) //Monitor or Not
  65. {
  66. monitor = 12.50;
  67. sub_total = monitor * days;
  68. }
  69. else //No Monitor
  70. {
  71. p += 10.50;
  72. sub_total = p * days;
  73. }
  74. }
  75. printf("\nYour subtotal is $ %7.2f", sub_total);
  76. ++pentium;
  77. }
  78. else // Pentium II
  79. {
  80. printf("\nDo you want to rent a printer (y/n): ");
  81. getchar();
  82. prin = getchar();
  83.  
  84. if ((prin == 'Y') || (prin == 'y')) //Printer
  85. {
  86. printf("\nDo you want to rent a monitor (y/n): ");
  87. getchar();
  88. mon = getchar();
  89.  
  90. if ((mon == 'Y') || (mon == 'y')) //Monitor
  91. {
  92. monitor += 21.00;
  93. sub_total = monitor * days;
  94. }
  95. else //No Monitor
  96. {
  97. printer += 19.00;
  98. sub_total = printer * days;
  99. }
  100. }
  101. else //No Printer
  102. {
  103. printf("\nDo you want to rent a monitor (y/n): ");
  104. getchar();
  105. mon = getchar();
  106.  
  107. if ((mon == 'Y') || (mon == 'y')) //Monitor or Not
  108. {
  109. monitor = 18.50;
  110. sub_total = monitor * days;
  111. }
  112. else //No Monitor
  113. {
  114. p += 15.50;
  115. sub_total = p * days;
  116. }
  117. }
  118. printf("\nYour subtotal is $ %7.2f", sub_total);
  119. ++pentium2;
  120. }
  121. printf("\nDo you want to rent another computer?(y/n) ");
  122. getchar();
  123. response = getchar();
  124. total += sub_total;
  125. ++customer;
  126. }
  127. while ((response == 'Y') || (response == 'y'));
  128. printf("\nThe total number of customers are: %d", customer);
  129. printf("\nThe total number of Pentium's rented are: %d", pentium);
  130. printf("\nThe total number of Pentium II's rented are: %9d", pentium2);
  131. printf("\nThe total rental charges for all the computers: %17.2f\n", total);
  132. }
Last edited by Crowe182; Oct 7th, 2003 at 2:14 pm.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crowe182 is offline Offline
6 posts
since Oct 2003
Oct 7th, 2003
0

Re: C Help!!!

Ergh, It's very difficult to read your code like that. Isn't there a code option or something?

Found it.

When typing code, wrap it in [CODE] and [/CODE] tags; and then indent stuff using the [INDENT] and [/INDENT]; for example:

  1. // This is some code:
  2.  
  3. if( something == true || somethingelse != false )
  4. {<blockquote>
  5. // This has been indented using the Indent tags.
  6. printf('blah blah blah');
  7. </blockquote>}
Team Colleague
Reputation Points: 63
Solved Threads: 6
Supreme Evil Overlord
Roberdin is offline Offline
282 posts
since Feb 2003
Oct 7th, 2003
0

Re: C Help!!!

OK got the changes made. How is that??
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Crowe182 is offline Offline
6 posts
since Oct 2003
Oct 7th, 2003
0

Re: C Help!!!

Remember, you can also go to the user control panel, and somewhere in your preference options, you can set to use a WYSIWYG editor to edit posts. Unfortunately, I have to run off to class in a few minutes, so don't really have time to look at this program right now.
Administrator
Staff Writer
Reputation Points: 1422
Solved Threads: 162
The Queen of DaniWeb
cscgal is offline Offline
13,645 posts
since Feb 2002
Oct 7th, 2003
0

Re: C Help!!!

no problem, just let me know when you get a chance.

Thanks....
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: Array limit
Next Thread in C Forum Timeline: Clear getchar in a loop





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


Follow us on Twitter


© 2011 DaniWeb® LLC