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

#include <stdlib.h> #include <iostream> #include <string> #include <fstream> #in

ID: 3539278 • Letter: #

Question

#include <stdlib.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cstdlib>
#include <cstdio>

using namespace std;

const int size=20;

struct Info
    {
           char SKU[size];
           string Category;
     int Quantity;
           double Cost;
           string Date;
    };


void getrecord(fstream &);
void displayrecord(fstream &);
void editrecord(fstream &);
void wholesaleitem(fstream &, char[]);
void wholesalecate(fstream &, string);
void quantityitem(fstream &);


int main()
{
Info record={"","",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;
string cate;
char SKU[size];
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;
    
    case item:
       {
      wholesaleitem(Info, SKU);
                   }
                    break;
    case category:
       {
      wholesalecate(Info, cate);
                   }
                    break;
    case quantity:
       {
      quantityitem(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.ignore();
cin.getline(record.SKU, size);
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 SKU 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();
}
void wholesaleitem(fstream &file,char SKU[size])
{
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);

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 wholesalecate(fstream &file,string category)
{
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);

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 quantityitem(fstream &file)
{
Info record;
fstream Info("name_of_your_choice.dat", ios::out | ios::binary);

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();
}

so i my in put

but my output file like

asd %u5106%u5106%u5106%u5106%u5106%u5106%u5106%u5106 6H as %u5106%u5106%u5106%u5106%u5106%u5106?      %u5106%u5106         ?@%uAC0E 1 %u5106%u5106%u5106%u5106%u5106%u5106%u5106      %u5106%u5106

....... what is m problem really...

Explanation / Answer

#include <stdlib.h>

#include <iostream>

#include <string>

#include <fstream>

#include <cstdlib>

#include <cstdio>


using namespace std;


const int size=20;


struct Info

{

char SKU[size];

string Category;

int Quantity;

double Cost;

string Date;

};



void getrecord(fstream &,long);

void displayrecord(fstream &);

void editrecord(fstream &);

void wholesaleitem(fstream &, string);

void wholesalecate(fstream &, string);

void quantityitem(fstream &);



int main()

{

Info record={"","",0,0.0,""};

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


/* for(int i=0; i<20; i++)

{

file.write(reinterpret_cast<char *>(&record),sizeof(Info));

} */

file.close();


int menu;

long i=1;

string cate;

string SKU;

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(file,i);

i++;

}

break;

case print:

{

displayrecord(file);

}

break;

case change:

{

editrecord(file);

}

break;

  

case item:

{

cout<<"choose SKU(Stock Keeping Unit)";

cin>>SKU;

wholesaleitem(file, SKU);

}

break;

case category:

{

cout<<"choose category";

cin>>cate;

wholesalecate(file, cate);

}

break;

case quantity:

{

quantityitem(file);

}

break;

}

}while (menu != leave);

system("pause");

return 0;


}

void getrecord(fstream &file,long i)

{

cout <<"Please enter information ";

// Info("name_of_your_choice.dat", ios::out | ios::binary);

file.open("name_of_your_choice.dat",ios::in|ios::out | ios::binary);

Info record={"","",0,0.0,""};

cout<<"Enter SKU(Stock Keeping Unit): ";

cin.ignore();

cin.getline(record.SKU, size);

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;

file.seekp(i * sizeof(Info), ios::beg);

file.write(reinterpret_cast<char *>(&record),sizeof(Info));

cout<<"Record added to file."<<endl;

cout<<endl;


file.close();

}



void displayrecord (fstream &file)

{

//Info("name_of_your_choice.dat", ios::out | ios::binary);

file.open("name_of_your_choice.dat",ios::out|ios::in | ios::binary);

Info record={"","",0,0.0,""};

long i;

cout<<"Enter the record number of the item to view:";

cin>>i;


file.seekg(i * sizeof(Info), ios::beg);


file.read(reinterpret_cast<char *>(&record),sizeof(Info));

cout<<file.tellg()<<endl<<sizeof(Info)<<endl;

cout<<" SKU(Stock Keeping Unit): "<<record.SKU<<endl;

cout<<" Category: "<<record.Category<<endl;

cout<<" Quantity on Hand: "<<record.Quantity<<endl;

cout<<" 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;

file.open("name_of_your_choice.dat", ios::in |ios::out | ios::binary);


long i;


cout << "Please enter the SKU 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;


file.seekp(i * sizeof(Info), ios::beg);

file.write(reinterpret_cast<char *>(&record),sizeof(Info));


file.close();

}

void wholesaleitem(fstream &file,string SKU)

{

Info record;

file.open("name_of_your_choice.dat", ios::in|ios::out | ios::binary);

double sum = 0;

while(!file.eof())

{

file.read(reinterpret_cast<char *>(&record),sizeof(Info));

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 wholesalecate(fstream &file,string category)

{

Info record;

file.open("name_of_your_choice.dat", ios::in |ios::out | ios::binary);


double sum = 0;

while(!file.eof())

{

file.read(reinterpret_cast<char *>(&record),sizeof(Info));

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 quantityitem(fstream &file)

{

Info record;

file.open("name_of_your_choice.dat", ios::in |ios::out | ios::binary);


double sum = 0;

while(!file.eof())

{

file.read(reinterpret_cast<char *>(&record),sizeof(Info));

sum += record.Quantity;

}


cout<<"The total Quantity of all items in the Inventory is "<<sum<<endl;

if(file.fail())

file.clear();

file.close();

}