Home > OS >  Why is my do/while loop in c not allowing me to input 'amount' more than once?
Why is my do/while loop in c not allowing me to input 'amount' more than once?

Time:11-09

I am attempting to develop a vending machine in C in which the user can insert as many 50 cent, 20 cent, and 10 cent coins as they would like to then move on to the 'purchase' of a product.

So far, my primitive code runs smoothly; the issue I am currently facing is that I can only 'insert' coins into the vending machine once, even though I think the 'while' condition in the 'do/while' statement is being executed.

Below you can find my code:

`

#include <iostream>
using namespace std; 

int main() {
    int productid;
    string order, finished;
    int amount;
        cout << "Welcome to the coffee machine! We have a variety of beverages we can offer. \n Here is our selection: \n 1) Coffee- 50 cents \n 2) Tea- 60 cents \n 3) Cappuccino- 80 cents\n";
        cout << "Please select your drink of choice by entering its ID: (1, 2, or 3)";
        cin >> productid;
        if (productid != 1 &&  productid != 2 && productid != 3){
            cout << "That is an invalid entry; please try again. \n";
            cin >> productid;
        }
        cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
        cout << "When finished, please input '0'. ";
        cin >> amount;
        if (amount != 50 && amount != 20 && amount != 10 && amount != 0){
            cout << "That is an invalid coin; please insert coins again.\n";
            cin >> amount;
        }
        do {
            amount  = amount;
        }
        while (amount != 0);

        return 0;
}

`

I was expecting to be able to insert coins until I input '0', but the code terminal says 'Process finished with exit code 0' after I insert coins once, thereby not allowing me to continue to insert coins.

If anyone has any suggestions as to how I could fix this so that the user can continuously insert coins until they input '0' I would greatly appreciate it.

Please feel free to leave any suggestions as to how to proceed as well.

Thank you

CodePudding user response:

Your do-while looks like this:

        do {
            amount  = amount;
        }
        while (amount != 0);

What does this do? It adds amount to itself until eventually it is 0. This can happen due to the fact that numbers are stored in a finite number of bits in memory and when due to some operation (value assignment in this case) is greater than the maximum value that can be stored for the given type, then the value will overflow its type limits and may get a smaller value than prior to the addition as a result, maybe even 0.

However, this is definitely not what you want.

You want: to read amount repeatedly until it's 0 and add it to a sum

You actually do: read amount exactly once, then add it to itself until it's 0

How to remedy this:

Move the reading inside the code and make sure that the sum will be a different variable:

        cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
        cout << "When finished, please input '0'. ";
        int sum = 0;
        do {
            cin >> amount;
            if (amount != 50 && amount != 20 && amount != 10 && amount != 0){
                cout << "That is an invalid coin; please insert coins again.\n";
            } else {
                sum  = amount;
            }
        }
        while (amount != 0);

CodePudding user response:

You need to put the do { ... } while(...); around the entire block you'd like to repeat. Also, you need a separate variable for the sum.

    int amount, sum = 0;

    // ...

         cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
        cout << "When finished, please input '0'. ";
        do {
            cin >> amount;
            while (amount != 50 && amount != 20 && amount != 10 && amount != 0){
                cout << "That is an invalid coin; please insert coins again.\n";
                cin >> amount;
            }
            sum  = amount;
        }
        while (amount != 0);

I've also changed an if to a while in your code for the case when the user makes multiple mistakes.

To solve these cases yourself, it's recommended that you either use a debugger and step through your code; or add some logging into the code an check what's going on (e.g., what the loop repeats).

CodePudding user response:

the reason why the program does not ask you for another input is because the do-while loop only includes one line: amount = amount;

Also this is the issue why the program terminates, it gets stuck in this loop of adding amount on itself and when the int overflow happens and the amount gets zero value, the program terminates. You can check this behavior by printing out the amount variable in the loop.

amount  = amount;
cout << amount << endl;

To fix the issue, you need to move couple of lines inside do block.

    do {
        cout << "Please insert your coins. \n This vending machine only accepts 50 cent coins, 20 cent coins, and 10 cent coins. \n ";
        cout << "When finished, please input '0'. ";
        cin >> amount;
        cout << amount << endl;
        if (amount != 50 && amount != 20 && amount != 10 && amount != 0){
            cout << "That is an invalid coin; please insert coins again.\n";
            cin >> amount;
        }
        
        amount  = amount;
        cout << amount << endl;
    }
    while (amount != 0);

Also, you need one more variable for the sum of coins (amount). This way if you add only one coin, the amount variable will be doubled.

Hope this helps.

  • Related