Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Part 2: Binary Files Write a program that uses a structure to store the followin

ID: 3913128 • Letter: P

Question

Part 2: Binary Files Write a program that uses a structure to store the following inventory data in a file: The data can be either read from a text file or the keyboard .Item name (string) . Quantity on hand(int) Wholesale cost(double) Retail Cost(double) The program should have a menu that allows the user to perform the following tasks: Add new records to the file Display any record in the file 1. 2. 3. Change any record in the file (Note: if this option chosen, then the whole record must be 4. a. User will provide the name of the item or the first n letters of the name of the item reentered even if the user wants to change only one or more fields) Display all records Prepare a report containing: 5.

Explanation / Answer

here is your modified code : ------------>>>>>>>>

#include<iostream>
#include<fstream>

using namespace std;

struct Item{
string name;
int qty;
double wholesale;
double retail;
};

void addRecord(fstream &,struct Item);
void viewRecord(fstream &,struct Item);
void changeRecord(fstream &);

int main(){
Item info;
int choice;

fstream inventory("inventory.dat",ios::in | ios::out | ios::binary);

do{
  cout<<"Please Enter an option(1-5): "<<endl;
  cout<<"1-Add new records to the file"<<endl;
  cout<<"2-View an existing record by typing the name";
  cout<<" of the item or the first n letters of the name of item"<<endl;
  cout<<"3-Change an existing record"<<endl;
  cout<<"4-Display all records:"<<endl;
  cout<<"5-Exit"<<endl;
  cin>>choice;
  cin.ignore();
  
  switch(choice){
   case 1:addRecord(inventory,info);
   break;
   case 2:viewRecord(inventory,info);
   break;
   case 3:changeRecord(inventory);
   break;
   case 4:
   break;
   case 5:
   break;
   default:cout<<"Invalid input"<<endl;
  }
  
}while(choice != 5);

inventory.close();
return 0;
}


void addRecord(fstream &inventory,struct Item info){
char again;
inventory.seekg(0,ios::end);
do{
  cout<<"Enter the following inventory data : "<<endl;
  cout<<"Enter the name: ";
  getline(cin,info.name);
  
  cout<<"Enter the Quantity : ";
  cin>>info.qty;
  
  cout<<"Enter the Wholesale cost: ";
  cin>>info.wholesale;
  
  cout<<"Enter the Retail price : ";
  cin>>info.retail;
  cin.ignore();
  
  if(info.qty < 0 || info.wholesale < 0 || info.retail < 0){
   cout<<"Quantity,Wholesale cost or Retail price can not be less than 0 "<<endl;
  }
}while(info.qty < 0 || info.wholesale < 0 || info.retail < 0);

inventory.write(reinterpret_cast<char *>(&info),sizeof(info));

cout<<"Do you want to enter another record(Enter Y or N)? "<<endl;
cin>>again;

cin.ignore();
if(again == 'Y' || again == 'y'){
  addRecord(inventory,info);
}
}

void viewRecord(fstream &inventory,struct Item info){
string n;
inventory.seekg(0,ios::beg);
if(!inventory){
  cout<<"Error opening file. Program aborting. ";
  return;
}
else{
  cin.ignore();
  cout<<"Please type the name or first n letters of the item ";
  cin>>n;
  
  while(inventory.read(reinterpret_cast<char *>(&info),sizeof(info))){
   if(info.name.find(n) != std::string::npos){
    cout<<"Name: ";
    cout<<info.name<<endl;
    cout<<"Quantity: ";
    cout<<info.qty<<endl;
    cout<<"Wholesale cost: ";
    cout<<info.wholesale<<endl;
    cout<<"Retail price: ";
    cout<<info.retail<<endl;
   }
  }
}
}

void changeRecord(fstream &inventory){
struct Item record;
long recNum;
inventory.seekg(0,ios::beg);
cout<<"Which record do you want to edit? ";
cin>>recNum;

inventory.seekg(recNum*sizeof(record),ios::beg);
inventory.read(reinterpret_cast<char *>(&record),sizeof(record));

cout<<"Name: ";
cout<<record.name<<endl;
cout<<"Quantity: ";
cout<<record.qty<<endl;
cout<<"Wholesale cost: ";
cout<<record.wholesale<<endl;
cout<<"Retail price: ";
cout<<record.retail<<endl;

cin.ignore();
cout<<"Enter the new data : "<<endl;
cout<<"Description: ";
getline(cin,record.name);

cout<<"Quantity : ";
cin>>record.qty;

cout<<"Wholesale cost: ";
cin>>record.wholesale;

cout<<"Retail price : ";
cin>>record.retail;
cin.ignore();

inventory.seekp(recNum*sizeof(record),ios::beg);

inventory.write(reinterpret_cast<char *>(&record),sizeof(record));
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote