Consider a program to enter codes of one to eight characters along with an assoc
ID: 3688708 • Letter: C
Question
Consider a program to enter codes of one to eight characters along with an associated item number and associated notes. A code can represent an item, package, or product’s name.
By using such a program, we could enter product quantities and additional notes (i.e. storage considerations) before associating them with some brief code (i.e. string), which would indicate the product’s ID. We could then retrieve a product’s information by entering the product’s ID. This might be a useful application for a small store’s inventory and stock management.
Codes are entered as 1 to 8 characters.
Use "e" for enter, "f" for find, "l" for list, "q" for quit.
Command: e Soda
Enter quantity: 20 Enter notes:
Command: e Milk
Enter quantity: 10
Enter notes: Chilled storage
Command: e Chips
Enter quantity: 25
Enter notes: Easily crushed
Command: f Milk
-- Milk
-- 10
-- Chilled storage
Command: f Popcorn
** No entry with code Popcorn
Command: . . .
There is also an "l" command, which will list all entries on the screen.
For the input to this program, upper and lower case letters are considered equivalent. For example, if a product name with the code "Soda" is entered, then the codes "SODA", "soda", and "SOda" will all retrieve the same entry with
"Soda" associated.
The entries are to be stored in a file from run to run. When the program begins, the entries in the file are to be read into an array. The array should allow for up to 200 entries. The inventory need not be kept in alphabetical order. You may use a simple sequential search to retrieve entries. When the program is exited, the entries should be stored back in a file for use when the program is run again.
Required Program Characteristics: The assignment is to write a program that incorporates the above features. The entries in the phonebook are to be represented with a simple class:
class Entry { public:
string name, number, notes;
};
Use an array to store the entries. The array should allow for up to 200 entries.
Entry entryList[200];
Use a function to read the entries into the program from a file, and a function to store the entries back into the file when the program is exited.
void readInventory () void storeInventory ()
Also, use a function to list all contacts:
void listAllEntries()
Final Submission:
In the printouts of the sample runs, each of the commands "e", "f", "l" and "q" should be illustrated.
You must also show that the external file is updated with entries added from a previous run.
Note: You may wish to use
string strToUpper (string S) { for (size_t i=0; i < S.length(); i++) S[i] = toupper(S[i]); return S;
}
Writing to a File void storeInventory () { fstream F("PhoneData.txt"); rec_num = 0;
while (rec_num < num_entries){
F << inventoryList[rec_num].name << " "
<< inventoryList[rec_num].quantity << " "
<< inventoryList[rec_num].notes << " " << endl; rec_num++;
}
cout << "Inventory stored." << endl; return;
}
Explanation / Answer
/**C++ program that opens a input file called inventory.txt and
display list of options and then prompt for the corresponding values and prints
the results to console.*/
//inverntory.cpp
#include<iostream>
#include<stdlib.h>
#include<fstream>
#include "Entry.h"
using namespace std;
//constant size
const int SIZE=200;
//function prototypes
void readInventory(ifstream &fin, Entry entries[], int &count);
void storeInventory(Entry entries[], int count);
void listAllEntries(Entry entries[], int count);
void find(Entry entries[], int count,string command);
string Tolowercase(string command);
int main()
{
//create an array of size of type Entry
Entry entry[SIZE];
int count=0;
ifstream fin;
//open input file inventory.txt
fin.open("inventory.txt");
if(!fin)
{
cout<<"File doesnot exist. "<<endl;
system("pause");
exit(0);
}
cout<<"Codes are entered as 1 to 8 characters."<<endl;
cout<<"use "e" for enter, "f" for find, "l" for list, ";
cout<<" "q" for quit. "<<endl;
readInventory(fin, entry,count);
bool repeat=true;
char letter;
int quantity;
string notes;
string command;
while(repeat)
{
cout<<"Command :";
cin>>letter>>command;
switch(letter)
{
case 'e':
//read quantity and notes
cout<<"Enter quantity:";
cin>>quantity;
cout<<"Enter notes:";
cin>>notes;
entry[count]=Entry(Tolowercase(command),quantity,notes);
count++;
break;
case 'f':
//call find method
find(entry,count,command);
break;
case 'l':
//call listAllEntries that will display the all entries in the file
listAllEntries(entry,count);
break;
case 's':
//call storeInventory that takes entry and count
//and write to file
storeInventory(entry,count);
break;
case 'q':
//close the program
exit(0);
}
}
system("pause");
return 0;
}
/**The method find that takes Entry array, count and command
and display all the entries in the array ,Entry*/
void find(Entry entries[], int count,string command)
{
command=Tolowercase(command);
bool found=false;
int index=0;
while(!found && index<SIZE)
{
if(strcmp(entries[index].getName().c_str(),command.c_str())==0)
{
found=true;
cout<<"--"<<entries[index].getName()<<endl;
cout<<"--"<<entries[index].getNumber()<<endl;
cout<<"--"<<entries[index].getNotes()<<endl;
}
index++;
}
}
/**Helper method to convert the command to lower case*/
string Tolowercase(string command)
{
for (std::string::size_type i=0; i<command.length(); ++i)
command[i]=tolower(command[i]);
return command;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.