diff options
| author | rPatrickWarner <[email protected]> | 2024-02-26 23:48:37 -0800 |
|---|---|---|
| committer | rPatrickWarner <[email protected]> | 2024-02-26 23:48:37 -0800 |
| commit | cbafc3d7511da08e95f52ad0a4b08e45fa164eac (patch) | |
| tree | 982cb2d6a26ff3a52ea9808af29cb3ff8dc198a1 | |
| parent | all standard functions and delete (diff) | |
| download | homework-7-reecepwarner-cbafc3d7511da08e95f52ad0a4b08e45fa164eac.tar.xz homework-7-reecepwarner-cbafc3d7511da08e95f52ad0a4b08e45fa164eac.zip | |
done because of the due date, I will still improve it though!
| -rw-r--r-- | Homework7/Homework7/Contact.cpp | 53 | ||||
| -rw-r--r-- | Homework7/Homework7/Contact.h | 54 | ||||
| -rw-r--r-- | Homework7/Homework7/ContactList.cpp | 52 | ||||
| -rw-r--r-- | Homework7/Homework7/ContactList.h | 20 | ||||
| -rw-r--r-- | Homework7/Homework7/Menu.cpp | 103 | ||||
| -rw-r--r-- | Homework7/Homework7/MenuHelper.h | 10 | ||||
| -rw-r--r-- | Homework7/Homework7/main.cpp | 20 |
7 files changed, 134 insertions, 178 deletions
diff --git a/Homework7/Homework7/Contact.cpp b/Homework7/Homework7/Contact.cpp index bf097fb..44a4406 100644 --- a/Homework7/Homework7/Contact.cpp +++ b/Homework7/Homework7/Contact.cpp @@ -4,12 +4,12 @@ #include <vector> -Contact::Contact(const Contact& copy)//Copy Constructor +Contact::Contact(const Contact& copy) { *this = copy; } -Contact& Contact::operator=(const Contact& rhs)//Copy Assignment +Contact& Contact::operator=(const Contact& rhs) { if (this != &rhs) { @@ -27,7 +27,7 @@ Contact& Contact::operator=(const Contact& rhs)//Copy Assignment return *this; } -Contact& Contact::operator=(Contact&& rhs) //Move Assignment +Contact& Contact::operator=(Contact&& rhs) { if (this != &rhs) @@ -45,61 +45,56 @@ Contact& Contact::operator=(Contact&& rhs) //Move Assignment return *this; } -Contact::Contact(Contact&& move) //Move Constructor +Contact::Contact(Contact&& move) { *this = move; } -void Contact::SetFirstName(const char* firstname) +void Contact::SetFirstName(char* firstname) { _firstname = firstname; } -const char* Contact::GetFirstName() +char* Contact::GetFirstName()const { return _firstname; } -void Contact::SetLastName(const char* lastname) +void Contact::SetLastName( char* lastname) { - _lastname = lastname; - - } -const char* Contact::GetLastName() +char* Contact::GetLastName()const { return _lastname; } - -void Contact::SetStreetAddress(const char* streetaddy) +void Contact::SetStreetAddress( char* streetaddy) { _streetaddress = streetaddy; } -const char* Contact::GetStreetAddress() +char* Contact::GetStreetAddress()const { return _streetaddress; - } -void Contact::SetCity(const char* city) +void Contact::SetCity( char* city) { _city = city; } -const char* Contact::GetCity() +char* Contact::GetCity()const { return _city; } -void Contact::SetState(const char* state) +void Contact::SetState( char* state) { _state = state; } -const char* Contact::GetState() +char* Contact::GetState()const { return _state; } @@ -113,11 +108,11 @@ int Contact::GetZip() return _zip; } -void Contact::SetEmail(const char* email) +void Contact::SetEmail(char* email) { _email = email; } -const char* Contact::GetEmail() +char* Contact::GetEmail() const { return _email; } @@ -125,33 +120,23 @@ const char* Contact::GetEmail() int Contact::InputInt() { + std::cout << "What's your zip code?: "; int data; std::cin >> data; return data; } -char* Contact::InputNonInt(char* PH) -{ - - /*std::cin.get(PH, 100, '\n'); - std::cin.ignore();*/ - std::cin >> PH; - - return PH; -} - - void Contact::Print() const { - std::cout << "Name: " << _firstname << " " << _lastname<< std::endl; + std::cout << "\nName: " << _firstname << " " << _lastname << std::endl; std::cout << "Email: " << _email << std::endl; std::cout << "Street Address: " << _streetaddress << std::endl; std::cout << "City: " << _city << std::endl; std::cout << "State: " << _state << std::endl; std::cout << "ZipCode: " << _zip << std::endl; std::cout << "////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////"; - + } diff --git a/Homework7/Homework7/Contact.h b/Homework7/Homework7/Contact.h index bf95ebc..2fcef1c 100644 --- a/Homework7/Homework7/Contact.h +++ b/Homework7/Homework7/Contact.h @@ -2,57 +2,55 @@ #define CONTACT_H #include <iostream> +#include <string> class Contact { public: Contact() = default; - - Contact(const Contact& copy); //Copy Constructor - Contact& operator=(const Contact& rhs);//Copy Assignment - Contact(Contact&& move);//Move Constructor - Contact& operator=(Contact&& rhs);//Move Assignment - - ~Contact() = default; - const char* GetLastName(); - void SetLastName(const char* lastname); + Contact(const Contact& copy); //Copy Constructor + Contact& operator=(const Contact& rhs); //Copy Assignment + Contact(Contact&& move); //Move Constructor + Contact& operator=(Contact&& rhs); //Move Assignment + + char* GetLastName() const; + void SetLastName(char* lastname); - void SetFirstName(const char* firstname); - const char* GetFirstName(); + void SetFirstName(char* firstname); + char* GetFirstName() const; - void SetStreetAddress(const char* streetaddy); - const char* GetStreetAddress(); + void SetStreetAddress(char* streetaddy); + char* GetStreetAddress() const; - void SetCity(const char* city); - const char* GetCity(); + void SetCity(char* city); + char* GetCity() const; - void SetState(const char* state); - const char* GetState(); + void SetState(char* state); + char* GetState() const; void SetZip(int zip); int GetZip(); - void SetEmail(const char* email); - const char* GetEmail(); - + void SetEmail(char* email); + char* GetEmail()const; int InputInt(); - - char* InputNonInt(char* PH); + void Print() const; private: - const char* _firstname{ }; - const char* _lastname{ }; - const char* _streetaddress{ }; - const char* _city{ }; - const char* _state{}; - const char* _email{ }; + char* _firstname{ }; + char* _lastname{ }; + char* _streetaddress{ }; + char* _city{ }; + char* _state{}; + char* _email{ }; + int _zip = 0; }; diff --git a/Homework7/Homework7/ContactList.cpp b/Homework7/Homework7/ContactList.cpp index c5314f4..c85ee80 100644 --- a/Homework7/Homework7/ContactList.cpp +++ b/Homework7/Homework7/ContactList.cpp @@ -21,18 +21,18 @@ ContactList& ContactList::operator=(const ContactList& rhs) { if (rhs.contacts_ != nullptr) { - contacts_ = AllocateContactList(sizeof(rhs.contacts_)); + delete[] contacts_; + contacts_ = nullptr; + + + contacts_ = AllocateContactList(rhs.length_); + size_ = rhs.size_; for (auto i = 0u; i < length_; i++) { contacts_[i] = rhs.contacts_[i]; } - size_++; - } - else - { - contacts_ = nullptr; } return *this; @@ -45,14 +45,15 @@ ContactList& ContactList::operator=(ContactList&& rhs) { if (this != &rhs) { - contacts_ = AllocateContactList(sizeof(rhs.contacts_)); - - for (auto i = 0u; i < length_; i++) - { - contacts_[i] = rhs.contacts_[i]; - } + delete[] contacts_; + contacts_ = nullptr; - size_++; + contacts_ = rhs.contacts_; + + length_ = rhs.length_; + size_ = rhs.size_; + + rhs.contacts_ = nullptr; } else { @@ -70,32 +71,27 @@ ContactList::~ContactList() { delete[] contacts_; contacts_ = nullptr; + } void ContactList::DeleteContact(Contact& contact) { - Prompts("Which contact would you like to delete?"); - int i = 0; - std::cin >> i; - Contact empty(contact); - empty.SetFirstName(" "); - empty.SetLastName(" "); - empty.SetEmail(" "); - empty.SetStreetAddress(" "); - empty.SetState(" "); - empty.SetCity(" "); + empty.SetFirstName(nullptr); + empty.SetLastName(nullptr); + empty.SetEmail(nullptr); + empty.SetStreetAddress(nullptr); + empty.SetState(nullptr); + empty.SetCity(nullptr); empty.SetZip(NULL); contact = empty; - - contacts_[i] = empty; } void ContactList::CopyList(const Contact* contacts, const size_t& length) { - length_ = length; //new overall length + length_ = length; //new overall length contacts_ = AllocateContactList(length); for (auto i = 0u; i < length; i++) @@ -107,14 +103,14 @@ void ContactList::CopyList(const Contact* contacts, const size_t& length) void ContactList::AddContact(const Contact& contact) { - contacts_[size_++] = contact; //the post increment will add a new contact and keep track of the number of contacts + contacts_[size_++] = contact; //the post increment will add a new contact and keep track of the number of contacts } void ContactList::PrintList() const { for (auto i = 0u; i < size_; i++) { - std::cout << i << ": "; contacts_[i].Print(); + contacts_[i].Print(); std::cout << std::endl; } } diff --git a/Homework7/Homework7/ContactList.h b/Homework7/Homework7/ContactList.h index e195970..8b2b1ee 100644 --- a/Homework7/Homework7/ContactList.h +++ b/Homework7/Homework7/ContactList.h @@ -8,26 +8,26 @@ class ContactList public: ContactList() = default; ContactList(const size_t& size); - ContactList(const ContactList& copy);//CopyConstructor - ContactList& operator=(const ContactList& rhs);//CopyAssignment - ContactList(ContactList&& move);//MoveConstructor - ContactList& operator=(ContactList&& rhs);//MoveAssignment + ContactList(const ContactList& copy); //CopyConstructor + ContactList& operator=(const ContactList& rhs); //CopyAssignment + ContactList(ContactList&& move); //MoveConstructor + ContactList& operator=(ContactList&& rhs); //MoveAssignment ~ContactList(); void DeleteContact(Contact& contact); - void CopyList(const Contact* contacts, const size_t& length); //constant reference beccause you dont want to change the value + void CopyList(const Contact* contacts, const size_t& length); //constant reference because you dont want to change the value void AddContact(const Contact& contact); - void PrintList() const; //const means that it will never change the data - size_t Size() const; //const function + void PrintList() const; //const means that it will never change the data + size_t Size() const; //const function private: Contact* contacts_{ nullptr }; - size_t length_{ 0 }; //total length of array - size_t size_{ 0 }; //total used elements in array + size_t length_{ 0 }; //total length of array + size_t size_{ 0 }; //total used elements in array - Contact* AllocateContactList(const size_t& size); //This is private because you don't want someone to have access to memory. No access outside of the class + Contact* AllocateContactList(const size_t& size); //This is private because you don't want someone to have access to memory. No access outside of the class }; diff --git a/Homework7/Homework7/Menu.cpp b/Homework7/Homework7/Menu.cpp index dbb2abb..a60cc2d 100644 --- a/Homework7/Homework7/Menu.cpp +++ b/Homework7/Homework7/Menu.cpp @@ -1,97 +1,94 @@ #include "MenuHelper.h" #include "ContactList.h" +using std::numeric_limits; +using std::streamsize; -Contact newContact; -Contact nextContact; +constexpr size_t MAX_STREAM_SIZE = numeric_limits<streamsize>::max(); +using std::cin; -ContactList contacts(3); -void Prompts(const char* prompt) +char* PromptCharInput(const char* prompt, long long maxlen) { + std::cout.flush(); std::cout << prompt << std::endl; + char* input = new char[maxlen]; + + do + { + std::cin.clear(); + cin.ignore(MAX_STREAM_SIZE, '\n'); + cin.get(input, maxlen, '\n'); + } + while (!std::cin); + + return input; } + + void Menu() { char Options = '\0'; + ContactList contacts(3); do { std::cout << "Welcome to the contacts menu\n" << "1)Add New Contact\n" - << "2)Update Contact\n" - << "3)Print All Contacts\n" - << "4)Delete Contact\n" - << "5)Exit\n"; + << "2)Print All Contacts\n" + << "3)Delete Contact\n" + << "4)Exit\n"; std::cin >> Options; switch (Options) { case('1'): - InputContact(); + contacts.AddContact(InputContact()); break; case('2'): + PrintContact(contacts); break; case('3'): - PrintContact(); + //contacts.DeleteContact(); break; case('4'): - contacts.DeleteContact(newContact); - break; - case('5'): - + std::cout << "Thank you, have a great day!" << std::endl; + break; default: std::cout << "Invalid Input, Try Again!" << std::endl; } - } while (Options != '5'); + } while (Options != '4'); } - -ContactList ContactAdder() -{ - ContactList contacts(3); - return contacts; -} - -void ContactAddition() +Contact InputContact() { + Contact newContact; + + newContact.SetFirstName(PromptCharInput("What is your first name?: ", 101)); + newContact.SetLastName(PromptCharInput("What is your last name?: ", 101)); + newContact.SetStreetAddress(PromptCharInput("What is your street address?: ", 101)); + newContact.SetEmail(PromptCharInput("What is your email?: ", 101)); + newContact.SetState(PromptCharInput("What is your state?: ", 101)); + newContact.SetCity(PromptCharInput("What is your city?: ", 101)); + newContact.SetZip(newContact.InputInt()); - -} -void InputContact() -{ - - - newContact.SetFirstName("Reece"); - newContact.SetLastName("Warner"); - newContact.SetEmail("[email protected]"); - newContact.SetStreetAddress("SW Ashland Loop"); - newContact.SetCity("Medford"); - newContact.SetState("Oregon"); - newContact.SetZip(97501); - - nextContact.SetFirstName("Jim"); - nextContact.SetLastName("Belushi"); - nextContact.SetEmail("[email protected]"); - nextContact.SetStreetAddress("Belushi Farms"); - nextContact.SetCity("Central Point"); - nextContact.SetState("Oregon"); - nextContact.SetZip(97504); + return newContact; - - contacts.AddContact(newContact); - contacts.AddContact(nextContact); - } - - - -void PrintContact() +void PrintContact(ContactList& contacts) { - contacts.PrintList(); +} + +ContactList ContactAdder(Contact& contact) +{ + ContactList contacts; + contacts.AddContact(contact); + + return contacts; } + diff --git a/Homework7/Homework7/MenuHelper.h b/Homework7/Homework7/MenuHelper.h index 33b0b49..41c904b 100644 --- a/Homework7/Homework7/MenuHelper.h +++ b/Homework7/Homework7/MenuHelper.h @@ -3,13 +3,13 @@ #include "Contact.h" #include "ContactList.h" -void Prompts(const char* prompt); +char* PromptCharInput(const char* prompt, long long maxlen); void Menu(); -void InputContact(); -void PrintContact(); -void ContactAddition(); -ContactList ContactAdder(); +Contact InputContact(); +void PrintContact(ContactList& contacts); +ContactList ContactAdder(Contact& contact); + #endif diff --git a/Homework7/Homework7/main.cpp b/Homework7/Homework7/main.cpp index 7f25651..dadec00 100644 --- a/Homework7/Homework7/main.cpp +++ b/Homework7/Homework7/main.cpp @@ -13,26 +13,6 @@ int main() Menu(); - //Contact newContact; - //newContact.SetFirstName("Reece"); - //newContact.SetLastName("Warner"); - //newContact.SetEmail("[email protected]"); - //newContact.SetStreetAddress("SW Ashland Loop"); - //newContact.SetCity("Medford"); - //newContact.SetState("Oregon"); - //newContact.SetZip(97501); - // - // - //ContactList contacts(3); - //contacts.AddContact(newContact); - //contacts.PrintList(); - - //ContactList contaxxxx(3); - //ContactList contaxxxx(contacts); //copy constructor - //contaxxxx = contacts; //copy assignment - //contaxxxx.PrintList(); - //contaxxxx = contacts;//move assignment - //contaxxxx.PrintList(); return 0; }
\ No newline at end of file |