So I already have most of the code and now I have to 1 creat

So I already have most of the code and now I have to:

1. create an index file to keep track of all the inventory IDs and their locations

2. modify my class to be able to display any record with a given ID without searching through the entire file

// This program displays the contents of the inventory file.
#include <iostream>
#include <fstream>
#include<string>
using namespace std;
class DB{
// Declaration of InventoryItem structure
class InventoryItem
{
public:
char Id[5];
char desc[31];
int qty;
float price;
};
string DBname;
fstream inventory;// (DBname, ios::in | ios::binary);
public: DB(string N){ DBname = N; }// constructor
private:void Input(InventoryItem &Inv)
{
cout << \"Please type Id\" << endl;
cin >> Inv.Id;
   cout << \"Please type Desc\" << endl;
   cin >> Inv.desc;
   cout << \"Please type qty\" << endl;
   cin >> Inv.qty;
   cout << \"Please type price\"<<endl;
   cin >> Inv.price;
}
public: void Create(int Nrec)
  {
  inventory.open(DBname, ios::out | ios::binary);
  // fstream inventory(\"Inventory.dat\", ios::out | ios::binary);
   InventoryItem record = { \"\", \" \", 0, 0.0 };

   // Write the blank records
   for (int count = 0; count < Nrec; count++)
   {
   Input(record);
   cout << \"Now writing record \" << count << endl;
   inventory.write(reinterpret_cast<char *>(&record), sizeof(record));
   }

   // Close the file.
   inventory.close();
   return ;
   }

public: void Display()
{
inventory.open(DBname, ios::in | ios::binary);

InventoryItem record = { \"\",\"\", 0, 0.0 };

// Now read and display the records
inventory.read(reinterpret_cast<char *>(&record),
sizeof(record));
while (!inventory.eof())
{
cout << \"Inventory Id: \";
cout << record.Id << endl;
cout << \"Description: \";
cout << record.desc << endl;
cout << \"Quantity: \";
cout << record.qty << endl;
cout << \"Price: \";
cout << record.price << endl << endl;
inventory.read(reinterpret_cast<char *>(&record), sizeof(record));
}

// Close the file.
inventory.close();

return;
}
};
int main()
{
DB Mydb(\"Inventory.dat\"); // declare a database
Mydb.Create(3); // load data
cout << \"***** display ***\" << endl;
Mydb.Display(); // print entire database
// implement the following function
// Mydb.Show(\"AB001\"); // display record with given ID
char C; cin >> C; return 0;
}

Solution

Programming Code in C++

#include <cstdlib>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;

//Structure.
struct InvStruct
{
string iName;
int iQuantity;
double iPrice;
};
const int MAXSIZE = 9;


void addToInventory(InvStruct aList[], int& aSize);
void displayInventory(const InvStruct aList[], int aSize);
void saveToFile(const InvStruct aList[], int aSize);
void openAFile(InvStruct aList[], int& aSize);
char menuResponse();


int main(int argc, char *argv[])
{


InvStruct iRecords[MAXSIZE];
int noOfRec = 0;
bool reRun = true;
do
{
cout << \"Inventory has \" << noOfRec << \" items\" << endl;
switch (menuResponse())
{
case \'A\':
addToInventory(iRecords, noOfRec);
break;
case \'D\':
displayInventory(iRecords, noOfRec);
break;
case \'O\':
openAFile(iRecords, noOfRec);
break;
case \'S\':
saveToFile(iRecords, noOfRec);
break;
case \'Q\':
reRun = false;
break;
default:
cout << \"Invalid Selection\" << endl;
}
} while (reRun);
cout << endl << \"Code Ends\" << endl;
return EXIT_SUCCESS;
}

//Method definition addToInventory().
void addToInventory(InvStruct aList[], int& aSize)
{
InvStruct temp;
char reRun;
char iStr[256];
if (aSize < MAXSIZE)
{
system(\"cls\");
cout << \"Enter data to inventory! \" << endl;
cout << \"Enter data: \" << endl << endl;
cout << \"Name: \";
cin.getline(iStr, 256, \'\ \');
temp.iName = iStr;
cout << \"Quantity: \";
cin >> temp.iQuantity;
cout << \"Value: \";
cin >> temp.iPrice;
cout << endl;
cout << \"Add more data to the inventory? (y/n) \";
cin >> reRun;
if (toupper(reRun) == \'Y\')
aList[aSize++] = temp;
}
else
{
cout << \"Can\'t add data! Inventory is full!!\" << endl;
system(\"pause\");
}
system(\"cls\");
}

//Method definition displayInventory().
void displayInventory(const InvStruct aList[], int aSize)
{
system(\"cls\");
double iCost = 0;
if (aSize < 1)
{
cout << \"No data to display\" << endl;
}
else
{
cout << \"All data in the inventory\" << endl << endl;
cout << fixed << setprecision(2);
cout << \"Name Quantity Price\" << endl;
cout << \"--------------------------------------\" << endl;
cout << left;
for (int inc = 0; inc < aSize; inc++)
{
cout << setw(21) << aList[inc].iName << right
<< setw(4) << aList[inc].iQuantity
<< setw(10) << aList[inc].iPrice << left << endl;
iCost = iCost + aList[inc].iPrice * aList[inc].iQuantity;
}
cout << \"--------------------------------------\" << endl;
cout << right << setw(3) << aSize;
cout << \" data listed\";
cout << right << setw(18) << iCost << endl << endl;
}
system(\"PAUSE\");
system(\"cls\");
}

//Method definition saveToFile()
void saveToFile(const InvStruct aList[], int aSize)
{
ofstream outf(\"Inventory.dat\");
if (!outf.fail())
{
system(\"cls\");
cout << \"Save the inventory\";
for (int inc = 0; inc < aSize; inc++)
{
outf << aList[inc].iName << \';\'
<< aList[inc].iQuantity << \';\'
<< aList[inc].iPrice;
if (inc < aSize - 1)
outf << endl;
}
cout << endl << aSize << \" data written.\" << endl;
outf.close();
system(\"PAUSE\");
system(\"cls\");
}
else
{
cout << \"Problem with the input file\" << endl;
system(\"PAUSE\");
system(\"cls\");
}
}


void openAFile(InvStruct aList[], int& aSize)
{
ifstream inf(\"Inventory.txt\");
string iStr;
stringstream iStrStrm;

// make sure the file stream is open before doing IO
if (!inf.fail())
{

system(\"cls\");
cout << \"Read inventory\";
aSize = 0;
while (!inf.eof() && aSize < MAXSIZE)
{

getline(inf, iStr, \';\');
aList[aSize].iName = iStr;

getline(inf, iStr, \';\');
iStrStrm.str(\"\");
iStrStrm.clear();
iStrStrm << iStr;
iStrStrm >> aList[aSize].iQuantity;

getline(inf, iStr);
iStrStrm.str(\"\"); iStrStrm.clear();
iStrStrm << iStr;
iStrStrm >> aList[aSize++].iPrice;
}
cout << endl << aSize << \" data read from the inventory.\" << endl;
system(\"PAUSE\");
system(\"cls\");
}
else
{
cout << \"Problem with the input file\" << endl;
system(\"PAUSE\");
system(\"cls\");
}

}


char menuResponse()
{
char reRun;
cout << endl << \"Your selection\" << endl
<< \"A - Add data, D - Display data, O - Open File, S - Save File, Q - Quit\" << endl
<< \"> \";
cin >> reRun;
cin.ignore(256, \'\ \');
return toupper(reRun);
}

Sample Output:

Inventory has 1 item

Your selection

A- Add data, D- Display data, O - Open File, S - Save File, Q - Quit

>

D

Name Quantity Price

--------------------------------------------------------------------------------------------------

12 12.50

--------------------------------------------------------------------------------------------------

1 data listed 150.00

So I already have most of the code and now I have to: 1. create an index file to keep track of all the inventory IDs and their locations 2. modify my class to b
So I already have most of the code and now I have to: 1. create an index file to keep track of all the inventory IDs and their locations 2. modify my class to b
So I already have most of the code and now I have to: 1. create an index file to keep track of all the inventory IDs and their locations 2. modify my class to b
So I already have most of the code and now I have to: 1. create an index file to keep track of all the inventory IDs and their locations 2. modify my class to b
So I already have most of the code and now I have to: 1. create an index file to keep track of all the inventory IDs and their locations 2. modify my class to b
So I already have most of the code and now I have to: 1. create an index file to keep track of all the inventory IDs and their locations 2. modify my class to b

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site