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

solve it in C++ and with comments please WORKING WITH ARRAY OF STRUCTURES / FUNC

ID: 3888957 • Letter: S

Question

solve it in C++ and with comments please

WORKING WITH ARRAY OF STRUCTURES / FUNCTIONS

Create a program to simulate a vending machine.

Define a structure called Item to hold the following information about each item:

Description

Price

Quantity in stock

Quantity sold

In main,create an array of these structures to represent the vending machine items. (Use a constant for the size of the array so it is easy to change). Initialize the data in the array. You can choose the number of items, the item descriptions, prices, in stock. Remember to start sold at 0 for each item.

Your application will:

1. Show the items available for purchase with a number by each one. The “first” item will be numbered as 1, but note that it is stored in element 0 of the array. (DO NOT make the array one bigger and ignore element 0). This should be done by a function.

2. Process a VALID purchase (valid item number, enough money, product in stock): a. User selects item b. User “enters” money c. Make sure there is enough money and the item is in stock. If so, display the item name. If there is change, show that

3. When all orders ended, print sales report. Should be done by a function.

4. Print items that need to be re-ordered. Should be done by a function.

You should create at least 3 functions:

showItems to print the items in the array

reportSales to print the sales report

reporReorders to print items that need to be re-ordered

Validation needed: valid item, enough money, product in stock. Give appropriate error messages as needed.

See sample output below, but DO NOT focus on going straight to sample output. Work on one specific thing at a time. Here is a suggested test plan. Remember to do testing EACH TIME.

1. Create the structure, array, initialize data, show the numbered items in vending machine. (create a function, or do without function then refactor to use a function).

2. Process a SINGLE purchase to make sure you have the proper processing steps: a. Prompt for valid item number to purchase. b. Enter amount of money. Loop to validate that there is enough money. c. Print the item and show change if any.

3. Add loop to process multiple orders, still valid choices only.

4. Modify process to check stock, give error message if out of stock. Update stock and sold.

5. Add validation loop for item number selected.

6. Write and test function to print sales report

7. Write and test function to print reorder report

Sample Output

VENDING MACHINE

1   chips           0.75

2   reeses        0.90

3   gummies     0.85

Enter item number or -1 to quit: 1

Enter money: .70

Insuficient, re-enter: .75

Dispensing product: chips

Enter item number or -1 to quit: 3

Invalid selection, re-enter: 1

Enter money: 1

Dispensing product: chips

Your change: 0.25

Enter item number or -1 to quit: 1

Item not in stock

Enter item number or -1 to quit: 2

Enter money: 1

Dispensing product: reeses

Your change: 0.10

Enter item number or -1 to quit: -1

SALES REPORT

ITEM         SOLD

chips            2

reeses         1

gummies      0

Total sales: $2.40

REORDER REPORT

Items that need to be restocked

chips

Press any key to continue . . .

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. chips " << data[0].price << endl;
   cout << "2. resse " << data[1].price << endl;
   cout << "3. gummies " << data[2].price << endl;

}

void reportSales(item data[]){

    cout << "ITEMS " << "SOLD" << endl;
    for (int i = 0; i<3; i++){
        cout << data[i].name << " " << data[i].qty_sold << endl;
    }
}

void reorderReport(item data[]){

    cout << "REORDER REPORT" << endl;
    cout << "Items that need to be restocked" << endl;
    for (int i = 0; i<3; i++){
        if (data[i].qty_in_stock == 0)
           cout << data[i].name << endl;
    }
}

int main(){

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

   data[0].name = "chips";
   data[0].price = 0.75;
   data[0].qty_in_stock = 1;
   data[0].qty_sold = 0;

   data[1].name = "resse";
   data[1].price = 0.90;
   data[1].qty_in_stock = 3;
   data[1].qty_sold = 0;

   data[2].name = "gummies";
   data[2].price = 0.85;
   data[2].qty_in_stock = 2;
   data[2].qty_sold = 0;
  

   do {
        showItems(data);
        cout << "Enter item number or -1 to quit: ";
        cin >> item_no;
        if (item_no == -1)
           break;
        while (item_no < 1 || item_no > 3) {
               cout << "Invalid selection. renter :" << endl;
               cin >> item_no;
        }

        if (item_no >=1 && item_no <= 3 ){
           if (data[item_no-1].qty_in_stock > 0){
              cout << "Enter money :";
              cin >> money;
              while (money < data[item_no-1].price) {
                       cout << "Insufficient, re-enter :";
                       cin>> money;
               }
               if (money >= data[item_no-1].price){
                 cout << "Dispensing Product:" << data[item_no-1].name << endl;
                 cout << "Your change :" << money - data[item_no-1].price << endl;
                 data[item_no-1].qty_in_stock--;
                 data[item_no-1].qty_sold++;
              }
           }
           else {
              cout << "Item not in stock" << endl;
           }
     
        }
   } while (item_no != -1);
   reportSales(data);
   reorderReport(data);
   return 0;
}