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

#include <iostream> #include <iomanip> #include <fstream> #include <string> usin

ID: 3690728 • Letter: #

Question

#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.length(); 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;

}

Introduction to Programming C++ Project Consider a program to enter codes of one to eight characters along with an as- sociated item number and associated notes. A code can represent an item package, or products 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 products ID. We could then retrieve a prod uct's information by entering the product's ID. This might be a useful applica tion for a small store's inventory and stock management. Codes are entered as 1 to 8 characters Use "e" for enter, "f" for find, "1" for list, "q"for quit. Command: e Soda Enter quantitv: 20 Enter notes: Command e Milk Enter quantity: 10 Enter notes: Chilled storage Command e Chips Enter quantitv: 25 Enter notes: Easily crushed Command: f Milk -- Milk 10

Explanation / Answer

Ans;

Complete Program:

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

const int MAX_LENGTH = 100;

struct Contact
{
   string name, number, notes;
};

class PhoneBook
{
public:
   PhoneBook();
   string toUpper(string S);
   void ReadFile();
   void StoreFile();
   void add_name(string name, string number, string notes);
   void retrieve_name(string name);
   void listAllContacts();
   void delete_name(string name);
   void update_name(string name);

private:  
   Contact contactList[MAX_LENGTH];
   int num_entries;  
};

PhoneBook::PhoneBook()
{
   num_entries = 0;
}

string PhoneBook::toUpper(string S)
{
   for(int i= 0; i < S.length(); i++)
       S[i] = toupper(S[i]);
   return S;
}

void PhoneBook::ReadFile()
{
   string str1, str2, eol;
   fstream input("PhoneData.txt");
  
   input >> str1;
   while(!input.eof() && num_entries < MAX_LENGTH)
   {      
       input >> str2;
       input.ignore();
       getline(input, eol);

       PhoneBook::add_name(str1, str2, eol);

       input >> str1;
   }

   cout << "Book read." << endl;

   input.close();
   return;
}

void PhoneBook::StoreFile()
{
   fstream F("PhoneData.txt");
   int rec_num = 0;
   while (rec_num < num_entries)
   {
       F << contactList[rec_num].name << " "
           << contactList[rec_num].number << " "
           << contactList[rec_num].notes << endl;
       rec_num++;
   }
   cout << "Phonebook stored." << endl;
   F.close();
   return;
}

void PhoneBook::add_name(string name, string number, string notes)
{
   if(num_entries < MAX_LENGTH)
   {
       int i = num_entries;

       while(i > 0 && toUpper(contactList[i - 1].name) > toUpper(name))
       {
           contactList[i].name = contactList[i - 1].name;
           contactList[i].number = contactList[i - 1].number;
           contactList[i].notes = contactList[i - 1].notes;

           i--;
       }

       contactList[i].name = name;
       contactList[i].number = number;
       contactList[i].notes = notes;
       num_entries++;
   }
   return;
}

void PhoneBook::retrieve_name(string name)
{
   for(int i = 0; i < num_entries; i++)
   {
       if (toUpper(contactList[i].name) == toUpper(name))
       {
           cout << "Phone Number: " << contactList[i].number << endl
               << "Notes: " << contactList[i].notes << endl;
           return;
       }
   }
   cout << "Name not found" << endl;
   return;
}

void PhoneBook::listAllContacts()
{
   int i = 0;
   while (i < num_entries)
   {
       cout << "-- " << contactList[i].name << " "
           << contactList[i].number << endl
           << "-- " << contactList[i].notes << endl << endl;
       i++;
   }
}

void PhoneBook::delete_name(string name)
{
   int index = -1;
   for(int i = 0; i < num_entries; i++)
   {
       if (toUpper(contactList[i].name) == toUpper(name))
       {
           index = i;
           break;
       }
   }
  
   if(index == -1)
       cout << "Name is not found" << endl;
   else
   {
       for(int i = index; i < num_entries + 1 && i < MAX_LENGTH - 1; i++)
           contactList[i] = contactList[i + 1];
   }
}

void PhoneBook::update_name(string name)
{
   int index = -1;
   for(int i = 0; i < num_entries; i++)
   {
       if (toUpper(contactList[i].name) == toUpper(name))
       {
           index = i;
           break;
       }
   }
  
   if(index == -1)
       cout << "Name is not found" << endl;
   else
   {
       string str2, eol;

       cout << "Enter Number: ";
       cin >> str2;

       cout << "Enter Notes: ";
       cin.ignore();
       getline(cin, eol);

       contactList[index].name = name;
       contactList[index].number = str2;
       contactList[index].notes = eol;
   }
}

int main()
{
   PhoneBook book;

   string name, number, notes;
   char command;

   book.ReadFile();

   cout << "Use "e" for enter, "f" for find, "l" for list, "d" for delete, "u" for update, "q" to quit."
       << endl << "Command: ";
   cin >> command;

   while(command != 'q')
   {
       switch (command)
       {
       case 'e':
           cin >> name;
           cout << "Enter Number: ";
           cin >> number;
           cout << "Enter Notes: ";
           cin.ignore();
           getline(cin, notes);
           book.add_name(name, number, notes);
           break;

       case 'f':
           cin >> name;
           book.retrieve_name(name);
           break;

       case 'l':
           book.listAllContacts();
           break;

       case 'd':
           cin >> name;
           book.delete_name(name);
           break;

       case 'u':
           cin >> name;
           book.update_name(name);
           break;
       }

       cout << " Command: ";
       cin >> command;
   }

   book.StoreFile();

   cout << "All set !";

   cout << endl;
   system("pause");
   return 0;
}