c++ help adjusting my code? RolodexEntryManager.cpp ----------------------------
ID: 3887445 • Letter: C
Question
c++ help adjusting my code?
RolodexEntryManager.cpp
-------------------------------------
#include <iostream>
#include "RolodexEntry.h"
#include <vector>
#include <climits>
#include <cctype>
using namespace std;
//function dec
void printMainMenu();
void printEditMenu(vector<RolodexEntry> & list);
void editContactSub(const short c, vector<RolodexEntry> & list);
void printSearchMenu(vector<RolodexEntry> & list);
void printList(vector<RolodexEntry> & list);
void eraseEntry(vector<RolodexEntry> & list, short index);
int main(void)
{
bool isRunning = true;
vector<RolodexEntry> list;
do
{
char c;
printMainMenu();
cin >> c;
cin.ignore(INT_MAX, ' ');
switch(tolower(c))
{
case '1': case 'a':
{
RolodexEntry newEntry;
newEntry.readIn();
list.push_back(newEntry);
break;
}
case '2': case 'e':
{
printEditMenu(list);
break;
}
case '3': case 'd':
{
printList(list);
cout << " Select Contact to delete:";
if(cin.fail())
{
cin.clear();
cin.ignore();
}
short c;
cin >> c;
eraseEntry(list, c-1);//resizes and moves back
break;
}
case '4': case 's':
{
printSearchMenu(list);
break;
}
case '5': case 'p':
{
printList(list);
break;
}
case '6': case 'q':
{
isRunning = false;
break;
}
default:
{
cout << "Invalid selection.";
break;
}
}
}while(isRunning);
return 0;
}
void printMainMenu()
{
cout << " Main Menu "
<< "1. Add contact "
<< "2. Edit contact "
<< "3. Delete contact "
<< "4. Search contacts "
<< "5. Print all contacts "
<< "6. Quit ";
return;
}
void printEditMenu(vector<RolodexEntry> & list)
{
cout << " ";
printList(list);
cout << " Select Contact to edit:";//read in choice
if(cin.fail())
{
cin.clear();
cin.ignore();
}
short c;
cin >> c;
cin.ignore(INT_MAX, ' ');
editContactSub(c, list);
return;
}
void editContactSub(const short c, vector<RolodexEntry> & list)//and work
{
short index = c;
if(index >= 0 && index < list.size())//it's gooooood!
{
RolodexEntry edited = list[static_cast<short>(c)-1];
//edit menu
cout << "1. edit First name "
<< "2. edit Last name "
<< "3. edit Address "
<< "4. edit Phone number "
<< "5. edit Email ";
char sel;
cin >> sel;
cin.ignore(INT_MAX, ' ');
switch(tolower(sel))
{
case '1': case 'f':
{
cout << " Enter new first name: ";
string newName;
cin >> newName;
edited.setFName(newName);
break;
}
case '2': case 'l':
{
cout << " Enter new last name: ";
string newName;
cin >> newName;
edited.setLName(newName);
break;
}
case '3': case 'a':
{
cout << " Enter new street number and street: ";
string newStreet;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, newStreet);
edited.setStreet(newStreet);
cout << " Enter new town: ";
string newtown;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, newtown);
edited.setTown(newtown);
cout << " Enter new state: ";
string newstate;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, newstate);
edited.setState(newstate);
cout << " Enter new zipcode: ";
long newzip;
cin >> newzip;
if(newzip > 99999)//long zip
{
edited.setZip(newzip);
edited.setSZipLong();//finds szip from long zip
}
else
{
edited.setSZip(newzip);
}
break;
}
case '4': case 'p':
{
cout << " Enter new areacode: ";
short newarea;
cin >> newarea;
edited.setArea(newarea);
cout << " Enter new exchange number: ";
short newex;
cin >> newex;
edited.setExchange(newex);
cout << " Enter new line: ";
short newLine;
cin >> newLine;
edited.setPLine(newLine);
break;
}
case '5': case 'e':
{
cout << " Enter new email: ";
string newemail;
cin >> newemail;
edited.setEmail(newemail);
break;
}
}
}
else
{
cout << "Contact at this index does not exist.";
}
return;
}
void printSearchMenu(vector<RolodexEntry> & list)//and work
{
cout << "1. search by Name "
<< "2. search by Address "
<< "3. search by Phone number "
<< "4. search by Email "
<< "5. Return to Main menu ";
char c;
cin >> c;
cin.ignore(INT_MAX, ' ');
switch(tolower(c))
{
case '1': case 'n':
{
cout << " Enter search term: ";
string search;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, search);
for(vector<RolodexEntry>::size_type i = 0; i < list.size(); i++)
{
if(list[i].getFName().find(search) != string::npos ||
list[i].getLName().find(search) != string::npos)
{
list[i].printEntry();
}
}
break;
}
case '2': case 'a':
{
cout << " Enter search term: ";
string search;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, search);
for(vector<RolodexEntry>::size_type i = 0; i < list.size(); i++)
{
if(list[i].getStreet().find(search) != string::npos ||
list[i].getTown().find(search) != string::npos ||
list[i].getState().find(search) != string::npos)
{
list[i].printEntry();
}
}
break;
}
case '3': case 'p':
{
cout << " Enter part of phone number (last four digits gives best results): ";
short search;
while(cin.fail())
{
cin.clear();
cin.ignore();
}
cin >> search;
for(vector<RolodexEntry>::size_type i = 0; i < list.size(); i++)
{
if(list[i].getArea() == search || list[i].getExchange() == search ||
list[i].getPLine() == search)
{
list[i].printEntry();
}
}
break;
}
case '4': case 'e':
{
cout << " Enter email: ";
string search;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, search);
for(vector<RolodexEntry>::size_type i = 0; i < list.size(); i++)
{
if(list[i].getEmail().find(search) != string::npos)
{
list[i].printEntry();
}
}
break;
}
case '5': case 'q':
{
break;
}
default:
{
cout << " Invalid seletion.";
break;
}
}
}
void printList(vector<RolodexEntry> & list)
{
cout << ' ';
for(vector<RolodexEntry>::size_type i = 0; i < list.size(); i++)
{
cout << i+1 << ".";
list[i].printEntry();
}
return;
}
void eraseEntry(vector<RolodexEntry> & list, short index) {
vector<RolodexEntry>::size_type pos = index - 1;
vector<RolodexEntry>::size_type k;
if (pos < list.size() && pos >= 0) {
for (k = pos + 1; k != list.size(); k++) {
list[k - 1] = list[k];
}
list.pop_back();
} else {
cout << "Out of bounds.";
}
return;
}
-----------------------------------------------------------------
RolodexEntry.cpp
------------------------------
#include "RolodexEntry.h"
#include <iostream>
using namespace std;
void RolodexEntry::printEntry()
{
cout << " Name: " << fName << " " << lName;
cout << " Address: " << street <<
" " << town << ", " <<
state << ' ';
if(zip != 0)
{
cout << zip;
}
else
{
cout << szip;
}
cout << " Phone: (" << area << ')' << exchange << '-' << line << " ";
}
void RolodexEntry::readIn()
{
cout << " Enter new contact's first name: ";
cin >> fName;
cout << " Enter new contacts's last name: ";
cin >> lName;
cout << " Enter new contacts's address (number and street): ";
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, street);
cout << " Enter new contacts's town: ";
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, town);
cout << " Enter new contacts's state: ";
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, state);
cout << " Enter new contacts's zipcode: ";
long tempZip;
cin >> tempZip;
if(tempZip > 99999)//long zip
{
zip = tempZip;
setSZipLong();//finds szip from long zip
}
else
{
szip = tempZip;
}
cout << " Enter new contacts's phone number (separated by spaces): ";
cin >> area >> exchange >> line;
cout << " Enter new contacts's email: ";
cin >> email;
}
----------------------------------------------------------------------
RolodexEntry.h
-----------------------------------------
#ifndef ROLODEXENTRY_H_INCLUDED
#define ROLODEXENTRY_H_INCLUDED
#include <string>
//using namespace std;
//base project only
class RolodexEntry
{
private:
std::string fName, lName, street, town, state;
long zip;
short szip, area, exchange, line;
std::string email;
public:
RolodexEntry(void):fName(), lName(), street(), town(),
state(), zip(000000000), szip(00000),
area(0), exchange(0), line(0), email() {}
RolodexEntry(std::string fname, std::string lname):street(), town(),
state(""), zip(000000000),
szip(00000), area(0),
exchange(0), line(0), email() {}
RolodexEntry(const RolodexEntry & r):fName(r.fName), lName(r.lName),
street(r.street), town(r.town),
state(r.state), zip(r.zip), szip(r.szip),
area(r.area), exchange(r.exchange),
line(r.line), email(r.email) {}
std::string getFName() const {return fName;}
void setFName(std::string first){fName = first;}
std::string getLName() const {return lName;}
void setLName(std::string last){lName = last;}
std::string getStreet() const{return street;}
void setStreet(std::string str){street = str;}
std::string getTown() const {return town;}
void setTown(std::string newtown){town = newtown;}
std::string getState() const {return state;}
void setState(std::string newstate){state = newstate;}
long getZip() const {return zip;}
void setZip(long newzip){zip = newzip;}
short getSZip() const {return szip;}
void setSZipLong(){szip = zip / 10000;}
void setSZip(short newszip){szip = newszip;}
short getArea() const {return area;}
void setArea(short newarea){area = newarea;}
short getExchange() const {return exchange;}
void setExchange(short exch){exchange = exch;}
short getPLine() const {return line;}
void setPLine(short newline){line = newline;}
std::string getEmail(){return email;}
void setEmail(std::string Email){email = Email;}
void printEntry();//other fns
void readIn();
bool isEqual(RolodexEntry e){return e.fName == fName && e.lName == lName &&
e.street == street && e.town == town &&
e.state == state && e.zip == zip && e.szip == szip &&
e.area == area && e.exchange == exchange && e.line == line &&
e.email == email;}
};
#endif // ROLODEXENTRY_H_INCLUDED
Explanation / Answer
Given below are the Rolodex.h and Rolodex.cpp files . Also modified the main program to use the new class. Hope it helps. If it did,please don't forget to rate the answer. Thank you very much.
Rolodex.h
#ifndef ROLODEX_H_INCLUDED
#define ROLODEX_H_INCLUDED
#include <iostream>
#include "RolodexEntry.h"
using std::size_t;
using std::string;
const size_t MAX_ENTRIES = 100;
class Rolodex
{
RolodexEntry list[MAX_ENTRIES];
size_t current;
public:
bool full(void) const {return current == MAX_ENTRIES;}
RolodexEntry get(size_t index) const;
bool set(size_t index, const RolodexEntry& new_entry);
bool add(const RolodexEntry& new_entry);
void eraseEntry(short index) ;
void printList();
size_t size();
size_t findByName(string search);
size_t findByAddress(string search);
size_t findByPhone(short search);
size_t findByEmail(string search);
};
#endif // ROLODEX_H_INCLUDED
Rolodex.cpp
#include "Rolodex.h"
#include <iostream>
using namespace std;
size_t Rolodex::size()
{
return current;
}
RolodexEntry Rolodex::get(size_t index) const
{
return list[index];
}
bool Rolodex::set(size_t index, const RolodexEntry& new_entry)
{
if(index >= 0 && index < size())
{
list[index] = new_entry;
return true;
}
else
return false;
}
bool Rolodex::add(const RolodexEntry& new_entry)
{
if(!full())
{
list[current++] = new_entry;
return true;
}
else
return false;
}
void Rolodex::eraseEntry(short index) {
size_t k;
if (index < size() && index >= 0) {
for (k = index + 1; k != size(); k++) {
list[k - 1] = list[k];
}
current--;
} else {
cout << "Out of bounds.";
}
return;
}
void Rolodex::printList()
{
cout << ' ';
for(size_t i = 0; i < size(); i++)
{
cout << i+1 << ".";
list[i].printEntry();
}
return;
}
size_t Rolodex::findByName(string search)
{
for(size_t i = 0; i < size(); i++)
{
if(list[i].getFName().find(search) != string::npos ||
list[i].getLName().find(search) != string::npos)
{
return i;
}
}
return -1;
}
size_t Rolodex::findByAddress(string search)
{
for(size_t i = 0; i < size(); i++)
{
if(list[i].getStreet().find(search) != string::npos ||
list[i].getTown().find(search) != string::npos ||
list[i].getState().find(search) != string::npos)
{
return i;
}
}
return -1;
}
size_t Rolodex::findByPhone(short search)
{
for(size_t i = 0; i < size(); i++)
{
if(list[i].getArea() == search || list[i].getExchange() == search ||
list[i].getPLine() == search)
{
return i;
}
}
return -1;
}
size_t Rolodex::findByEmail(string search)
{
for(size_t i = 0; i < size(); i++)
{
if(list[i].getEmail().find(search) != string::npos)
{
return i;
}
}
return -1;
}
RolodexEntryManager.cpp
#include <iostream>
#include "RolodexEntry.h"
#include "Rolodex.h"
#include <climits>
#include <cctype>
using namespace std;
//function dec
void printMainMenu();
void printEditMenu(Rolodex & list);
void editContactSub(const short c, Rolodex & list);
void printSearchMenu(Rolodex & list);
int main(void)
{
bool isRunning = true;
Rolodex list;
do
{
char c;
printMainMenu();
cin >> c;
cin.ignore(INT_MAX, ' ');
switch(tolower(c))
{
case '1': case 'a':
{
RolodexEntry newEntry;
newEntry.readIn();
list.add(newEntry);
break;
}
case '2': case 'e':
{
printEditMenu(list);
break;
}
case '3': case 'd':
{
list.printList();
cout << " Select Contact to delete:";
if(cin.fail())
{
cin.clear();
cin.ignore();
}
short c;
cin >> c;
list.eraseEntry(c-1);//resizes
break;
}
case '4': case 's':
{
printSearchMenu(list);
break;
}
case '5': case 'p':
{
list.printList();
break;
}
case '6': case 'q':
{
isRunning = false;
break;
}
default:
{
cout << "Invalid selection.";
break;
}
}
}while(isRunning);
return 0;
}
void printMainMenu()
{
cout << " Main Menu "
<< "1. Add contact "
<< "2. Edit contact "
<< "3. Delete contact "
<< "4. Search contacts "
<< "5. Print all contacts "
<< "6. Quit ";
return;
}
void printEditMenu(Rolodex & list)
{
cout << " ";
list.printList();
cout << " Select Contact to edit:";//read in choice
if(cin.fail())
{
cin.clear();
cin.ignore();
}
short c;
cin >> c;
cin.ignore(INT_MAX, ' ');
editContactSub(c, list);
return;
}
void editContactSub(const short c, Rolodex& list)//and work
{
short index = c - 1;
if(index >= 0 && index < list.size())//it's gooooood!
{
RolodexEntry edited = list.get(static_cast<short>(c)-1);
//edit menu
cout << "1. edit First name "
<< "2. edit Last name "
<< "3. edit Address "
<< "4. edit Phone number "
<< "5. edit Email ";
char sel;
cin >> sel;
cin.ignore(INT_MAX, ' ');
switch(tolower(sel))
{
case '1': case 'f':
{
cout << " Enter new first name: ";
string newName;
cin >> newName;
edited.setFName(newName);
list.set(c - 1, edited);
break;
}
case '2': case 'l':
{
cout << " Enter new last name: ";
string newName;
cin >> newName;
edited.setLName(newName);
list.set(c - 1, edited);
break;
}
case '3': case 'a':
{
cout << " Enter new street number and street: ";
string newStreet;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, newStreet);
edited.setStreet(newStreet);
cout << " Enter new town: ";
string newtown;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, newtown);
edited.setTown(newtown);
cout << " Enter new state: ";
string newstate;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, newstate);
edited.setState(newstate);
cout << " Enter new zipcode: ";
long newzip;
cin >> newzip;
if(newzip > 99999)//long zip
{
edited.setZip(newzip);
edited.setSZipLong();//finds szip from long zip
}
else
{
edited.setSZip(newzip);
}
list.set(c - 1, edited);
break;
}
case '4': case 'p':
{
cout << " Enter new areacode: ";
short newarea;
cin >> newarea;
edited.setArea(newarea);
cout << " Enter new exchange number: ";
short newex;
cin >> newex;
edited.setExchange(newex);
cout << " Enter new line: ";
short newLine;
cin >> newLine;
edited.setPLine(newLine);
list.set(c - 1, edited);
break;
}
case '5': case 'e':
{
cout << " Enter new email: ";
string newemail;
cin >> newemail;
edited.setEmail(newemail);
list.set(c - 1, edited);
break;
}
}
}
else
{
cout << "Contact at this index does not exist.";
}
return;
}
void printSearchMenu(Rolodex & list)//and work
{
cout << "1. search by Name "
<< "2. search by Address "
<< "3. search by Phone number "
<< "4. search by Email "
<< "5. Return to Main menu ";
char c;
cin >> c;
cin.ignore(INT_MAX, ' ');
int index;
RolodexEntry entry;
switch(tolower(c))
{
case '1': case 'n':
{
cout << " Enter search term: ";
string search;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, search);
index = list.findByName(search);
if(index != -1)
{
entry = list.get(index);
entry.printEntry();
}
else
cout << "No such name" << endl;
break;
}
case '2': case 'a':
{
cout << " Enter search term: ";
string search;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, search);
index = list.findByAddress(search);
if(index != -1)
{
entry = list.get(index);
entry.printEntry();
}
else
cout << "No such address." << endl;
break;
}
case '3': case 'p':
{
cout << " Enter part of phone number (last four digits gives best results): ";
short search;
while(cin.fail())
{
cin.clear();
cin.ignore();
}
cin >> search;
index = list.findByPhone(search);
if(index != -1)
{
entry = list.get(index);
entry.printEntry();
}
else
cout << "No such phone." << endl;
break;
}
case '4': case 'e':
{
cout << " Enter email: ";
string search;
cout.flush();
if (cin.peek() == ' ')
{
cin.ignore();
}
getline(cin, search);
index = list.findByEmail(search);
if(index != -1)
{
entry = list.get(index);
entry.printEntry();
}
else
cout << "No such email." << endl;
break;
}
case '5': case 'q':
{
break;
}
default:
{
cout << " Invalid seletion.";
break;
}
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.