Consider a program to enter codes of one to eight characters along with an assoc
ID: 3689049 • 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;
}
NOTE: The program standards are given below:
1. Program Header . In addition to you name and project number, include a brief description of the program functionality in the header comment for the program
2. Variables and Constants. All variables and constants in a program must be declared at the beginning of the program
3. Initialization. Variables may not be initialized in their declaration.
4. Printing of if-then. Use one of the following
if (expression) statement
if (expression)
Single-statement
if (expression) {
Multiple-statements
}
5. Printing of if-then-else. Use one of the following
if (expression) statement
else statement
if (expression)
Single-statement
else
Single-statement
if (expression) {
Multiple-statements
} else {
Multiple-statements
}
6. Printing of while loops. Use one of the following
while (expression) statement
while (expression)
Single-statement
while (expression) {
Multiple-statements
}
7. For loops. The bodies of all for-loops must be indented at least 3 (but no more than 5) spaces:
for(int i = 0; i < n; i++) {
cout << "Enter the number:" << flush;
cin >> num;
if (num < 0) num_neg++;
else if (num > 0) num_pos++;
else num_zero++;
}
8. Methods. The bodies of all functions must be indented at least 3 (but no more than 5) spaces:
double CircleArea(double r) {
const double Pi = 3.1415;
return Pi * r * r;
}
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cctype>
using namespace std;
int rec_num=0;
int num_entries;
class Entry {
public:
string name, number, notes;
}InventoryList[200];
string strToUpper (string S) {
for (size_t i=0; i < S.length(); i++)
S[i] = toupper(S[i]);
return S;
}
void readInventory () {
string line;
rec_num=0;
ifstream infile("PhoneData.txt");
while (getline(infile, line))
{
int a, b , c;
if (!(infile >> a >> b >> c)) { break; } // error
else {
InventoryList[rec_num].name = a;
InventoryList[rec_num].number = b;
InventoryList[rec_num].notes = c;
rec_num++;
}
}
}
void storeInventory () {
fstream F("PhoneData.txt");
int rec_num = 0;
while (rec_num < num_entries){
F << InventoryList[rec_num].name << " "
<< InventoryList[rec_num].number << " "
<< InventoryList[rec_num].notes << " " << endl; rec_num++;
}
cout << "Inventory stored." << endl;
return;
}
void enter()
{
char ch,a;
cin>>InventoryList[rec_num].name;
cout<<endl<<"Enter quantity: ";
cin>>InventoryList[rec_num].number;
cout<<endl<<"Enter notes: ";
cin>>InventoryList[rec_num].notes;
rec_num++;
num_entries =rec_num;
}
void findd(string product)
{ int flag=0;
for(int i=0;i<num_entries;i++)
{
if(strToUpper(InventoryList[i].name) == strToUpper(product))
{
cout<<endl<<"--"<<InventoryList[i].name;
cout<<endl<<"--"<<InventoryList[i].number;
cout<<endl<<"--"<<InventoryList[i].notes;
flag=1;
break;
}
}
if(flag == 0)
cout<<endl<<"** No entry with this code "<<product;
return ;
}
void listt()
{
for(int i=0;i<num_entries;i++)
{
cout<<InventoryList[i].name<<endl;
cout<<InventoryList[i].number<<endl;
cout<<InventoryList[i].notes<<endl;
cout<<endl;
}
}
int main()
{
char c;
string f;
cout<<"Press e to enter,f to find,l to list & q to quit";
cout<<" Command:";
cin>>c;
while(c!='q')
{
if(c=='e') enter();
else if(c=='f') {cin>>f; findd(f); }
else if(c=='l') listt();
cout<<" Command:";
cin>>c;
}
storeInventory();
readInventory();
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.