My c++ program is 99% done but I just need an overloaded operator added to my Ad
ID: 3911930 • Letter: M
Question
My c++ program is 99% done but I just need an overloaded operator added to my Address and Name class to be used in my main.cpp for my array.
I've asked the user to input names and addresses for people and then give him the option to compare them or edit the listings he entered. How would I do that? I have no idea how to use overloaded operators. Ideally, it should compare individually first, middle, and last names while only comparing the addresses as a whole to one another.
Thank you!
my code:
main.cpp
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
#include <stdlib.h>
#include "Header.h"
void Description();
int ToProcess();
void DisplayStrings(Person*, int);
void EditCompare(Person*, int);
int main()
{
Description(); //Lab#, student name, and program description + instructions
Person P;
Person* Entry = &P;
int size = ToProcess(); //gets the # of entries to process
Entry = new Person[size];
string string;
int zip;
cin.ignore(1000, ' ');
cout << "|Enter their data below: _________________________________ ";
for (int i = 0; i < size; i++)
{
cout << "|Person #" << i + 1 << endl;
cout << " |First Name: ";
getline(cin, string);
Entry[i].NameData.setfirst(string);
cout << " |Middle Name: ";
getline(cin, string);
Entry[i].NameData.setmiddle(string);
cout << " |Last Name: ";
getline(cin, string);
Entry[i].NameData.setlast(string);
cout << " |Street: ";
getline(cin, string);
Entry[i].AddressData.setstreet(string);
cout << " |City: ";
getline(cin, string);
Entry[i].AddressData.setcity(string);
cout << " |State: ";
getline(cin, string);
Entry[i].AddressData.setstate(string);
cout << " |Zip: ";
getline(cin, string);
Entry[i].AddressData.setzip(string);
}
DisplayStrings(Entry, size);
EditCompare(Entry, size);
delete Entry;
Entry = 0;
system("pause");
return 0;
}
void Description()
{
cout << "|Lab #4 |Jeremy G----- ";
cout << "______________ ";
cout << "|Description: ";
cout << " The user will enter the number of strings he wants to input ";
cout << " and then the strings themselves to be edited or compared ";
cout << "______________ ";
}
int ToProcess()
{
int number;
cout << "|Enter the number of people you want to enter <min. 2> or 0 to exit: ------>|";
cin >> number;
if (number == 0)
{
cout << "|Exiting program...";
exit(EXIT_FAILURE);
}
while (cin.fail() || number == 1)
{
cin.clear();
cin.ignore(1000, ' ');
cout << "!Error - invalid input Try again: ------>|";
cin >> number;
}
return number;
}
void DisplayStrings(Person* data, int index)
{
cout << "_________________________________ ";
cout << "|Current Entries: ";
for (int count = 0; count < index; count++)
{
cout << " " << count + 1 << ". ";
cout << data[count].NameData.getname() << " " << data[count].AddressData.getaddress() << endl;
}
}
void EditCompare(Person* data, int index)
{
int compare1, compare2, editnum;
string editCompare, editstring;
cout << "|Type /edit to edit an entry, /compare to compare two entries, or /exit to exit: ------>|";
cin >> editCompare;
while (editCompare != "/edit" && editCompare != "/compare" && editCompare != "/exit")
{
cin.clear();
cin.ignore(1000, ' ');
cout << "!ERROR - Please enter a valid input. Try again: ------>|";
cin >> editCompare;
}
if (editCompare == "/compare")
{
cout << "|Enter which two entry numbers you want to compare: ";
cout << " |Entry #";
cin >> compare1;
cout << " |Entry #";
cin >> compare2;
while (compare1 < index || compare2 < index)
{
cin.clear();
cin.ignore(1000, ' ');
cout << "!Error - invalid input Try again: ------>|";
cin >> editnum;
}
//compare();
}
else if (editCompare == "/edit")
{
cout << "|Enter which entry you want to edit: ";
cout << " |Entry #";
cin >> editnum;
while (editnum < index)
{
cin.clear();
cin.ignore(1000, ' ');
cout << "!Error - invalid input Try again: ------>|";
cin >> editnum;
}
cout << "|New data for Entry #" << editnum << ": ";
cout << " |First Name: ";
cin.ignore(1000, ' ');
getline(cin, editstring);
data[editnum - 1].NameData.setfirst(editstring);
cout << " |Middle Name: ";
getline(cin, editstring);
data[editnum - 1].NameData.setmiddle(editstring);
cout << " |Last Name: ";
getline(cin, editstring);
data[editnum - 1].NameData.setlast(editstring);
cout << " |Street: ";
getline(cin, editstring);
data[editnum - 1].AddressData.setstreet(editstring);
cout << " |City: ";
getline(cin, editstring);
data[editnum - 1].AddressData.setcity(editstring);
cout << " |State: ";
getline(cin, editstring);
data[editnum - 1].AddressData.setstate(editstring);
cout << " |Zip: ";
getline(cin, editstring);
data[editnum - 1].AddressData.setzip(editstring);
DisplayStrings(data, index);
}
else
{
cout << "|Exiting program...";
}
}
Header.h
#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
class Name
{
private:
string first,
middle,
last;
public:
void setfirst(string);
void setmiddle(string);
void setlast(string);
string getname();
};
void Name::setfirst(string fname)
{
first = fname;
}
void Name::setmiddle(string mname)
{
middle = mname;
}
void Name::setlast(string lname)
{
last = lname;
}
string Name::getname()
{
return first + " " + middle + " " + last;
}
class Address
{
string street,
city,
state,
zip;
public:
void setstreet(string);
void setcity(string);
void setstate(string);
void setzip(string);
string getaddress();
};
void Address::setstreet(string astreet)
{
street = astreet;
}
void Address::setcity(string acity)
{
city = acity;
}
void Address::setstate(string astate)
{
state = astate;
}
void Address::setzip(string azip)
{
zip = azip;
}
string Address::getaddress()
{
return street + " " + " " + city + ", " + state + ", " + zip;
}
class Person : Name, Address
{
public:
Name NameData;
Address AddressData;
};
Explanation / Answer
i am not giving you exact implementation, but will help you so that you can do it yourself..
In your header file, to whatever class, you need to compare, you need to overload comparison and equals function for those classes. Below, i have overloaded name and address class comarison functions. Below should be your new header file:
now if you want to compare two Names say n1 and n2, You can directly do n1==n2 for equality check, or n1 < n2 or n1 > n2 or n1 <= n2 and so on..
Using these You can write the comparison in whatever way you want. Thanks!!
Let me know if any other help is needed on this. Thanks!
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.