-1

C++ beginner here!

For a personal assignment which asks for a knowledge showcase on vectors, I'm trying to create a vending machine-esque program that is playable on a console.


//NOT FINISHED

#include <iostream>
#include <vector>


class Vend
{
public: 

    int choice;
    
    std::vector<double> order;

    double total = 0.0;
    

    void mainMenu()
    {
        std::cout << "0: Leave\n1: Pepsi 1.19\n2: Coke [SOLD OUT]\n";
        std::cout << "3: Canada Dry 1.19\n4: Granola Bar 1.99\n5: Chips 1.99\n";
        std::cout << "What's your choice?\n";
    }

    int option() 
    {
        std::cin >> choice;

        switch (choice)
        {

        case 0: //Leave vending machine
            std::cout << "You are now leaving the vending machine...";
            return 0;
            break;
        case 1: //Pepsi
            order.push_back(1.19);

            break;
        case 2: //Sold out Coke
            std::cout << "It's sold out!";
            return 0;
            break;
        case 3: //Root Beer
            order.push_back(1.19);
            break;
        case 4: //7UP
            order.push_back(1.99);
            break;
        case 5: // Canada Dry
            order.push_back(1.99);
            break;
        default:
            std::cout << "Invalid option!";
            return 0; 
            break;
        }

        for (int i = 0; i < order.size(); i++)

        {
            total+=order[i];
        }



    }

    void printTotal()
    {
        std::cout << "Your current total is " << total << ".\n";
    }

    double purchase()
    {
        std::cout << "Your final total is " << total << ". Please input your money!\n";
        double money;
        std::cin >> money;

        if (money >= total && money <= 3.20)
        {
            std::cout << "Purchase successful. Have a good day.\n";
            return 0;
        }
        else
        {
            std::cout << "Purchase declined.\n";
        }
    }
};

int main()
{
    Vend vend;

    std::cout << "You are now walking towards a vending machine, carrying $3.20.\n";
    std::cout << "As you approach the machine, a voice can be heard from your destination...\n\n";

    std::cout << "Welcome to our vending machine! Which item would you like?\n";

    
    
    for (int selection = 0; selection < 3; selection++)
    {   
        vend.mainMenu();
        vend.option();
        vend.printTotal();
        selection++;
        if (selection == 1)
        {
            std::cout << "You have selected one item. Do you want to pick another item? Y/N \n";
            char response;
            std::cin >> response;
            
            if (!(response != 'Y' && response != 'y'))
            {
                std::cout << "Pick another item.\n";
            }
            else
            {
                vend.purchase();
                return 0;
            }
        }       
    }
    vend.purchase();
    
}

When I ran my program, everything was fine if I were to only buy one item. However, if I were to purchase two items, the program would then calculate the sum of both items, but I would always get incorrect values (i.e if I were to pick a $1.19 pepsi and $1.19 root beer, the total would be $3.57, which is obviously wrong).

I suspect it has something to do with my vectors and for statements, such as this dude:

        for (int i = 0; i < order.size(); i++)

        {
            total+=order[i];
        }

Any idea why the total for two items is always wrong?

  • Since C++11 there has been a shorthand for iterating through vectors: `for (double i: order) { total += i; }` – jdaz Dec 15 '20 at 04:30
  • After the first time through the loop in `main()`, the object `vend` needs to be reset to a default state (e.g. resetting `vend.total` to `0.0`) for subsequent iterations. Otherwise values are retained (and added to) in subsequent iterations. There is also a problem of using floating point values to represent currency which is not a good idea - since floating point variables cannot precisely represent values like `0.1` or `0.01`, and those errors build up when doing multiple operations (e.g. adding values). – Peter Dec 15 '20 at 04:35
  • Suggest a read of the answer to [Struct and Pointers Stuck](https://stackoverflow.com/a/65271858/3422102) about *Mixing Your Interface and Implementation*. It will help you keep the logic of your code straight. You do keep them separate to a limited degree - which is good. – David C. Rankin Dec 15 '20 at 04:38

2 Answers2

1

Yeah, the issue is that you're summing a new total on top of the previous total.

If you want to do it this way (instead of incrementally adding to the total), make sure to set total to 0 before the for loop. Otherwise, you can ditch the for loop, and just make total += the new item.

Dan Weber
  • 401
  • 2
  • 9
1

You are counting some items repeatedly because you are not reseting the counter.

Put this line:

 total = 0.0;

before this one:

 for (int i = 0; i < order.size(); i++)

Everytime you add a new item, you go back to your vector with the previous value still in total, thats your error.

vmp
  • 2,370
  • 1
  • 13
  • 17