**Okay i have a problem that i am not able to sort out for couple of days now. I dont know why the code is not doing what it supposed to do.

Basically I have written this code for a vending machine. Things i want in a vending machine
  1. press different numbers for different products.
  2. should show a list of products inside the machine.
  3. show the quantity of a product.
  4. show the price of a product.
  5. get item from the list.
  6. reduce the quantity of an item after the selection of that item.

if possible - >

7. calculate the number of coins to be inserted.

Things i managed to get working are the first 6 features. I partially got the 7th feature working but some how it fails to return the desired result when the total number of coins are inserted that sums up the cost of the product. It returns a memory location istead of the displaying the correct output. I am attaching the full codes please help me figure out what the problem is. Thanks!**

vending.h

#ifndef  VENDING_HEADER
#define VENDING_HEADER  


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

class vendingM
{
    double price[10];
    string products[10];
    int quantity[10];


public:
    vendingM(void);
    void displayList();
    void quantityOfProducts();
    void priceChecker();
    void getItem();


    ~vendingM(void);
};

#endif VENDING_HEADER

vending.cpp

#include "vending.h"

vendingM::vendingM()
{
    products[0] = ("1. Mars");
    products[1] = ("2. Snickers");
    products[2] = ("3. Bounty");
    products[3] = ("4. Galaxy");
    products[4] = ("5. Twix");
    products[5] = ("6. Walkers");
    products[6] = ("7. Mccoy");
    products[7] = ("8. Pepsi");
    products[8] = ("9. Coca Cola");
    products[9] = ("10. Sprite");

    price[0] =  1;
    price[1] =  1;
    price[2] =  1;
    price[3] =  1;
    price[4] =  1;
    price[5] =  2;
    price[6] =  2;
    price[7] =  3;
    price[8] =  3;
    price[9] =  4;

    quantity[0] = 20;
    quantity[1] = 20;
    quantity[2] = 20;
    quantity[3] = 20;
    quantity[4] = 20;
    quantity[5] = 20;
    quantity[6] = 20;
    quantity[7] = 20;
    quantity[8] = 20;
    quantity[9] = 20;

}

void vendingM::displayList()
{
    cout << "The products list\n--------------------\n"; 
    for(int x=0; x<10; x++)
    {
        cout << products[x];
        cout << endl;
    };
}

void vendingM::priceChecker()    // price of the product
{
    cout << "Type the name of the product whose price you want to check or type exit to quit out of the price checker.\n\n";
    string x;
    cin >> x;


    if (x == "Mars" || x=="mars")
    {
        cout << "The price of this product is $" << price[0] << endl;
    }

    else if (x == "Snickers" || x == "snickers")
    {
        cout << "The price of this product is $" << price[1] << endl;
    }


    else if (x == "Bounty" || x == "bounty")
    {
        cout << "The price of this product is $" << price[2] << endl;
    }


    else if (x == "Galaxy" || x == "galaxy")
    {
        cout << "The price of this product is $" << price[3] << endl;
        cout << endl;
    }

    else if (x == "Twix" || x == "twix")
    {
        cout << "The price of this product is $" << price[4] << endl;
        cout << endl;
    }

    else if (x == "Walkers" || x == "walkers")
    {
        cout << "The price of this product is $" << price[5] << endl;
        cout << endl;
    }


    else if (x == "Mccoy" || x == "mccoy")
    {
        cout << "The price of this product is $" << price[6] << endl;
        cout << endl;
    }


    else if (x == "Pepsi" || x == "pepsi")
    {
        cout << "The price of this product is $" << price[7] << endl;
        cout << endl;
    }


    else if (x == "Coca Cola" || x == "coca cola")
    {
        cout << "The price of this product is $" << price[8] << endl;
        cout << endl;
    }


    else if (x == "Sprite" || x == "sprite")
    {
        cout << "The price of this product is $" << price[9] << endl;
        cout << endl;
    }

    else
        cout << "You have entered a bad command.\n";
    cout << endl;
}

void vendingM::quantityOfProducts()    //quantity of the product
{
    string z;
    cout << "\nEnter the name of the product whose quantity you want to check\n\n";
    cin >> z;

    if (z == "Mars" || z=="mars")
    {
        cout << "The quantity of this product is: " << quantity[0] << endl;
    }

    else if (z == "Snickers" || z == "snickers")
    {
        cout << "The quantity of this product is: " << quantity[1] << endl;
    }


    else if (z == "Bounty" || z == "bounty")
    {
        cout << "The quantity of this product is: " << quantity[2] << endl;
    }


    else if (z == "Galaxy" || z == "galaxy")
    {
        cout << "The quantity of this product is: " << quantity[3] << endl;
        cout << endl;
    }

    else if (z == "Twix" || z == "twix")
    {
        cout << "The quantity of this product is: " << quantity[4] << endl;
        cout << endl;
    }

    else if (z == "Walkers" || z == "walkers")
    {
        cout << "The quantity of this product is: " << quantity[5] << endl;
        cout << endl;
    }


    else if (z == "Mccoy" || z == "mccoy")
    {
        cout << "The quantity of this product is: " << quantity[6] << endl;
        cout << endl;
    }


    else if (z == "Pepsi" || z == "pepsi")
    {
        cout << "The quantity of this product is: " << quantity[7] << endl;
        cout << endl;
    }


    else if (z == "Coca Cola" || z == "coca cola")
    {
        cout << "The quantity of this product is: " << quantity[8] << endl;
        cout << endl;
    }


    else if (z == "Sprite" || z == "sprite")
    {
        cout << "The quantity of this product is: " << quantity[9] << endl;
        cout << endl;
    }

    else
        cout << "You have entered a bad command.\n";
    cout << endl;

}

