Thread: Modified Address Book Using Prebuilt Classes

  1. #1
    Registered User
    Join Date
    Jan 2024
    Posts
    1

    Modified Address Book Using Prebuilt Classes

    Suffering a setback on a project and don't know what to do. I am supposed to create a new program by modifying an address book program I created for a previous assignment. I must modify the program to use the Array Class instead of a basic array. Then, create another new program by modifying the address book program created in the previous module to use the Vector Class instead of a basic array. I also have to implement exception catching in appropriate areas that may cause errors. I should implement exception catching in at least one area of the program for each of the Array class and Vector class programs I create.

    My professor didn't cover any of this in our lesson this week and I am struggling to figure it out.

    Here is my previous code that needs to be modified. There are three files (Record.h, Record.cpp, and main.cpp).

    Program:
    Code:
    /***** Record.h *****/
    #ifndef RECORD_H
    #define RECORD_H
    #include <iostream>
    using namespace std;
    //Record class
    class Record {
    private:
      //private data member
      int recordNumber;
      string firstName;
      string lastName;
      int age;
      int telephone;
    public:
      //default constructor
       Record();
      //parameterized constructor
       Record(int, string, string, int, int);
      //getter functions
      int getRecordNumber();
      string getFirstName();
      string getLastName();
      int getAge();
      int getTelephone();
      //setter functions
      void setRecordNumber(int);
      void setFirstName(string);
      void setLastName(string);
      void setAge(int);
      void setTelephone(int);
      void print();
    };
    
    #endif                          // RECORD_H
    
    /***** Record.cpp *****/
    #include <iostream>
    #include <iomanip>
    #include"Record.h"
    using namespace std;
    //default constructor
    Record::Record()
    {
      recordNumber = 0;
      firstName = "";
      lastName = "";
      age = 0;
      telephone = 0;
    }
    
    //parameterized constructor
    Record::Record(int rno, string fn, string ln, int ag, int tno)
    {
      recordNumber = rno;
      firstName = fn;
      lastName = ln;
      age = ag;
      telephone = tno;
    }
    
    //getter functions
    int Record::getRecordNumber()   //returns the recordNumber
    {
      return recordNumber;
    }
    
    string Record::getFirstName()   //returns the firstname
    {
      return firstName;
    }
    
    string Record::getLastName()    //returns the lastname
    {
      return lastName;
    }
    
    int Record::getAge()            //returns the age
    {
      return age;
    }
    
    int Record::getTelephone()      //returns the telephone
    {
      return telephone;
    }
    
    //setter functions
    void Record::setRecordNumber(int rno) //set the recordNumber
    {
      recordNumber = rno;
    }
    
    void Record::setFirstName(string fn)  //set the firstname
    {
      firstName = fn;
    }
    
    void Record::setLastName(string ln) //set the lastname
    {
      lastName = ln;
    }
    
    void Record::setAge(int ag)     //set the age
    {
      age = ag;
    }
    
    void Record::setTelephone(int tno)  //  //set the telephone
    {
      telephone = tno;
    }
    
    void Record::print()            //print a record
    {
      cout << left << setw(14) << recordNumber;
      cout << left << setw(10) << firstName;
      cout << left << setw(10) << lastName;
      cout << left << setw(5) << age;
      cout << telephone << endl;
    }
    
    /***** main.cpp *****/
    #include <iostream>
    #include"Record.h"
    using namespace std;
    //main function
    int main()
    {
      //array of Record
      Record rec[10];
      int op, n = 0, age, telephone;
      string firstName, lastName;
      while (1) {
        // prints the menu
        cout << "1. Input information into an record." << endl;
        cout << "2. Display all information in all records." << endl;
        cout << "3. Exit the program" << endl;
        cout << "Enter option: ";
        cin >> op;
        switch (op) {
        case 1:                    //Input information into an record
          cout << "Enter first name: ";
          cin >> firstName;
          cout << "Enter last name: ";
          cin >> lastName;
          cout << "Enter age: ";
          cin >> age;
          cout << "Enter telephone number: ";
          cin >> telephone;
          rec[n] = Record(n + 1, firstName, lastName, age, telephone);
          n++;
          break;
        case 2:                    //Display all information in all records
          cout << "Record Number FirstName LastName Age Telephone-Number" << endl;
          cout << "-----------------------------------------------------" << endl;
          for (int i = 0; i < n; i++) {
            rec[i].print();
          }
          break;
        case 3:                    //Exit the program
          cout << "Exit the program." << endl;
          return 0;
        }
        cout << endl;
      }
    }
    Last edited by Salem; 01-11-2024 at 01:24 PM. Reason: Removed crayola

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > Record rec[10];
    Isn't it just
    std::array<Record, 10> a;

    std::array - cppreference.com
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Address Book
    By Javier Medina in forum C++ Programming
    Replies: 1
    Last Post: 09-03-2014, 01:32 PM
  2. address book
    By cboard in forum C Programming
    Replies: 10
    Last Post: 04-05-2007, 11:47 PM
  3. Address Book
    By datainjector in forum C Programming
    Replies: 6
    Last Post: 12-10-2002, 05:18 PM
  4. Address Book
    By Granger9 in forum C Programming
    Replies: 4
    Last Post: 09-09-2002, 01:02 PM
  5. Address book
    By sundeeptuteja in forum C++ Programming
    Replies: 7
    Last Post: 08-18-2002, 02:40 PM

Tags for this Thread