Question
C++ Create a function to remove the item with the given item name from the inventory. The inventory should stay sorted by item name after
C++
- Create a function to remove the item with the given item name from the inventory. The inventory should stay sorted by item name after the removal. The function returns true if the removal is successful and it returns false if there is no matching name item or the array is empty.
bool removeItem(InventoryItem list[], int& size, const char itemName[]);
- Sample test:
char itemName[] = banana;
if(removeItem(inventory, numberOfItems, itemName)== true)
{..}
main.h
#pragma once #include using namespace std; #include #include #include #include "item.h"
const int CAPACITY = 20;
void populateInventory(InventoryItem list[], int& size, const char fileName[]); void printInventory(const InventoryItem list[], int size);
int main(int argc, char ** argv); //add your function prototypes here
main.cpp
#include "main.h"
int main(int argc, char ** argv) { InventoryItem inventory[CAPACITY]; int numberOfItems = 0; char fileName[] = "items.txt";
populateInventory(inventory, numberOfItems, fileName);
//invoke your functions here to do what is required
printInventory(inventory, numberOfItems);
return 0; }
//add your funciton implementations here
void populateInventory(InventoryItem list[], int& size, const char fileName[]) { ifstream inFile;
char name[MAX_CHAR]; float price;
inFile.open(fileName);
if(!inFile) { cerr << "Fail to open " << fileName << " to populate inventory!" << endl; exit(1); }
inFile.get(name, MAX_CHAR, ':'); while(!inFile.eof()) { inFile.get(); //throw away the delimiter ':' inFile >> price; inFile.ignore(MAX_CHAR, ' '); //throw away ' '
strcpy(list[size].itemName, name);
list[size].itemPrice = price; size++;
inFile.get(name, MAX_CHAR, ':'); } inFile.close(); }
void printInventory(const InventoryItem list[], int size) { int colWidth = 20; cout << fixed; cout << setprecision(2);
cout << setw(colWidth) << "Item Name" << setw(colWidth) << "Item Price" << endl; cout << "-----------------------------------------------" << endl; for(int index = 0; index < size; index++) { cout << setw(colWidth) << list[index].itemName << setw(colWidth) << list[index].itemPrice << endl; }
}
items.txt
apple juice:4.99 banana:0.69 cookie:0.50 donut:1.00 egg:3.88 fishstick:4.88 milk:2.99 yogurt:2.38
item.h
#pragma once
const int MAX_CHAR = 101;
struct InventoryItem { char itemName[MAX_CHAR]; float itemPrice; };
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started