void vendingM::getItem()  // get item from the vending machine
{
    string item;
    cin >> item;
    cout << endl;

    double money;
    double priceC;



    int* ptrItemQuantity;
    if(item == "mars")
    {
        cout << "you have selected mars.\n\n";
        do
        {
            cout << "Please insert the correct amount of money as the change will not be returned: ";
            cin >> money;
            cout << endl;


            if (money < 1)
            {
                priceC = price[0];
                do
                {
                    cout << "you have entered " << money << endl;
                    priceC -= money;
                    cout << "total money you need to insert is: " << priceC << endl;
                    cout << "Please insert more money: ";
                    cin >> money;
                    cout << "\n\n";

                }while(priceC != 0);

            }

        if(money > 1)
        {
            cout << "you have inserted more money than needed..sorry but you are not getitng your change back :P\n\n";

        }

            //cout  << money << endl;


        }while(money == 1 || money > 1);




        cout << "dispensing mars..Enjoy your meal.\n\n";



    ptrItemQuantity = &quantity[0];
    *ptrItemQuantity -= 1;

    }

    else if(item == "snickers")
    {

    cout << "you have selected snickers..dispensing snickers..Enjoy your meal.\n\n";
    ptrItemQuantity = &quantity[1];
    *ptrItemQuantity -= 1;

    }

    else if(item == "bounty")
    {

    cout << "you have selected bounty..dispensing bounty..Enjoy your meal.\n\n";
    ptrItemQuantity = &quantity[2];
    *ptrItemQuantity -= 1;

    }

    else if(item == "galaxy")
    {

    cout << "you have selected galaxy..dispensing galaxy..Enjoy your meal.\n\n";
    ptrItemQuantity = &quantity[3];
    *ptrItemQuantity -= 1;

    }

    else if(item == "twix")
    {

    cout << "you have selected twix..dispensing twix..Enjoy your meal.\n\n";
    ptrItemQuantity = &quantity[4];
    *ptrItemQuantity -= 1;

    }

    else if(item == "walkers")
    {

    cout << "you have selected walkers..dispensing walkers..Enjoy your meal.\n\n";
    ptrItemQuantity = &quantity[5];
    *ptrItemQuantity -= 1;

    }

    else if(item == "mccoy")
    {

    cout << "you have selected mccoy..dispensing mccoy..Enjoy your meal.\n\n";
    ptrItemQuantity = &quantity[6];
    *ptrItemQuantity -= 1;

    }

    else if(item == "pepsi")
    {

    cout << "you have selected pepsi..dispensing pepsi..Enjoy your drink.\n\n";
    ptrItemQuantity = &quantity[7];
    *ptrItemQuantity -= 1;

    }

    else if(item == "coke")
    {

    cout << "you have selected coca cola..dispensing coca cola..Enjoy your drink.\n\n";
    ptrItemQuantity = &quantity[8];
    *ptrItemQuantity -= 1;

    }

    else if(item == "sprite")
    {

    cout << "you have selected sprite..dispensing sprite..Enjoy your drink.\n\n";
    ptrItemQuantity = &quantity[9];
    *ptrItemQuantity -= 1;

    }

    else

        cout << "you have entered a wrong choice. Please type a name from the list.\n\n";


}



vendingM::~vendingM()
{
}

main.cpp

#include "vending.h"


void main()
{
    cout << "\n\n\n\n----------Welcome to the coolest vending machine!!!!!!!-----------\n\n";
    cout << "Press 1 and enter to display the list of products in the vending machine.\n";
    cout << "Press 2 and enter to check the price of each product.\n";
    cout << "Press 3 and enter to check the quantity of the products.\n";
    cout << "Press 4 to get an item from the vending machine.\n";
    cout << "Press 5 and enter twice to exit out of the vending machine.\n";
    cout << endl;

    vendingM vmObject;
    int x;

    do{

    cin >> x;
    cout << endl;


    switch(x)
    {
    case 1: 

        vmObject.displayList();

        break;

    case 2:

        vmObject.priceChecker();

        break;

    case 3:

        vmObject.quantityOfProducts();

        break;

    case 4:
        cout << "Please type the name of the item you want to get\n\n";
        vmObject.getItem();
        break;

    case 5: 
        cout << "Thank you for using this vending machine.\n\n";
        break;



    default:

        cout << "you have entered a wrong information.Please select a choice from the given options.\n\n";


    }
    cout << "\n\nPress 1 and enter to display the list of products in the vending machine.\n";
    cout << "Press 2 and enter to check the price of each product.\n";
    cout << "Press 3 and enter to check the quantity of the products.\n";
    cout << "Press 4 to get an item from the vending machine.\n";
    cout << "Press 5 and enter twice to exit out of the vending machine.\n";
    cout << endl;


    } while(x != 5);


    //cin.get();
    system("pause");
    //return 0;
};

Recommended Answers

All 3 Replies

you have a ; after the last bracket. and system("pause"); is not a good thing to use.

the problems that u pointed out are not concerned with the problem that i have as you can see i did use cin.get(); as an alternate of system("pause"); but i just commented it out and having the semi colon at the end of the bracket of main.cpp file does not really create a problem.

I am sorry but i forgot to mention that people have the right to copy and paste the code in their compiler to see what problem i am coming across. i have mentioned above the codes which bit goes under the vending.h header file which code goes under the vending.cpp file and which code goes under the main.cpp file. After you copy and paste the code, all you have to do is compile it and it should be up and running. Please someone help me solve this problem quickly. Thanks!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.