diff options
| author | Connor McDowell <[email protected]> | 2024-02-21 16:51:29 -0800 |
|---|---|---|
| committer | Connor McDowell <[email protected]> | 2024-02-21 16:51:29 -0800 |
| commit | 2f564c37810887b388bacd7d4fff2f438b0e4a67 (patch) | |
| tree | 83db4e8145c05861a992a6bcd8fe5c322bb011c4 | |
| parent | gunna call it and talk with prof during office hours (diff) | |
| download | homework-6-connormcdowell275-2f564c37810887b388bacd7d4fff2f438b0e4a67.tar.xz homework-6-connormcdowell275-2f564c37810887b388bacd7d4fff2f438b0e4a67.zip | |
doubling working now
| -rw-r--r-- | Project1/Contacts.h | 11 | ||||
| -rw-r--r-- | Project1/contacts.cpp | 52 | ||||
| -rw-r--r-- | Project1/program.cpp | 52 |
3 files changed, 64 insertions, 51 deletions
diff --git a/Project1/Contacts.h b/Project1/Contacts.h index 0119030..e46da9c 100644 --- a/Project1/Contacts.h +++ b/Project1/Contacts.h @@ -10,12 +10,13 @@ struct contact bool a = true; size_t id = 0; size_t count = 0; - char Name[30]{}; - char Email[105]{}; - char StreetAddress[45]{}; - char City[35]{}; - char State[4]{}; + char Name[30]; + char Email[105]; + char StreetAddress[45]; + char City[35]; + char State[4]; int Zip = 0; + contact() : a(true), id(0), count(0), Zip(0) {} }; diff --git a/Project1/contacts.cpp b/Project1/contacts.cpp index 4c9814b..e542d64 100644 --- a/Project1/contacts.cpp +++ b/Project1/contacts.cpp @@ -116,33 +116,43 @@ contact contact_double(contact*& newContact, size_t& MAX, size_t t) //supposedly doubles length. doesn't work. //current ideas: add if loop to whole main with the id counter t and if length of newContact = max double the length. //current problems: using const size_t max prevents editing size for the whole function, and it resets as soon as the while loop loops or leaves the scope of the if statement. + //contact* doubleContact = new contact[MAX * 2]; + //for (auto a = 0u; a < MAX; ++a) + //{ + // doubleContact[a].a = newContact[a].a; + // doubleContact[a].id = newContact[a].id; + // doubleContact[a].count = newContact[a].count; + // doubleContact[a].Name[25] = newContact[a].Name[25]; + // doubleContact[a].Email[100] = newContact[a].Email[100]; + // doubleContact[a].StreetAddress[35] = newContact[a].StreetAddress[35]; + // doubleContact[a].City[30] = newContact[a].City[30]; + // doubleContact[a].State[3] = newContact[a].State[3]; + // doubleContact[a].Zip = newContact[a].Zip; + // cout << "List number: " << doubleContact[a].id << endl; + // cout << "name: " << doubleContact[a].Name << endl; + // cout << "Email: " << doubleContact[a].Email << endl; + // cout << "Address: " << doubleContact[a].StreetAddress << endl; + // cout << "city: " << doubleContact[a].City << endl; + // cout << "state: " << doubleContact[a].State << endl; + // cout << "Zip: " << doubleContact[a].Zip << endl; + //} + //newContact = doubleContact; + //delete[] doubleContact; + ////delete[] newContact; + //MAX = MAX * 2; + ////printAll(&doubleContact[MAX], MAX); + ////printAll(&newContact[MAX], MAX); + //return newContact[MAX]; + contact* doubleContact = new contact[MAX * 2]; for (auto a = 0u; a < MAX; ++a) { - doubleContact[a].a = newContact[a].a; - doubleContact[a].id = newContact[a].id; - doubleContact[a].count = newContact[a].count; - doubleContact[a].Name[30] = newContact[a].Name[30]; - doubleContact[a].Email[100] = newContact[a].Email[100]; - doubleContact[a].StreetAddress[35] = newContact[a].StreetAddress[35]; - doubleContact[a].City[30] = newContact[a].City[30]; - doubleContact[a].State[3] = newContact[a].State[3]; - doubleContact[a].Zip = newContact[a].Zip; - cout << "List number: " << doubleContact[a].id << endl; - cout << "name: " << doubleContact[a].Name << endl; - cout << "Email: " << doubleContact[a].Email << endl; - cout << "Address: " << doubleContact[a].StreetAddress << endl; - cout << "city: " << doubleContact[a].City << endl; - cout << "state: " << doubleContact[a].State << endl; - cout << "Zip: " << doubleContact[a].Zip << endl; + doubleContact[a] = newContact[a]; } + delete[] newContact; newContact = doubleContact; - delete[] doubleContact; - //delete[] newContact; MAX = MAX * 2; - //printAll(&doubleContact[MAX], MAX); - //printAll(&newContact[MAX], MAX); - return newContact[MAX]; + return newContact[MAX - 1]; } size_t max_double(size_t MAX) diff --git a/Project1/program.cpp b/Project1/program.cpp index acf6c8a..a75931b 100644 --- a/Project1/program.cpp +++ b/Project1/program.cpp @@ -33,33 +33,35 @@ int main() cout << "5. Quit\n"; cout << "\nEnter your choice: " << endl; cin >> c; - if (t == MAX) - { - newContact[MAX] = contact_double(newContact, MAX, t); - //MAX = max_double(MAX); - } - if (c == 1) - { - addNew(&newContact[MAX], MAX, t); - //cout << t << endl; - } - if (c == 2) - { - update(&newContact[MAX], MAX); - } - if (c == 3) - { - printAll(&newContact[MAX], MAX); - } - if (c == 4) - { - delete_contact(&newContact[MAX]); - } - if (c == 5) + switch (c) { + case 1: + addNew(newContact, MAX, t); + ++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 + } + break; + case 2: + update(newContact, MAX); + break; + case 3: + printAll(newContact, t); // Print only the existing contacts + break; + case 4: + delete_contact(newContact); + break; + case 5: O = 0; - delete[] newContact; - //abort; + break; + default: + cout << "Invalid choice. Please enter a number from 1 to 5." << endl; } } return 0; |