C++ Project Name: IC22_Inventory Write a program that defines and uses a structu
ID: 3765033 • Letter: C
Question
C++ Project Name: IC22_Inventory
Write a program that defines and uses a structure (named Inventory) to store the following inventory data in a file:
Item Description (at most 50 characters)
Quantity on Hand
Wholesale Cost
Retail Cost
Date Added to Inventory (exactly 8 characters in the form MM/DD/YYYY)
The program should have a menu that allows the user to perform the following options:
Add new records to the file.
Display any record in the file - If this option is selected, ask user to enter item number (between 1 and total number of items in inventory). Validate user input to be within this range. Display the current inventory item information in the following format:
ITEM DESCRIPTION: Rim, 22"Diameter, Chrome, 3 Spokes
QUANTITY ON HAND: 14
WHOLESALE COST: $98.99
RETAIL COST: $179.99
DATE ADDED: 10/01/2014
Quit - exit the program
Input Validation: The program should not accept quantities, or wholesale or retail costs, less than 0.
Explanation / Answer
Answer:
program code:
#include <iostream>
#include <fstream>
#include <iomanip>
#include <cctype>
using namespace std;
const int DESC_SIZEOF = 51;
const int DATE_SIZEOF= 11;
struct InventoryItemStr
{
char desctin[DESC_SIZEOF];
int quantityof;
double whlCost1;
double rtlCost1;
char date[DATE_SIZEOF];
int menu();
};
int menu();
void addrecord_s(fstream &); // FUNCTION PROTOTYPE TO ADD A record_s
void disprecord_s(fstream &); // FUNCTION PROTOTYPE TO VIEW A record_s
void chgrecord_s(fstream &); // FUNCTION PROTOTYPE TO CHANGE A record_s
int main()
{
long selection1=0;
long recNumBB; // VARIABLE TO HOLD THE record_s NUMBER OF THE INVENTORY ITEM
fstream inventoryM ("Inventory.txt", ios::in | ios::out | ios::binary);
inventoryMItemStr record_s = {" ", 0, 0.0};
cout << fixed << showpoint << setprecision(2);
cout<<"Inventory Managment"<<endl;
// WRITE THE BLANK record_sS
for (int count = 0; count < 5; count++)
{
inventoryM.write(reinterpret_cast<char *>(&record_s),sizeof(record_s));
}
inventoryM.close();
inventoryM.open("Inventory.txt", ios::out | ios::binary);
while (selection1 != 4)
{
switch (selection1)
{
case 1: // ADD A NEW record_s
{
addrecord_s(inventoryM);
break;
}
case 2: //VIEW record_s
{
disprecord_s(inventoryM);
break;
}
case 3: //CHANGE record_s
{
chgrecord_s(inventoryM);
break;
}
default: //INVALID SELECTION1
{
cout << " INVALID SELECTION1" << endl;
}
selection1 =menu();
}
}
cout << "HAVA A NICE DAY." << endl;
inventoryM.close();
system("pause");
return 0;
}
int menu()
{
long selection1=0;
int choice1;
selection1 =menu();
cout << "PLEASE MAKE A SELECTION1, 1 THROUGH 4." << endl;
cout << "1. ADD A NEW record_s" << endl;
cout << "2. VIEW AN EXISITNG record_s" << endl;
cout << "3. CHANGE AN EXISITNG record_s" << endl;
cout << "4. EXIT" << endl;
cout << endl;
cout <<"ENTER YOUR CHOICE1 (1-4): ";
cin >> choice1;
while(choice1 < 1 || choice1 > 4)
{
cout << "INVAILD SELECTION1!" << endl;
cout << "PLEASE ENTER YOUR CHOICE1 (1-4) : ";
cin >> choice1;
}
cout << endl;
return choice1;
}
void addrecord_s(fstream &file) // FUNCTION TO ADD NEW INFORMATION TO INVENTORY
{
cout << "ENTER THE FOLLOWING INVENTORY DATA:"<<endl;
fstream inventoryM("Inventory.txt", ios::out | ios::binary);
InventoryItemStr record_s;
//GET NEW DATA
cout << "DESCRIPTION: ";
cin.ignore();
cin.getline(record_s.desctin, 51);
cin >> record_s.desctin;
cout << "QUANTITY: ";
cin >> record_s.quantityof;
cout << "WHOLESALE COST: ";
cin >> record_s.whlCost1;
cout << "RETAIL PRICE: ";
cin >> record_s.rtlCost1;
cout << "DATE ADDED TO INVENTORY (IN 00/00/0000 FORMAT): ";
cin >> record_s.date;
inventoryM.write(reinterpret_cast<char *>(&record_s),sizeof(record_s));
cout << "record_s ADDED TO FILE."<<endl;
file.close();
}
// FUNCTION TO VIEW record_s
void disprecord_s(fstream &file)
{
fstream inventoryM ("Inventory.txt", ios::out | ios::binary);
InventoryItemStr record_s;
long recNumB;
cout << "ENTER THE record_s NUMBER OF THE ITEM TO VIEW:";
cin >> recNumB;
// MOVE TO THE record_s AND READ IT.
inventoryM.seekg(recNumB * sizeof(record_s), ios::beg);
inventoryM.read(reinterpret_cast<char *>(&record_s),sizeof(record_s));
cout << "DESCRIPTION: " << record_s.desctin << endl;
cout << "QUANTITY: " << record_s.quantityof << endl;
cout << "WHOLESALE COST: " << record_s.whlCost1 << endl;
cout << "RETAIL PRICE: " << record_s.rtlCost1 << endl;
cout << "DATE (IN 00/00/0000 FORMAT): " << record_s.date << endl;
cout << endl;
if(file.fail())
file.clear();
file.close();
}
// FUNCTION TO CHANGE A record_s
void chgrecord_s(fstream &file)
{
fstream inventoryM ("Inventory.txt", ios::out | ios::binary);
InventoryItemStr record_s;
long recNumB;
cout << "ENTER THE record_s NUMBER OF THE ITEM YOU WANT TO EDIT: ";
cin >> recNumB; // MOVE TO THE record_s AND READ IT.
inventoryM.seekg(recNumB * sizeof(record_s), ios::beg);
inventoryM.read(reinterpret_cast<char *>(&record_s),sizeof(record_s));
cout << "DESCRIPTION: " << record_s.desctin << endl;
cout << "quantityof: " << record_s.quantityof << endl;
cout << "WHOLESALE COST: " << record_s.whlCost1 << endl;
cout << "RETAIL PRICE: " << record_s.rtlCost1 << endl;
cout << "DATE (IN 00/00/0000 FORMAT): " << record_s.date << endl;
cout << endl;
inventoryM.seekp(recNumB * sizeof(record_s), ios::beg);
inventoryM.write(reinterpret_cast<char *>(&record_s),sizeof(record_s));
inventoryM.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.