Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a vending machine that offers the following options; 1) Get gum 2) Get

ID: 3890968 • Letter: C

Question

Consider a vending machine that offers the following options;
1) Get gum
2) Get Chocolate
3) Get Popcorn
4) Get juice
5) Display total sold so far
6) Quit
Design and implement a program that continuously allows a user to select from these options. When options 1-4 are selected an appropriate message is to be displayed acknowledged their choice. For example, when option 3 is selected the following message could be displayed; "Here is your popcorn"
When option 5 is selected, the number f each type of item sold is displayed. For example;
3 items of gum sold
2 items of chocolate sold
6 items of popcorn sold
9 items of juice sold
When option 6 is chosen the program terminates. If an option other than 1-6 is entered an appropriate error message should be displayed such as;
Error, options 1-6 only!

Explanation / Answer

#include<iostream>
#include<string>

using namespace std;

struct item {
    string name;
    double price;
    int qty_in_stock;
    int qty_sold;
};

void showItems(item data[]){

   cout << "VENDING MACHINE" << endl;
   cout << "1. Get gum " << endl;
   cout << "2. Get Chocolate" << endl;
   cout << "3. Get Popcorn" << endl;
   cout << "4. Get Juice" << endl;
   cout << "5. Display total sold so far" << endl;
   cout << "6. Quit " << endl;

}

void reportSales(item data[]){
    for (int i = 0; i<4; i++){
        cout << data[i].qty_sold << " items of " << data[i].name << " " << " sold" << endl;
    }
}

int main(){

   item data[4];
   int item_no;
   double money;

   data[0].name = "Gum";
   data[0].qty_sold = 0;

   data[1].name = "Chocolate";
   data[1].qty_sold = 0;

   data[2].name = "Popcorn";
   data[2].qty_sold = 0;
  
   data[3].name = "Juice";
   data[3].qty_sold = 0;

   do {
        showItems(data);
        cout << "Enter your choice : ";
        cin >> item_no;
        if (item_no == -1)
           break;
        while (item_no < 1 || item_no > 6) {
               cout << "Error, options 1-6 only! " << endl;
               cin >> item_no;
        }
        if (item_no >= 1 && item_no <=4) {
           cout << "Here is your " << data[item_no-1].name << endl;
           data[item_no-1].qty_sold++;
        }
        if (item_no == 5){
           reportSales(data);  
        }
       
   } while (item_no != 6);
  
  
   return 0;
}