C++ Write two programs which will work with a structure. a) The first program wi
ID: 3670798 • Letter: C
Question
C++
Write two programs which will work with a structure. a) The first program will use a structure to store the following inventory data in a file: Item Description Quantity on Hand Wholesale Cost Retail Cost Date Added to Inventory The program will write five records to an output file for different items; the quantity, costs, and date for each item will be different from the other items. b) The second program will read the file created by the first program using the same structure. The second program will calculate and display the following information: total wholesale value of the inventory, total retail value of the inventory, total quantity of all items in the inventory
Explanation / Answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
const int DES = 20;
const int DATE = 10;
void Add();
void Show();
void Edit();
struct Inventory{
int quantity;
char item[DES];
char date[DATE];
double retail;
double wholesale;
};
int main(){
cout << "Options: 1) Add a record. 2) Show a record. 3) Edit a record. ";
cout << "WARNING: Anything other than the options listed above will close the program. ";
int answer = 1;
while(answer > 0 && answer < 4){
cout << " Enter in one choice from 1-3:";
cin >> answer;
switch(answer){
case 1: Add();
break;
case 2: Show();
break;
case 3: Edit();
break;
default: cout << "ERROR: INVALID CHOICE PAUSING... ";
}
if(answer < 1 || answer >= 4){
cout << " To Continue: Choose from options 1, 2, or 3. ";
cout << "To Exit: Enter anything else. Answer:";
cin >> answer;
switch(answer){
case 1: Add();
break;
case 2: Show();
break;
case 3: Edit();
break;
default: cout << "Exiting.... ";
}
}
}
cout << " Program Terminated ";
return 0;
}
void Add(){
ofstream fout;
fout.open("inventory.txt",ios::out);
Inventory inventory;
int num;
//sets the product number to find in file
cout << "WARNING: Will overwrite products with the same PRODUCT ID. ";
cout << "Enter the PRODUCT ID of the record: ";
cin >> num;
cout << num << " was set as the PRODUCT ID for this item. ";
//enter the information
cout << "Enter the items Description:";
cin.ignore();
cin.getline(inventory.item, DES);
cout << "Enter the amount currently on hand:";
cin >> inventory.quantity;
if(inventory.quantity < 0){
while(inventory.quantity < 0){
cout << "Amount cant be under Nothing(0). ";
cout << "Please correctly enter the correct amount currently on hand:";
cin >> inventory.quantity;
}
}
cout << "Enter the retail cost for the item:";
cin >> inventory.retail;
if(inventory.retail < 0.01){
while(inventory.retail < 0.01){
cout << "Price cant be under $0.01. Please correctly enter the correct price:";
cin >> inventory.retail;
}
}
cout << "Enter the wholesale cost for the item:";
cin >> inventory.wholesale;
if(inventory.wholesale < 0.01){
while(inventory.wholesale < 0.01){
cout << "Price cant be under $0.01. Please correctly enter the correct price:";
cin >> inventory.wholesale;
}
}
cout << "Enter the date that it was added into the inventory.(Format: MM/DD/YYYY): ";
cin >> inventory.date;
fout.seekp(num*sizeof(inventory),ios::beg);
fout.write(reinterpret_cast<char*>(&inventory),sizeof(inventory));
fout.close();
}
void Show(){
ifstream fin;
fin.open("inventory.txt",ios::in);
Inventory inventory;
int num;
cout << "Please Enter the PRODUCT ID: ";
cin >> num;
fin.seekg(num*sizeof(inventory),ios::beg);
fin.read(reinterpret_cast<char*>(&inventory),sizeof(inventory));
cout << " Displaying Item.... ";
cout << "|=========================================|";
cout << " Description ----- ";
cout << inventory.item;
cout << " Quantity -------- ";
cout << inventory.quantity;
cout << " Whosesale Cost:-- $";
cout << inventory.wholesale;
cout << " Retail ---------- $";
cout << inventory.retail;
cout << " Date ------------ ";
cout << inventory.date << endl;
cout <<"|=========================================| ";
fin.close();
}
void Edit(){
fstream fout;
fout.open("inventory.txt",ios::in|ios::out);
Inventory inventory;
int num;
cout << "Enter the Product ID of the item you want edited:";
cin >> num;
fout.seekg(num*sizeof(inventory),ios::beg);
fout.read(reinterpret_cast<char*>(&inventory),sizeof(inventory));
cout << " Displaying Item.... ";
cout << "|=========================================|";
cout << " Description ----- ";
cout << inventory.item;
cout << " Quantity -------- ";
cout << inventory.quantity;
cout << " Whosesale Cost:-- $";
cout << inventory.wholesale;
cout << " Retail ---------- $";
cout << inventory.retail;
cout << " Date ------------ ";
cout << inventory.date << endl;
cout <<"|=========================================| ";
cout << "Enter the new information. ";
cout << "Enter the items Description:";
cin.ignore();
cin.getline(inventory.item, DES);
cout << "Enter the amount currently on hand:";
cin >> inventory.quantity;
cout << "Enter the retail cost for the item:";
cin >> inventory.retail;
cout << "Enter the wholesale cost for the item:";
cin >> inventory.wholesale;
cout << "Enter the date that it was added into the inventory.(Format: MM/DD/YYYY): ";
cin >> inventory.date;
fout.seekg(num*sizeof(inventory),ios::beg);
fout.write(reinterpret_cast<char*>(&inventory),sizeof(inventory));
fout.close();
}
output
1) Add a record.
Options:
1) Add a record.
2) Show a record.
3) Edit a record.
WARNING: Anything other than the options listed above will close the program.
Enter in one choice from 1-3:
/*
Write a program that reads the information in the file created by the program in Programming
Challenge 12. The program should calculate and display the following information:
• The total wholesale value of the inventory
• The total retail value of the inventory
• The total quantity of all items in the inventory
*/
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
struct Inven
{
string Desc, // Item decpription
Date; // Date added to inventory
int Qty; // Quantity on hand
double WhsleCost, // Wholesale cost
RetailCost; // Retail cost
};
int main()
{
Inven record;
double Totwhsle = 0, // Accumulates total wholesale value
TotRetail = 0; // Accumulates total Retail value
int TotQty = 0; // Accumulates total quantity of all items
fstream File("inventory.dat", ios::in | ios::binary);
if (!File)
{
cout << "Error open file, aborting program. ";
return 0;
}
File.read(reinterpret_cast<char *>(&record), sizeof(record));
while (!File.fail())
{
Totwhsle += record.WhsleCost * record.Qty;
TotRetail += record.RetailCost * record.Qty;
TotQty += record.Qty;
File.read(reinterpret_cast<char *>(&record), sizeof(record));
}
File.close();
cout << fixed << showpoint << setprecision(2);
cout << "Total wholesale value of the inventory: $"
<< setw(10) << Totwhsle << endl;
cout << "Total retail value of the inventory: $"
<< setw(10) << TotRetail << endl;
cout << "Total quantity of all items in the inventory: $"
<< setw(10) << TotQty << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.