Thread: Need Help With A Do While Loop

  1. #1
    Registered User Hackstorix's Avatar
    Join Date
    Feb 2013
    Posts
    9

    Post Need Help With A Do While Loop


    Hi

    I am a student at Westminster Kingsway college and i am studying c programming for my course, i need some help for a coffee dispenser that i created for one of my assessments.

    Every Time I Ask For Change It Does Not Stop Looping

    Code:
    #include<process.h>
    #include"header.h"
    #include<stdio.h>
    #include<conio.h>
    
    
    void main(void)//Emre
    {
        int mainoption;
        system("color 9f");
        do{
            mainoption = SnacksMainMenu();//Sheku
            SnacksMenuChoice(mainoption);//Riffat
            ClearTheScreen();
        }while(mainoption != 2768);
        getch();
    }
    
    
    unsigned int SnacksMainMenu(void)//SnacksMainMenu
    {
        int option;
        printf("The Rapid Snacks Coffee Dispenser - *** By Emre Celikoz *** \n");
        printf("\n1.  Coffee With Milk : 50p\n");
        printf("2.  Coffee Without Milk : 50p\n");
        printf("3.  Coffee with Sugar : 50p\n");
        printf("4.  Coffee Without Sugar : 50p\n");
        printf("5.  Tea With Milk : 50p\n");
        printf("6.  Tea Without Milk : 50p\n");
        printf("7.  Tea With Sugar : 50p\n");
        printf("8.  Tea With Sugar : 50p\n");
        printf("9.  Chicken And Vegetable Soup : 75p\n");
        printf("10. Mulligatawny Soup : 75p\n");
        printf("11. Tomato Soup : 75p\n");
        printf("12. Hot Chocolate : 35p\n");
        printf("13. Hot Water : 10p\n");
        printf("\n");
        printf("Please Enter Your Selection Number > ");
        scanf("%d",&option);
        return option;
    }
    void SnacksMenuChoice(unsigned int choice)//SnacksMenuChoice
    {
        if(choice == 1)//Coffee With Milk
        {
        int AmountOwing = 50;
        int Contribution;
        int AmountOfDebt;
        int Change;
        AmountOfDebt = AmountOwing;
        printf("\nYou Have  Selected Coffee With Milk...\n");
        printf("\nPlease Enter %d > ",AmountOwing);
        scanf("%d",&Contribution);
    
    
            if(Contribution <= 49){
                do{
                    printf("\nEnter amount >");
                    scanf("%d",&Contribution);
                    AmountOfDebt = AmountOfDebt - Contribution;
                }while(AmountOfDebt != 0);
            }
            else if(Contribution == 50)
            {
                printf("Thank You For Your Using Rapid Snacks\n");
                printf("Please Collect Your Item From Below\n");
            }
            else if(Contribution >= 51){
                printf("Thank You For Your Using Rapid Snacks\n");
                Change = Contribution - 50;
                printf("Please Collect Your Change %d\n",Change);
                printf("Please Collect Your Item From Below\n");
            }
            getch();
        }


    Thank You, Hackstorix
    Last edited by Hackstorix; 03-28-2013 at 12:30 PM.

  2. #2
    Registered User
    Join Date
    May 2012
    Posts
    505
    If amountofdebt goes negative then the code will run for a long time, until you get an overflow.

    So you need to put it checks.#

    As a general approach, printf() diagnostics to help with debugging.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help - Collect data from Switch loop inside While loop
    By James King in forum C Programming
    Replies: 15
    Last Post: 12-02-2012, 10:17 AM
  2. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  3. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  4. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  5. While loop ..crating an infanat loop
    By fmchrist in forum C Programming
    Replies: 7
    Last Post: 01-14-2006, 10:52 AM

Tags for this Thread