Add new records to the file. and this is my code #include <iostream> #include <s
ID: 3539171 • Letter: A
Question
Add new records to the file.
and this is my code
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
struct Info
{
int SKU;
string Category;
int Quantity;
double Cost;
string Date;
};
void getrecord(fstream &);
void displayrecord(fstream &);
void editrecord(fstream &);
void wholesaleitem(fstream &);
void wholesalecate(fstream &);
void quantityitem(fstream &);
int main()
{
Info record={0,"",0,0.0,""};
fstream Info("name_of_your_choice.dat", ios::in | ios::out | ios::binary);
for(int i=0; i<20; i++)
{
Info.write(reinterpret_cast<char *>(&record),sizeof(record));
}
Info.close();
Info.open("name_of_your_choice.dat",ios::out | ios::binary);
int menu;
const int enter = 1,
print = 2,
change = 3,
item = 4,
category = 5,
quantity = 6,
leave = 7;
do
{
cout << "Inventory Program. "
<< endl
<< "1) Add new records to the file. "
<< "2) Display any record in the file. "
<< "3) Change any record in the file. "
<< "4) Display total wholesale value of a particular item inventory. "
<< "5) Display total wholesale value of a particular category inventory. "
<< "6) Display total quantity of all items in the inventory. "
<< "7) leave "
<< "Selection: ";
cin >> menu;
cout<< endl;
switch (menu)
{
case enter:
{
getrecord(Info);
}
break;
case print:
{
displayrecord(Info);
}
break;
case change:
{
editrecord(Info);
}
break;
}
}while (menu != leave);
Info.close();
system("pause");
return 0;
}
void getrecord(fstream &file)
{
cout <<"Please enter information ";
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);
cout<<"Enter SKU(Stock Keeping Unit): ";
cin>>record.SKU;
cout<<endl<<"Enter Category: ";
cin>>record.Category;
cout<<endl<<"Enter Quantity on Hand: ";
cin>>record.Quantity;
cout<<endl<<"Enter Wholesale Cost: ";
cin>>record.Cost;
cout<<endl<<"Date Added to Inventory(mm/dd/yyyy): ";
cin>>record.Date;
Info.write(reinterpret_cast<char *>(&record),sizeof(record));
cout<<"Record added to file."<<endl;
cout<<endl;
file.close();
}
void displayrecord (fstream &file)
{
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);
long i;
cout<<"Enter the record number of the item to view:";
cin>>i;
Info.seekg(i * sizeof(record), ios::beg);
Info.read(reinterpret_cast<char *>(&record),sizeof(record));
cout<<"Enter SKU(Stock Keeping Unit): "<<record.SKU<<endl;
cout<<"Enter Category: "<<record.Category<<endl;
cout<<"Enter Quantity on Hand: "<<record.Quantity<<endl;
cout<<"Enter Wholesale Cost: "<<record.Cost<<endl;
cout<<"Date Added to Inventory(mm/dd/yyyy): "<<record.Date<<endl;
cout<<endl;
if(file.fail())
file.clear();
file.close();
}
void editrecord(fstream &file)
{
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);
long i;
cout << "Please enter the speaker you want to edit."<<endl;
cin >> i;
cout<<"Enter SKU(Stock Keeping Unit): ";
cin>>record.SKU;
cout<<endl<<"Enter Category: ";
cin>>record.Category;
cout<<endl<<"Enter Quantity on Hand: ";
cin>>record.Quantity;
cout<<endl<<"Enter Wholesale Cost: ";
cin>>record.Cost;
cout<<endl<<"Date Added to Inventory(mm/dd/yyyy): ";
cin>>record.Date;
cout<<endl;
Info.seekp(i * sizeof(record), ios::beg);
Info.write(reinterpret_cast<char *>(&record),sizeof(record));
Info.close();
}
but i dont know how to do
Display total wholesale value of a particular item inventory.
Display total wholesale value of a particular category inventory.
Display total quantity of all items in the inventory.
and this is right code???
Explanation / Answer
i have assumd that an item is identified by the SKU. You can also return the result instead of printing the result by just changinf the return types to double and returning the sum from each function. Hope this helps :)
void getTotalWholesaleValueOfItem(fstream &file,int SKU){
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);
long i;
double sum = 0;
while(!Info.eof()){
Info.read(reinterpret_cast<char *>(&record),sizeof(record));
if(record.SKU == SKU){
sum += record.Cost;
}
}
cout<<"The total Wholesale Cost of the item "<<record.SKU<<" is : "<<sum<<endl;
if(file.fail())
file.clear();
file.close();
}
void getTotalWholesaleValueOfCategory(fstream &file,int category){
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);
long i;
double sum = 0;
while(!Info.eof()){
Info.read(reinterpret_cast<char *>(&record),sizeof(record));
if(record.Category == category){
sum += record.Cost;
}
}
cout<<"The total Wholesale Cost of the Category "<<record.Category<<" is : "<<sum<<endl;
if(file.fail())
file.clear();
file.close();
}
void getTotalQuantity(fstream &file){
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);
long i;
double sum = 0;
while(!Info.eof()){
Info.read(reinterpret_cast<char *>(&record),sizeof(record));
sum += record.Quantity;
}
cout<<"The total Quantity of all items in the Inventory is "<<sum<<endl;
if(file.fail())
file.clear();
file.close();
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.