1

I am currently working on a vending machine program. However, I am not able to figure out how to ask the user for input and also print out the options and prices. This is a part of my code, my question is : How can i ask the user for an input in main() and get the options from the Base subclass.

Thank you in advance.

class Product
{
  protected:
    string product_description;
    int itemCost = 0;

  public:
    virtual void consume(void)
    {
    }

    virtual int cost(void) { return this->itemCost; }
    virtual string description(void) { return product_description; }
    virtual Product* ReturnHighestCostItem(void)
    {
      return this;
    }
    virtual void RemoveHighestCostItem(void)
    {
      return;
    }
};

class Base : public Product
{
   public:
     Base(int option)
     {
       if (option == 1)
       {
         this->product_description = "Plain";
         this->itemCost = 100;
       }
       else if (option == 2)
       {
         this->product_description = "Spicy";
         this->itemCost = 150;
       }
       else if (option == 4)
       {
         this->product_description = "Chocolate";
         this->itemCost = 200;
       }
       else if (option == 8)
       {
         this->product_description = "Coconut";
         this->itemCost = 200;
       }
       else if (option == 16)
       {
         this->product_description = "Fruity";
         this->itemCost = 200;
       }
    }
};

int main()
{
   Product obj; 
   cout << "The Available descriptions are:" << obj.description()<< endl;
   return 0;
}
Vishaal Shankar
  • 1,648
  • 14
  • 26
  • 1
    And where is your main()? Also have you heard about std::cin and std::cout? – Amadeusz Feb 07 '18 at 17:13
  • See: https://stackoverflow.com/questions/7944861/getting-user-input-in-c – Nick Feb 07 '18 at 17:14
  • 2
    Consider using `switch/case` instead of `if/else if`. – Jabberwocky Feb 07 '18 at 17:15
  • Use `switch` , instead of `if else if`. – NishanthSpShetty Feb 07 '18 at 17:15
  • @user7716102 And what's your problem with just using [`std::cin`](http://en.cppreference.com/w/cpp/io/cin) to proceed? –  Feb 07 '18 at 17:20
  • @user7716102 Use std::cin. Follow link given by Nick – Amadeusz Feb 07 '18 at 17:20
  • As it's already been pointed out you need to use `std::cin` to get users input. Check out [my answer](https://stackoverflow.com/questions/48669567/vending-machine-c/48671355#48671355) for some examples and other considerations. As far as product descriptions, it seems like you might need a `VendingMachine` class that can contain your inventory of `Products`... – Justin Randall Feb 07 '18 at 19:08

1 Answers1

0

A vending machine has a restricted input. It shouldn't take a user's money unless the user enters a valid address. Most vending machines I've used take a letter that designates the row, followed by a number that designates the column i.e. upper left hand corner would typically be A0. The item to the right of that might be A1 or A2 etc. Each Product in the vending machine needs to be mapped to one of these addresses. The user will likely input money (or credit?) and then input their selection. You will have to validate their selection i.e. the address they specified is valid for a particular product, and that they have enough credit to purchase that product. To better visualize your requirements you can refer to this picture.vending machine input

You should familiarize yourself with std::cin and obtaining user input. You could store the input as a std::string to make it easy to validate and lookup the associated Product to vend.

#include <iostream>
#include <iomanip>
#include <string>
#include <limits>

int main()
{
    double credits;       ///< user input for credits
    std::string selection;  ///< user input for vending address

    std::cout << "Welcome to vending machine!" << std::endl;
    std::cout << "Please input credits to purchase an item ex. 1.00 or 5.00" << std::endl;
    std::cout << "Ready -> " << std::flush;

    // we need to enforce that the user entered a number here
    while(!(std::cin >> credits))
    {
        std::cout << "Invalid value!\nPlease input credits to purchase an item! ex. 5.00" << std::endl;
        std::cout << "Ready -> " << std::flush;
        std::cin.clear();
        std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    }

    // (optional) format output stream for displaying credits i.e. 1.00
    std::cout << std::fixed;
    std::cout << std::setprecision(2);

    std::cout << "Please make a selection (A-J,0-8) ex. B6 or A2" << std::endl;
    std::cout << "Ready (cr. " << credits << ") -> " << std::flush;
    // any input can be a std::string so we will need to post-process to make sure
    // it's a valid input (format is correct and the product exists to vend)
    std::cin >> selection;
    // TODO: validate the users selection!

    return 0;
}
Justin Randall
  • 2,243
  • 2
  • 16
  • 23