Need help with Looping

This is a discussion on Need help with Looping within the C Programming forums, part of the General Programming Boards category; I have setup a vending machine program but I need to use looping to keep asking the user for money ...

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    9

    Need help with Looping

    I have setup a vending machine program but I need to use looping to keep asking the user for money and accepting until $1.00 has been entered. For example, if the user enters .25 it will ask them to enter in more money. If they have entered in .25 and they enter in another .25 it should show that their new total is .50. I wanted to make the entire main function loop so that once the transaction is completed, the program will ask if you want to purchase a drink (Y/N) and once Y(yes) is chosen the program would loop again and start over with 0.00 and will ask to insert money again. If N(no) should be selected, the loop should exit and an output statement of what has been purchased should show up.
    Code:
    #include <stdio.h>
    main()
    {
    float fAnySelection = 1.00; 
    int iSelection = 0; 
    float f1Payment = 0.00; 
    float fChangeDue = 0.00; 
    	
    printf("\nWelcome to Harry's Retro Soda Pop Vending Machine\n"); 
    printf("\nPlease Enter $1.00 to view Various Soda Choices: "); 
    scanf("%f%", &fAnySelection); 
    	
    if (fAnySelection < 1.00){ //$1 inserted is greater than any selection. 
     printf("\nInserting more money guarantees you a drink next time! "); //User has to insert more money to get a drink from vending machine
     printf("Please Enter at the least $1.00: ");
     scanf("%f%", &fAnySelection);}
    	
    if (fAnySelection  >= 1.00){ //Any selection is greater than or equal to $1
    			
     printf("\nEnter 1. for Coca-Cola\n"); //Different types of soda's that the user gets a choice from. Some of these are still around but they have been since the 1970's or even earlier. 
     printf("\nEnter 2. for Pepsi-Cola\n");
     printf("\nEnter 3. for RC Cola\n");
     printf("\nEnter 4. for Diet Rite Cola\n");
     printf("\nEnter 5. for Tab\n");
     printf("\nEnter 6. for Big Red Soda\n");
     printf("\nEnter 7. for Brookdale Orange Soda\n");
     printf("\nEnter 8. for Fresca Soda\n");
     printf("\nEnter 9. for Nehi Grape Soda\n");
     printf("\nEnter 10. for Welch's Grape Soda\n");
     printf("\nPlease Enter Your Selection:");
     scanf("%d%", &iSelection);}
     	
    if (iSelection == 1) { 
     printf("\nThanks for your purchase,you made an excellent choice, enjoy and don't forget your drink!\n"); 
    			}
    	
    if (iSelection == 2) { 
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
    			}
    	
    if (iSelection == 3) { 
     printf("\nThanks for your purchase and enjoy. Ohh! and don't forget your drink!\n");
    			}
    			
    if (iSelection == 4) {
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n"); //Let's user know that the choice they have made was a good idea. 
    			}
    			
    if (iSelection == 5) {
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
    			}
    			
    if (iSelection == 6) {
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
    			}
    			
    if (iSelection == 7) {
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
    			}
    			
    if (iSelection == 8) {
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
    			}
    			
    if (iSelection == 9) {
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
    			}
    			
    if (iSelection == 10) {
     printf("\nThanks for your purchase, you made an excellent choice, enjoy and don't forget your drink!\n");
    			}
    if (iSelection < 11) { //User making a choice greater than number indicated is invalid
     printf("\nThe Choice you have selected is currently invalid. ");
     printf("Please Try Again: ");
     scanf("%d%", &iSelection); }
    	
    fChangeDue=fAnySelection-1.00;{ 
    if (fChangeDue>0.00);
     printf("\nAmount of change owed to user $%.2f", fChangeDue);} //Shows change owed to customer, if any that is owed. 
    			
    getchar();
    			
    }

  2. #2
    Fountain of knowledge.
    Join Date
    May 2006
    Posts
    794
    You need a loop to put in a carridge return every 100 characters or so too

  3. #3
    Registered User kryptkat's Avatar
    Join Date
    Dec 2002
    Posts
    638
    fAnySelection
    meow!

  4. #4
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,762
    Why do you use all those if statements when all your going to do no matter what soda the user buys is say the same thing?
    Code:
    /*This is much simpler */
    if(iSelection > 0 && iSelection <= 10)
        fputs("\nThanks for your purchase, enjoy and don't forget your drink!\n", stdout);
    Last edited by whiteflags; 05-20-2006 at 12:33 PM.

  5. #5
    Registered User
    Join Date
    May 2006
    Posts
    20
    citizen is right and also for longer codes your program will work more efficiently,even faster and your code will look more simple with that kind of simplizations

    if I were you I would put a scanf statement at the end of your code but before getchar() function to ask the user if he or she wanna get another drink via a printf statement. After that I would put the whole main function except variable declarations into a while loop which controls the answer to the scanf statement that I put before "getchar()" in each iteration.

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    scanf("%f%", &fAnySelection);
    You shouldn't have a trailing %. Use %f, not %f%.

    Code:
    printf("\nPlease Enter $1.00 to view Various Soda Choices: "); 
    scanf("%f%", &fAnySelection); 
    	
    if (fAnySelection < 1.00){ //$1 inserted is greater than any selection. 
     printf("\nInserting more money guarantees you a drink next time! "); //User has to insert more money to get a drink from vending machine
     printf("Please Enter at the least $1.00: ");
     scanf("%f%", &fAnySelection);}
    	
    if (fAnySelection  >= 1.00){ //Any selection is greater than or equal to $1
    			
     printf("\nEnter 1. for Coca-Cola\n"); //Different types of soda's that the user gets a choice from. Some of these are still around but they have been since the 1970's or even earlier. 
     printf("\nEnter 2. for Pepsi-Cola\n");
     printf("\nEnter 3. for RC Cola\n");
     printf("\nEnter 4. for Diet Rite Cola\n");
     printf("\nEnter 5. for Tab\n");
     printf("\nEnter 6. for Big Red Soda\n");
     printf("\nEnter 7. for Brookdale Orange Soda\n");
     printf("\nEnter 8. for Fresca Soda\n");
     printf("\nEnter 9. for Nehi Grape Soda\n");
     printf("\nEnter 10. for Welch's Grape Soda\n");
     printf("\nPlease Enter Your Selection:");
     scanf("%d%", &iSelection);}
    You could convert this to something like
    Code:
    while(money_entered < 100) {
        printf("Enter more money: ");
        scanf("%i", &x);
    }
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  7. #7
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    if (fChangeDue>0.00);
     printf("\nAmount of change owed to ...
    You have an extra semicolon there, which will cause the printf to always execute.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #8
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Quote Originally Posted by citizen
    Why do you use all those if statements when all your going to do no matter what soda the user buys is say the same thing?
    Code:
    /*This is much simpler */
    if(iSelection > 0 && iSelection <= 10)
        fputs("\nThanks for your purchase, enjoy and don't forget your drink!\n", stdout);
    If you intend to print a different message for every drink, look up the switch statement.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Code:
    if (iSelection < 11) { //User making a choice greater than number indicated is invalid
     printf("\nThe Choice you have selected is currently invalid. ");
     printf("Please Try Again: ");
     scanf("%d%", &iSelection); }
    Perhaps you meant iSelection < 0 || iSelection > 10?
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  10. #10
    ... kermit's Avatar
    Join Date
    Jan 2003
    Posts
    1,528
    You know, board members are allowed to edit their posts for up to twenty-four (IIRC) hours after they make one. That means that all you need to do is press the button that says 'edit', and you can add those other thoughts, the ones you didn't think of the first time, rather than clutter up the thread with four posts in as many minutes...
    Last edited by kermit; 05-20-2006 at 02:13 PM.

  11. #11
    Registered User
    Join Date
    May 2006
    Posts
    9
    How would I put into code where it asks the user for money(for example, if they insert .25. It will ask them for additional .75) because drinks cost $1.00. I am having the hardest time trying to figure that part of it out. Any help would be greatly appreciated. I also made all of the changes that everyone suggested and it works like a charm. Now all I need to figure out is what is mentioned above.

  12. #12
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    is it looping now? your program should look like this

    1) user enters money
    2) if money entered is less than amount remaining, deduct money
    (eg balance -= money)
    3) printf ("$%.2f remaining\n", balance);
    4) loop starts over
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. wierd looping effect after exporting 3ds to .x annimation
    By Anddos in forum Game Programming
    Replies: 3
    Last Post: 01-06-2009, 01:43 PM
  2. problems with prototype function looping
    By dezz101 in forum C Programming
    Replies: 5
    Last Post: 04-29-2008, 07:03 AM
  3. looping went berserk
    By miryellis in forum C Programming
    Replies: 7
    Last Post: 09-21-2004, 02:59 PM
  4. Looping questions
    By Peyote in forum C++ Programming
    Replies: 3
    Last Post: 09-16-2003, 12:01 AM
  5. looping and input
    By Kinasz in forum C Programming
    Replies: 2
    Last Post: 03-17-2003, 07:12 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21