0

I am working on the "checkout" process of my vending machine code. I want to write it so that the program will keep asking for the amount needed from the user until all the money is entered. However, this code segment does not completely work.

"Checkout" Segment of Code:

while (money < total) {
    float amountOwed = total - money;
    cout << "Please insert another $" << amountOwed << endl;

    cout << "Enter amount: $" << flush;
    float payment;
    cin >> payment;
}
if (money > total) {
    float change = money - total;
    cout << "Thank you! You have $" << change << " change." << endl;
}

if (money == total) {
    cout << "Thank you! Have a nice day!." << endl;
}

Full code below:

#include <iostream>
#include <iomanip>
using namespace std;


string menuItems[5] = { "Popcorn", "Coconut Clusters" , "Granola Bar" , "Trail Mix" , "Chocolate" };

float cost[5] = { 2, 3, 2.50, 1.50, 1 };

void vendingMachine() {

    for (int i = 0; i < 5; i++)

        cout << i + 1 << ". " << menuItems[i] << ": $" << cost[i] << endl;

}

int main() {

    cout.precision(2);
    cout << std::fixed;

    cout << "Vending Machine" << endl;
    cout << "----Items------" << endl;

    vendingMachine();
    cout << "Enter 0 to checkout" << endl;
    
    float total;
    total = 0;

    int item;

    do {
        cout << "Enter your selection: " << flush;
        
        cin >> item;
        
        item = item - 1;
        //here will be printed : $0 has been added to cart even if you pressed 0 and what to escape
        //is it possible to fix this??
        cout << menuItems[item] << ": $" << cost[item] << " has been added to cart." << endl;
        total = total + cost[item];

    } while (item != -1);

    cout << "                         " << endl;
    cout << "Proceding to checkout..." << endl;
    cout << "========================" << endl;

    cout << "Amount due: $" << total << endl;


    cout << "Insert money here: $" << flush;
    float money;
    cin >> money;

    

    while (money < total) {
        float amountOwed = total - money;
        cout << "Please insert another $" << amountOwed << endl;

        cout << "Enter amount: $" << flush;
        float payment;
        cin >> payment;
    }
    if (money > total) {
        float change = money - total;
        cout << "Thank you! You have $" << change << " change." << endl;
    }

    if (money == total) {
        cout << "Thank you! Have a nice day!." << endl;
    }

        
    
    return 0;
}
0

1 Answer 1

3

In this loop:

while (money < total) {

you are not modifying money or total so the loop will never exit.

You probably want to update money like this:

while (money < total) {
  // ...
  cin >> payment;
  money += payment;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.