#include #include #include #include using namespace std; struct Entry { string n
ID: 3691269 • Letter: #
Question
#include
#include
#include
#include
using namespace std;
struct Entry {
string name, quantity, notes;
};
Entry entryList[100];
int rec_num = 0;
int num_entries;
string toUpper (string S) {
for (int i= 0; i
S[i] = toupper(S[i]);
return S;
}
void ReadFile () {
string S, eol;
fstream input("Inventory.txt");
while (!input.eof() && !input.fail()){
input >> entryList[rec_num].name
>> entryList[rec_num].quantity;
getline(input, eol); //reads the end-of-line marker
getline(input, S);
entryList[rec_num].notes = S;
rec_num++;
}
cout << "Inventory read." << endl;
num_entries = rec_num;
input.close();
return;
}
void StoreFile () {
fstream F("Inventory.txt");
rec_num = 0;
while (rec_num < num_entries){
F << entryList[rec_num].name << " "
<< entryList [rec_num].quantity << " "
<< entryList [rec_num].notes << " " << endl;
rec_num++;
}
cout << "Inventory stored." << endl;
return;
}
void add_item(string name, string quantity, string notes){
entryList [num_entries].name = name;
entryList [num_entries].quantity = quantity;
entryList [num_entries].notes = notes;
num_entries++;
return;
}
void retrieve_item(string name){
for (int i = 0; i < num_entries; i++){
if (toUpper(entryList [i].name) == toUpper(name)) {
cout << "Quantity: " << entryList [i].quantity << endl
<< "Notes: " << entryList [i].notes << endl;
return;
}
}
cout << "Item not found" << endl;
return;
}
void listAllItems() {
int i = 0;
while (i < num_entries) {
cout << "-- " << entryList [i].name << " "
<< entryList [i].quantity << endl
<< "-- " << entryList [i].notes << endl << endl;
i++;
}
}
int main(){
string name, quantity, notes;
char command;
ReadFile ();
cout << "Use "e" for enter, "f" for find, "l" for list, "q" to quit."
<< endl << "Command: ";
cin >> command;
while (command != 'q'){
switch (command){
case 'e': cin >> name; cout << "Enter Quantity: ";
cin >> quantity; cout << "Enter Notes: ";
cin.ignore(); getline(cin, notes);
add_item(name, quantity, notes); break;
case 'f': cin >> name; retrieve_item(name); break;
case 'l': listAllItems(); break;
}
cout << " Command: "; cin >> command;
}
StoreFile();
cout << "All set !";
return 0;
}
Explanation / Answer
% Add Code in Bold is for other 2 commands (delete and update)
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
struct Entry {
string name, quantity, notes;
};
Entry entryList[100];
int rec_num = 0;
int num_entries;
string toUpper (string S) {
for (int i= 0; i
S[i] = toupper(S[i]);
return S;
}
void ReadFile () {
string S, eol;
fstream input("Inventory.txt");
while (!input.eof() && !input.fail()){
input >> entryList[rec_num].name >> entryList[rec_num].quantity;
getline(input, eol); //reads the end-of-line marker
getline(input, S);
entryList[rec_num].notes = S;
rec_num++;
}
cout << "Inventory read." << endl;
num_entries = rec_num;
input.close();
return;
}
void StoreFile () {
fstream F("Inventory.txt");
rec_num = 0;
while (rec_num < num_entries){
F << entryList[rec_num].name << " " << entryList [rec_num].quantity << " " << entryList [rec_num].notes << " " << endl;
rec_num++;
}
cout << "Inventory stored." << endl;
return;
}
void add_item(string name, string quantity, string notes){
entryList [num_entries].name = name;
entryList [num_entries].quantity = quantity;
entryList [num_entries].notes = notes;
num_entries++;
return;
}
void retrieve_item(string name){
for (int i = 0; i < num_entries; i++){
if (toUpper(entryList [i].name) == toUpper(name)) {
cout << "Quantity: " << entryList [i].quantity << endl << "Notes: " << entryList [i].notes << endl;
return;
}
}
cout << "Item not found" << endl;
return;
}
void delete_item(string name){
for (int i = 0; i < num_entries; i++){
if (toUpper(entryList [i].name) == toUpper(name)) {
for (int j = i+1; j < num_entries; j++)
entryList[j-1] = entryList[j];
cout << "Item Successfully Deleted" << endl;
num_entries -= 1;
return;
}
}
cout << "Item not found, can not be deleted" << endl;
return;
}
void delete_item(string name,string quantity, string notes){
for (int i = 0; i < num_entries; i++){
if (toUpper(entryList [i].name) == toUpper(name)) {
entryList[i].quantity = quantity;
entryList[i].notes = notes;
cout << "Item Successfully Updated" << endl;
return;
}
}
cout << "Item not found, can not be updated" << endl;
return;
}
void listAllItems() {
int i = 0;
while (i < num_entries) {
cout << "-- " << entryList [i].name << " " << entryList [i].quantity << endl << "-- " << entryList [i].notes << endl << endl;
i++;
}
}
int main(){
string name, quantity, notes;
char command;
ReadFile ();
cout << "Use "e" for enter, "f" for find, "l" for list, "u" for update, "d" for update, "q" to quit." << endl << "Command: ";
cin >> command;
while (command != 'q'){
switch (command){
case 'e':
cin >> name;
cout << "Enter Quantity: ";
cin >> quantity; cout << "Enter Notes: ";
cin.ignore(); getline(cin, notes);
add_item(name, quantity, notes);
break;
case 'f':
cin >> name;
retrieve_item(name);
break;
case 'l':
listAllItems();
break;
case 'd':
cin >> name;
delete_item(name);
break;
case 'u':
cin >> name;
cout << "Enter Quantity: ";
cin >> quantity; cout << "Enter Notes: ";
cin.ignore(); getline(cin, notes);
update_item(name,quantity,notes);
break;
}
cout << " Command: ";
cin >> command;
}
StoreFile();
cout << "All set !";
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.