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;
}
}