diff options
| author | Connor McDowell <[email protected]> | 2024-03-03 15:37:39 -0800 |
|---|---|---|
| committer | Connor McDowell <[email protected]> | 2024-03-03 15:37:39 -0800 |
| commit | c9aba80e18f2d6db8793c182bc0117a102df4777 (patch) | |
| tree | f22f05926efafc7c4df94e2736f4d5714f4eb6c5 /Project1 | |
| parent | first commit of the day, following lecture. again. (diff) | |
| download | homework-7-connormcdowell275-c9aba80e18f2d6db8793c182bc0117a102df4777.tar.xz homework-7-connormcdowell275-c9aba80e18f2d6db8793c182bc0117a102df4777.zip | |
temp refactored add. still needs cleaning
Diffstat (limited to 'Project1')
| -rw-r--r-- | Project1/Contact_list.h | 15 | ||||
| -rw-r--r-- | Project1/contacts.cpp | 113 | ||||
| -rw-r--r-- | Project1/program.cpp | 39 |
3 files changed, 89 insertions, 78 deletions
diff --git a/Project1/Contact_list.h b/Project1/Contact_list.h index 1748a44..18348a1 100644 --- a/Project1/Contact_list.h +++ b/Project1/Contact_list.h @@ -13,8 +13,12 @@ private: size_t size_{ 0 }; + contact* allocateContactList(const size_t& size); + + public: contact_list() = default; + contact_list(const size_t& size); ~contact_list(); void set_length(size_t MAX); @@ -23,17 +27,18 @@ public: void set_size(size_t size); size_t get_size(); - void DeleteContact(contact newContact[], size_t MAX); + void DeleteContact(contact& contact); void CopyList(const contact* contacts, const size_t& size); - void AddContact(const contact& contact, size_t MAX, size_t& t); - - void Update(contact newContact, size_t MAX); + void AddContact(const contact& contact); - void Print(contact newContact[], size_t& MAX) const; + void Print() const; size_t Size() const; + + void Update(contact newContact, size_t MAX); + }; diff --git a/Project1/contacts.cpp b/Project1/contacts.cpp index 2b52062..4de1d59 100644 --- a/Project1/contacts.cpp +++ b/Project1/contacts.cpp @@ -7,13 +7,34 @@ using std::cin; using std::cout; using std::endl; -//getters n setters +// constructors contact_list::~contact_list() { - + delete[] contacts_; + contacts_ = nullptr; +} + +contact* contact_list::allocateContactList(const size_t& size) +{ + contact* contacts = nullptr; + + length_ = size; + + contacts = new contact[size]; + + return contacts; } +contact_list::contact_list(const size_t& size) +{ + length_ = size; + contacts_ = allocateContactList(size); +} + + +//getters n setters + void contact::Set_firstName(const char* firstName) { _firstName = firstName; @@ -123,30 +144,23 @@ size_t contact::Get_id() } //functions -void contact_list::AddContact(const contact& newContact, size_t MAX, size_t& t) -{ - //using couts to prompt using for all parts of newContact struct. - //update plans: check for newContact[i].bool value, if false, use a modified void update - //(new function thats just void update with different couts) to be called with an if statement. - //or another for. - //for (size_t i = 0; i < max/length; i++) - //if (bool == false) - // void mod_update - // to check for any "empty"/deleted contacts before adding one. if bool == false should include break after mod_update. +void contact_list::AddContact(const contact& contact) +{ + contacts_[size_++] = contact; //CURRENTLY TESTING - for (size_t i = 0; i < MAX, i++;) + for (size_t i = 0; i < size_, i++;) { - int a = newContact[i].Get_a(); - for(a = 1; ++i;) + int a = contact.Get_a(); + for (a = 1; ++i;) { - newContact[i].Set_a(0); - int temp = newContact[i].Get_id(); - for (auto j = temp; j < MAX; j++) + contact.Set_a(0); + int temp = contact[i].Get_id(); + for (auto j = temp; j < size_; j++) { - if(newContact[i].Get_id() != 0) - { + if (contact.Get_id() != 0) + { i = i + 1; - } + } } char firstName[30]; char lastName[30]; @@ -155,36 +169,36 @@ void contact_list::AddContact(const contact& newContact, size_t MAX, size_t& t) char City[35]; char State[4]; int Zip = 0; - newContact[i].Set_id(i); + contact.Set_id(i); //newContact[i].Set_a(t); cin.ignore(1000, '\n'); cout << "Please enter each piece of information when you are prompted to" << endl; cout << "enter first name: " << endl; cin >> firstName; - newContact[i].Set_firstName(firstName); + contact.Set_firstName(firstName); cout << "enter last name: " << endl; cin >> lastName; - newContact[i].Set_lastName(lastName); + contact.Set_lastName(lastName); cout << "enter Email: " << endl; cin >> Email; - newContact[i].Set_email(Email); + contact.Set_email(Email); cout << "enter Street Address: " << endl; cin >> StreetAddress; - newContact[i].Set_streetAddress(StreetAddress); + contact.Set_streetAddress(StreetAddress); cout << "enter city: " << endl; cin >> City; - newContact[i].Set_city(City); + contact.Set_city(City); cout << "enter State as two letter abbreviation: " << endl; cin >> State; - newContact[i].Set_state(State); + contact.Set_state(State); cout << "Please enter the next value as a series of numbers" << endl; cout << "enter Zip: " << endl; cin >> Zip; - newContact[i].Set_zip(Zip); + contact.Set_zip(Zip); break; - } + } } - for (size_t i = t ; i < MAX; i++) + for (size_t i = t; i < MAX; i++) { char firstName[30]; char lastName[30]; @@ -193,52 +207,41 @@ void contact_list::AddContact(const contact& newContact, size_t MAX, size_t& t) char City[35]; char State[4]; int Zip = 0; - newContact[i].Set_a(0); - newContact[i].Set_id(i+1); + contact.Set_a(0); + contact.Set_id(i + 1); //newContact[i].count = t; cin.ignore(1000, '\n'); cout << "Please enter each piece of information when you are prompted to" << endl; cout << "enter first name: " << endl; cin >> firstName; - newContact[i].Set_firstName(firstName); + contact.Set_firstName(firstName); cout << "enter last name: " << endl; cin >> lastName; - newContact[i].Set_lastName(lastName); + contact.Set_lastName(lastName); cout << "enter Email: " << endl; cin >> Email; - newContact[i].Set_email(Email); + contact.Set_email(Email); cout << "enter Street Address: " << endl; cin >> StreetAddress; - newContact[i].Set_streetAddress(StreetAddress); + contact.Set_streetAddress(StreetAddress); cout << "enter city: " << endl; cin >> City; - newContact[i].Set_city(City); + contact.Set_city(City); cout << "enter State as two letter abbreviation: " << endl; cin >> State; - newContact[i].Set_state(State); + contact.Set_state(State); cout << "Please enter the next value as a series of numbers" << endl; cout << "enter Zip: " << endl; cin >> Zip; - newContact[i].Set_zip(Zip); + contact.Set_zip(Zip); t++; break; } - + //cout << newContact[i]->Name << "\n" << newContact[i]->Email << "\n" << newContact[i]->StreetAddress << "\n" << newContact[i]->City << "\n" << newContact[i]->State << "\n" << newContact[i]->Zip << endl; - -} -// important below. may need if current testing doesnt work. -//void contact_list::AddContact(const contact& contact) -//{ -// size_t length = contact_list::length_; -// for (size_t i = 0; i < length, i++;) -// { -// -// } -// -//} +} void contact_list::Update(contact newContact, size_t MAX) { @@ -462,4 +465,6 @@ void contact_list::DeleteContact(contact newContact[], size_t MAX) void contact_list::CopyList(const contact* contacts, const size_t& size) { -}
\ No newline at end of file +} + + diff --git a/Project1/program.cpp b/Project1/program.cpp index c887eac..d6761d5 100644 --- a/Project1/program.cpp +++ b/Project1/program.cpp @@ -3,7 +3,7 @@ // class: CST116 // reason: homework number 6 -#include "Contacts.h" +#include "Contact_list.h" #include <iostream> using std::cin; @@ -17,12 +17,12 @@ int main() size_t MAX = 3; int O = 1; /* t = index counter*/ - //contact newContact[]; - contact* newContact = new contact[MAX]; - contact_list contact_list; + contact newContact; + //contact* newContact = new contact[MAX]; + contact_list contacts(MAX); size_t t = 0; - contact_list.set_size(sizeof(newContact[MAX])); + contacts.set_size(sizeof(newContact)); while (O == 1) { //cout << MAX << endl; @@ -39,26 +39,27 @@ int main() { case 1: //addNew(newContact, MAX, t); - contact_list.AddContact(*newContact, MAX, t); - if (t >= MAX) // Check if the number of contacts exceeds MAX - { - contact* newContactTemp = new contact[MAX * 2]; // Double the size - for (size_t i = 0; i < MAX; ++i) // Copy existing contacts - newContactTemp[i] = newContact[i]; - delete[] newContact; // Deallocate old memory - newContact = newContactTemp; // Update pointer - MAX *= 2; // Update MAX - } + contacts.AddContact(newContact); + //if (t >= MAX) // Check if the number of contacts exceeds MAX + //{ + // contact* newContactTemp = new contact[MAX * 2]; // Double the size + // for (size_t i = 0; i < MAX; ++i) // Copy existing contacts + // newContactTemp[i] = newContact[i]; + // delete[] newContact; // Deallocate old memory + // newContact = newContactTemp; // Update pointer + // MAX *= 2; // Update MAX + //} + break; case 2: - contact_list.Update(*newContact, MAX); + contacts.Update(newContact, MAX); break; case 3: - contact_list.Print(newContact, MAX); // Print only the existing contacts + contacts.Print(&newContact, MAX); // Print only the existing contacts break; case 4: - contact_list.DeleteContact(newContact, MAX); - contact_list.CopyList(newContact, MAX); + contacts.DeleteContact(&newContact, MAX); + contacts.CopyList(&newContact, MAX); break; case 5: O = 0; |