diff options
| author | rPatrickWarner <[email protected]> | 2024-02-28 21:02:17 -0800 |
|---|---|---|
| committer | rPatrickWarner <[email protected]> | 2024-02-28 21:02:17 -0800 |
| commit | aab4b4f3173df0d43360340835a15aac82e0ad0f (patch) | |
| tree | f3b38259f61f72a180d7de82a36d745bc7b7c521 | |
| parent | done because of the due date, I will still improve it though! (diff) | |
| download | homework-7-reecepwarner-main.tar.xz homework-7-reecepwarner-main.zip | |
| -rw-r--r-- | Homework7/Homework7/Contact.cpp | 47 | ||||
| -rw-r--r-- | Homework7/Homework7/Contact.h | 20 | ||||
| -rw-r--r-- | Homework7/Homework7/ContactList.cpp | 72 | ||||
| -rw-r--r-- | Homework7/Homework7/ContactList.h | 9 | ||||
| -rw-r--r-- | Homework7/Homework7/Menu.cpp | 43 | ||||
| -rw-r--r-- | Homework7/Homework7/MenuHelper.h | 3 | ||||
| -rw-r--r-- | Homework7/Homework7/main.cpp | 2 |
7 files changed, 117 insertions, 79 deletions
diff --git a/Homework7/Homework7/Contact.cpp b/Homework7/Homework7/Contact.cpp index 44a4406..3741954 100644 --- a/Homework7/Homework7/Contact.cpp +++ b/Homework7/Homework7/Contact.cpp @@ -3,8 +3,7 @@ #include <string> #include <vector> - -Contact::Contact(const Contact& copy) +Contact::Contact(const Contact& copy) { *this = copy; } @@ -22,7 +21,6 @@ Contact& Contact::operator=(const Contact& rhs) _email = rhs._email; - } return *this; } @@ -61,7 +59,6 @@ char* Contact::GetFirstName()const return _firstname; } - void Contact::SetLastName( char* lastname) { _lastname = lastname; @@ -117,19 +114,50 @@ char* Contact::GetEmail() const return _email; } - -int Contact::InputInt() +int Contact::InputInt(const char* prompt) { - std::cout << "What's your zip code?: "; - int data; + std::cout << prompt << std::endl; + + std::cout.flush(); + + int data = 0; std::cin >> data; + + while (!std::cin) + { + std::cout << prompt << std::endl; + std::cin.clear(); + + cin.ignore(MAX_STREAM_SIZE, '\n'); + std::cin >> data; + + } + return data; } +char* Contact::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 Contact::Print() const { - + std::cout << "\nName: " << _firstname << " " << _lastname << std::endl; std::cout << "Email: " << _email << std::endl; std::cout << "Street Address: " << _streetaddress << std::endl; @@ -140,3 +168,4 @@ void Contact::Print() const } + diff --git a/Homework7/Homework7/Contact.h b/Homework7/Homework7/Contact.h index 2fcef1c..4ee3067 100644 --- a/Homework7/Homework7/Contact.h +++ b/Homework7/Homework7/Contact.h @@ -3,7 +3,10 @@ #define CONTACT_H #include <iostream> #include <string> - +using std::numeric_limits; +using std::streamsize; +constexpr size_t MAX_STREAM_SIZE = numeric_limits<streamsize>::max(); +using std::cin; class Contact { @@ -37,12 +40,11 @@ public: void SetEmail(char* email); char* GetEmail()const; - int InputInt(); + int InputInt(const char* prompt); + char* PromptCharInput(const char* prompt, long long maxlen); void Print() const; - - private: char* _firstname{ }; char* _lastname{ }; @@ -50,16 +52,8 @@ private: char* _city{ }; char* _state{}; char* _email{ }; - - int _zip = 0; + int _zip = 0; }; - - - - - - - #endif diff --git a/Homework7/Homework7/ContactList.cpp b/Homework7/Homework7/ContactList.cpp index c85ee80..70d9122 100644 --- a/Homework7/Homework7/ContactList.cpp +++ b/Homework7/Homework7/ContactList.cpp @@ -1,5 +1,7 @@ #include "ContactList.h" #include "MenuHelper.h" + + Contact* ContactList::AllocateContactList(const size_t& size) { Contact* contacts = nullptr; @@ -10,6 +12,7 @@ Contact* ContactList::AllocateContactList(const size_t& size) return contacts; } + ContactList::ContactList(const size_t& size) { length_ = size; @@ -17,6 +20,7 @@ ContactList::ContactList(const size_t& size) contacts_ = AllocateContactList(size); } + ContactList& ContactList::operator=(const ContactList& rhs) { if (rhs.contacts_ != nullptr) @@ -37,10 +41,12 @@ ContactList& ContactList::operator=(const ContactList& rhs) return *this; } + ContactList::ContactList(ContactList&& move) { *this = move; } + ContactList& ContactList::operator=(ContactList&& rhs) { if (this != &rhs) @@ -63,10 +69,12 @@ ContactList& ContactList::operator=(ContactList&& rhs) return *this; } + ContactList::ContactList(const ContactList& copy) { *this = copy; } + ContactList::~ContactList() { delete[] contacts_; @@ -74,19 +82,51 @@ ContactList::~ContactList() } -void ContactList::DeleteContact(Contact& contact) +void ContactList::DeleteContact(const char* prompt, ContactList contacts) { - Contact empty(contact); - empty.SetFirstName(nullptr); - empty.SetLastName(nullptr); - empty.SetEmail(nullptr); - empty.SetStreetAddress(nullptr); - empty.SetState(nullptr); - empty.SetCity(nullptr); - empty.SetZip(NULL); - - contact = empty; - + contacts.PrintList(); + + int i = 0; + std::cout << prompt << std::endl; + + std::cin >> i; + + while (!std::cin || i > size_) + { + + std::cout << prompt << std::endl; + std::cin.clear(); + + cin.ignore(MAX_STREAM_SIZE, '\n'); + std::cin >> i; + + } + + contacts_[i].SetFirstName(nullptr); + contacts_[i].SetLastName(nullptr); + contacts_[i].SetEmail(nullptr); + contacts_[i].SetStreetAddress(nullptr); + contacts_[i].SetState(nullptr); + contacts_[i].SetCity(nullptr); + contacts_[i].SetZip(NULL); + + if (i == 0) //if you're deleting the first contact + { + for (auto j = 0u; j < size_; j++) + { + contacts_[j] = contacts_[j+1]; + } + + } + if (i > 0 && i < size_) //if you're deleting anything in the middle + { + for (i; i < size_; i++) + { + contacts_[i] = contacts_[i + 1]; + } + } + + size_--; //decrement the size of array because of removal } void ContactList::CopyList(const Contact* contacts, const size_t& length) @@ -103,20 +143,22 @@ 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; } void ContactList::PrintList() const { for (auto i = 0u; i < size_; i++) { - contacts_[i].Print(); + std::cout <<"Contact:" << i; contacts_[i].Print(); std::cout << std::endl; } } size_t ContactList::Size() const { - return size_; } + + + diff --git a/Homework7/Homework7/ContactList.h b/Homework7/Homework7/ContactList.h index 8b2b1ee..4b3354f 100644 --- a/Homework7/Homework7/ContactList.h +++ b/Homework7/Homework7/ContactList.h @@ -1,6 +1,7 @@ #ifndef CONTACT_LIST_H #define CONTACT_LIST_H #include "Contact.h" +constexpr size_t MAX = 5; class ContactList { @@ -15,11 +16,12 @@ public: ~ContactList(); - void DeleteContact(Contact& contact); + void DeleteContact(const char* prompt, ContactList contacts); 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 + private: Contact* contacts_{ nullptr }; @@ -31,9 +33,4 @@ private: }; - - - - - #endif diff --git a/Homework7/Homework7/Menu.cpp b/Homework7/Homework7/Menu.cpp index a60cc2d..9443c50 100644 --- a/Homework7/Homework7/Menu.cpp +++ b/Homework7/Homework7/Menu.cpp @@ -1,33 +1,11 @@ #include "MenuHelper.h" #include "ContactList.h" -using std::numeric_limits; -using std::streamsize; - -constexpr size_t MAX_STREAM_SIZE = numeric_limits<streamsize>::max(); -using std::cin; - -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; -} - +#include "Contact.h" void Menu() { char Options = '\0'; - ContactList contacts(3); + ContactList contacts(MAX); do { @@ -46,7 +24,7 @@ void Menu() PrintContact(contacts); break; case('3'): - //contacts.DeleteContact(); + contacts.DeleteContact("Which contact would you like to delete?", contacts); break; case('4'): std::cout << "Thank you, have a great day!" << std::endl; @@ -65,13 +43,13 @@ 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()); + newContact.SetFirstName(newContact.PromptCharInput("What is your first name?: ", 101)); + newContact.SetLastName(newContact.PromptCharInput("What is your last name?: ", 101)); + newContact.SetStreetAddress(newContact.PromptCharInput("What is your street address?: ", 101)); + newContact.SetEmail(newContact.PromptCharInput("What is your email?: ", 101)); + newContact.SetState(newContact.PromptCharInput("What is your state?: ", 101)); + newContact.SetCity(newContact.PromptCharInput("What is your city?: ", 101)); + newContact.SetZip(newContact.InputInt("What is your zip?")); return newContact; @@ -88,7 +66,6 @@ ContactList ContactAdder(Contact& contact) ContactList contacts; contacts.AddContact(contact); - return contacts; } diff --git a/Homework7/Homework7/MenuHelper.h b/Homework7/Homework7/MenuHelper.h index 41c904b..b23c8eb 100644 --- a/Homework7/Homework7/MenuHelper.h +++ b/Homework7/Homework7/MenuHelper.h @@ -3,11 +3,12 @@ #include "Contact.h" #include "ContactList.h" -char* PromptCharInput(const char* prompt, long long maxlen); void Menu(); Contact InputContact(); + void PrintContact(ContactList& contacts); + ContactList ContactAdder(Contact& contact); diff --git a/Homework7/Homework7/main.cpp b/Homework7/Homework7/main.cpp index dadec00..d9753ec 100644 --- a/Homework7/Homework7/main.cpp +++ b/Homework7/Homework7/main.cpp @@ -10,9 +10,7 @@ int main() { - Menu(); - return 0; }
\ No newline at end of file |