自动售货机 - 逻辑错误

时间:2017-01-30 00:51:09

标签: c

// My issue is a rather specific one, the code compiles "Insert Coins" "Coin
//not   accepted " indefinitely and doesn't allow input at all. I've tried this
//program with "While" only loops and "do" loops and it always compiles
//indefinitely without allowing input.I'm trying to figure out where my logic
//error is and possibly a simpler solution if possible. Thanks.



    #include <stdio.h>

    int main(void){

    int money, drink_selection, coins;
    money = 0;

 do{  //ISSUE HERE??

      do{ //OR HERE??
        printf("Insert Coins: "); //REPEATS THIS 

        scanf("%d" ,&coins); //DOESNT ALLOW THIS
            if(coins == 0 || coins == 5 || coins == 10 || coins == 25)
             {
             money +=coins;
             }

        else {
             printf("Coin not accepted \n");//REPEATS INDEFINITELY
             }
   }while(coins != 0); // looping here? 
    printf("Please select from the following menu: 1 - Coffee or 2 - Tea ) \n");
    printf("Enter your choice: \n");
    scanf("%d", &drink_selection);

    switch(drink_selection){
    case 1:
    printf("You have selected coffee as your choice. \n");
    money-=25;
        if (money >=0){
        printf("Please take your coffee. \n");
                      }

        break;

    case 2:
    money-=15;
        if (money >= 0){
        printf("Tea dispensing \n");
                       }
        break;

    default:
    printf("Ivalid input");
    break;

                    }

        if (money > 0){
        printf("Your change is %d cents ", &coins);
                      }

        else if (money < 0){
        printf("Insufficient amount, your change is: %d", &coins);
                           }

       }while(money == 0); //POSSIBLY ISSUE IS HERE?
        return 0;

       }

0 个答案:

没有答案