Using loop functions

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

Join Date: May 2007
Posts: 6
Reputation: jlb_2_99 is an unknown quantity at this point 
Solved Threads: 0
jlb_2_99 jlb_2_99 is offline Offline
Newbie Poster

Using loop functions

 
0
  #1
Jun 2nd, 2007
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...
Reply With Quote Quick reply to this message  
Join Date: May 2004
Posts: 178
Reputation: jim mcnamara is on a distinguished road 
Solved Threads: 10
jim mcnamara jim mcnamara is offline Offline
Junior Poster

Re: Using loop functions

 
0
  #2
Jun 2nd, 2007
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 */
Reply With Quote Quick reply to this message  
Reply

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




Views: 2742 | Replies: 1
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC