Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

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:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

`

#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

EDIT: This problem has been solved thanks to the insight of everyone that commented and offered solutions. Thank you for being a welcoming community and being patient with very beginner programmers like me. I don’t feel as intimiated to ask questions anymore.

>Solution :

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).

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading