Question

Write a complete C++ program as described below. Documentation is required; see section. Starter code and...

Write a complete C++ program as described below. Documentation is required; see section.

Starter code and input files are given to you. Please go through them on Titanium before beginning to program.

Each item that is scanned at a cash register reduces the grocery store’s inventory – i.e., the number of items decreases by one. Your program should code this logic.

A text file contains the initial inventory (i.e., number of items) of every type of item carried in the store in the following format:

23043 Apple       6

74001 Coke        12

10078 Eggs_6ct    10

10079 Eggs_12ct   6

34302 Milk_1qt    5

59004 Bread_wheat 5

Column 1 is the item code, column 2 is the item name, and column 3 is the number of items.

The list of scanned items is represented by an array of item codes (integers). For example, if the array is: [34302, 10078, 34302], the inventory after scanning should be printed as:

Apple   6

Coke 12

Eggs_6ct 9

Eggs_12ct 6

Milk_1qt 3

Bread_wheat 5

Implement your logic within a class GroceryInventory. The class should have the following public member functions:

  • GroceryInventory(string filename): the constructor which takes in the filename of the initial inventory and reads in the data to its member variables
  • int printInventory(int itemcodes[], int nItems): given an array of item codes, print the reduced inventory in the format shown above and returns the total number of items (41 in the example above)

These public member functions will be called from the provided main program and the answers printed there (see file part2.cpp)

You are free to add other (public/private) member variables and functions to class GroceryInventory as needed.

  • You can implement all your code either in one header file called GroceryInventory.h or split the declaration and definition in GroceryInventory.h and GroceryInventory.cpp
  • Class and filenames are case-sensitive. The class and filenames should be GroceryInventory (not Groceryinventory, groceryinventory, …)

Homework Answers

Answer #1

Note: part2.cpp is not provided in the question

Code for the above program is provided below. If any double, please comment below.

GroceryInventory.h

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

// Declaration of class
class GroceryInventory
{
   int itemCode[10];
   string itemName[10];
   int numOfItems[10];
   int count;
public:

   // Read data from file
   GroceryInventory(string filename)
   {
       // Open the file
       ifstream in;
       in.open(filename);
       int i=0;

       // Read values to array
       while(in>>itemCode[i])
       {
           in>>itemName[i];
           in>>numOfItems[i];
           i++;
       }
       // Total items
       count=i;
   }

   // Print the values
   int printInventory(int itemcodes[], int nItems)
   {

       // Parse through the list of scanned items
       for(int j=0;j<nItems;j++)
       {
           // Check the itemcode
           for(int i=0;i<count;i++)
           {
               // Decrement scanned item count
               if(itemcodes[j]==itemCode[i])
                   numOfItems[i]--;
           }
       }
       int tot=0;

       // Print each item
       for(int i=0;i<count;i++)
       {
           cout<<itemName[i]<<" "<<numOfItems[i]<<endl;

           // Find total items
           tot=tot+numOfItems[i];
       }
       return tot;
   }
};

part2.cpp

#include<iostream>
#include"GroceryInventory.h"
using namespace std;
int main()
{
   GroceryInventory gr("inp.txt");
   // Array of scanned items
   int arr[3]={34302,10078,34302};
   int count=gr.printInventory(arr,3);
   // Print the total
   cout<<"Total items = "<<count<<endl;
   system("pause");
}

Output:

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output...
*****C++ program***** Please implement the following, comments throughout code to explain, and provide screenshots of output for proof. Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Implement the following functions: Implement a function called readData int readData( int *arr) arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array arr....
In C++ Employee Class Write a class named Employee (see definition below), create an array of...
In C++ Employee Class Write a class named Employee (see definition below), create an array of Employee objects, and process the array using three functions. In main create an array of 100 Employee objects using the default constructor. The program will repeatedly execute four menu items selected by the user, in main: 1) in a function, store in the array of Employee objects the user-entered data shown below (but program to allow an unknown number of objects to be stored,...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the...
***C++ CODING*** Write a program for sorting a list of integers in ascending order using the bubble sort algorithm. Requirements Implement the following functions: Implement a function called readData int *readData( )   The function returns a pointer that points to the locations with integers reading from the file data.txt. arr is a pointer for storing the integers. The function returns the number of integers. The function readData reads the list of integers from a file call data.txt into the array...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads...
Here's the requirement. Write a client program Subset.java that takes a command-line integer k , reads in a sequence of strings from standard input using StdIn.readString() , and prints out exactly k of them, uniformly at random. Each item from the sequence can be printed out at most once. You may assume that 0 k N , where N is the number of string on standard input. The running time of the program must be linear in the size of...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below)...
Given the following specifications for an array-based unsorted list, implement all of the functions (declared below) and a write a driver code to test all of your implementations. // Define a structure to use as the list item struct ListItem { int key; int Data; }; #define MAX_SIZE 50 // Define maximum length of the list class UnsortedArray { private: int head; // Index to head of the list ListItem theList[MAX_SIZE]; // The list public: UnsortedArray(); // Class constructor ~...
Write a C program Design a program that uses an array to store 10 randomly generated...
Write a C program Design a program that uses an array to store 10 randomly generated integer numbers in the range from 1 to 50. The program should first generate random numbers and save these numbers into the array. It will then provide the following menu options to the user: Display 10 random numbers stored in the array Compute and display the largest number in the array Compute and display the average value of all numbers Exit The options 2...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
Write a code in c++ using linear insertion following the steps below. Comment your work. 1....
Write a code in c++ using linear insertion following the steps below. Comment your work. 1.    Ask the user for the name of a file containing data. If it does not exist, the program should display an error, then ask for a new file name. Entering an asterisk (*) as the first and only character on a line should terminate the program. 2.     You can use a statically-allocated one-dimensional array of doubles for this with length 100. You...
You are to write a C++ program to produce an inventory report for a local company....
You are to write a C++ program to produce an inventory report for a local company. Your input will be item name, item number, quantity, price per item, safe stock value. The following shows which columns the input will be in: item name             item number         quantity                  price                      safe stock 20 chars                 5 char                     3 char                      6 chars                 3 chars Output will be as follows: item number         item name    quantity   price     price*quantity   %ofStock    flag You will put a symbol in the...
In this assignment, you’ll make an inventory system for a store’s items, including produce and books....
In this assignment, you’ll make an inventory system for a store’s items, including produce and books. The starter program is an inventory system for only produce. 1. Include the price of an item by adding to the Item class the protected data member int priceInDollars that stores the price in dollars of an individual item. Write a public function SetPrice with a single int parameter prcInDllrs and returns nothing. SetPrice assigns the value of prcInDllrs to priceInDollars. Modify the AddItemToInventory...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT