944,184 Members | Top Members by Rank

Ad:
  • C Discussion Thread
  • Unsolved
  • Views: 6012
  • C RSS
Jun 2nd, 2007
0

Using loop functions

Expand Post »
When using loop functions or statements which loop statement do you prefer to use that would be easy for a beginner like me to use:
The do while loop, for loop, or while loop. I ask because I am to re-write my code from using the If statement to using one of the stated loop functions into my code. I am writing a coke machine program, I haven't started re-writing it yet I wanted to know which one of the stated loop functions would be practical for a beginner like me to use. If you need to see my code here it is. It is written in If statements, I haven't started on the loop functions yet.
  1. #include <stdio.h>
  2.  
  3. int main()
  4. {
  5. char drinkchoice;
  6. float insert;
  7. float money = 1.00;
  8. float totalinsert;
  9.  
  10.  
  11. printf("\nHow much do you wish to insert: ");
  12. scanf("%f", &insert);
  13.  
  14.  
  15.  
  16.  
  17. if(totalinsert < money)
  18. {
  19. printf("Please insert more money: ");
  20. scanf("%f", &insert);
  21. totalinsert = insert + totalinsert;
  22. }
  23.  
  24. if(totalinsert < money)
  25. {
  26. printf("Please insert more money: ");
  27. scanf("%f", &insert);
  28. totalinsert = (insert + totalinsert);
  29. }
  30.  
  31. if(totalinsert < money)
  32. {
  33. printf("Please insert more money: ");
  34. scanf("%f", &insert);
  35. totalinsert = (insert + totalinsert);
  36. }
  37.  
  38. if(totalinsert < money)
  39. {
  40. printf("Please insert more money: ");
  41. scanf("%f", &insert);
  42. totalinsert = (insert + totalinsert);
  43. }
  44.  
  45. if(totalinsert == money)
  46. {
  47. printf("\nWhat would you like.");
  48. printf("\nPress 1 for coke\nPress 2 for sprite\nPress 3 for drpepper: ");
  49. scanf("%c", &drinkchoice);
  50. totalinsert = (totalinsert == insert);
  51. getchar();
  52. }
  53.  
  54.  
  55.  
  56. if(drinkchoice == '1')
  57. {
  58. printf("here is you coke");
  59. scanf("%c", &drinkchoice);
  60. getchar();
  61. }
  62. if(drinkchoice == '2')
  63. {
  64. printf("here is your sprite");
  65. scanf("%c", &drinkchoice);
  66. getchar();
  67. }
  68. if(drinkchoice == '3')
  69. {
  70. printf("here is your dr pepper");
  71. scanf("%c", &drinkchoice);
  72. getchar();
  73. }
  74.  
  75.  
  76.  
  77.  
  78.  
  79. getchar();
  80. return 0;
  81.  
  82.  
  83. }
Last edited by WaltP; Jun 3rd, 2007 at 2:44 pm. Reason: Added CODE tags -- you actually typed right over how to use them when you entered this post...
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
jlb_2_99 is offline Offline
6 posts
since May 2007
Jun 2nd, 2007
0

Re: Using loop functions

Short answer would be to have a while loop controlled by the totalinsert and money variables
  1. while( totalinsert < money)
  2. {
  3. /* ask for money here, add it to total insert */
  4. }
  5. /* you get here when you have enough totalinsert */
Reputation Points: 62
Solved Threads: 10
Junior Poster
jim mcnamara is offline Offline
179 posts
since May 2004

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: car park C programming
Next Thread in C Forum Timeline: Can anyone help me with my source code?





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


Follow us on Twitter


© 2011 DaniWeb® LLC