diff options
| author | rPatrickWarner <[email protected]> | 2024-02-19 11:49:57 -0800 |
|---|---|---|
| committer | rPatrickWarner <[email protected]> | 2024-02-19 11:49:57 -0800 |
| commit | f7f21bd6757fb54f23d828a8396042da41a06ea9 (patch) | |
| tree | c4fd726b4d3c2a89e5a09905fef43f95fbbdfb52 /Homework6ReeceWarner | |
| parent | init (diff) | |
| download | homework-6-reecepwarner-f7f21bd6757fb54f23d828a8396042da41a06ea9.tar.xz homework-6-reecepwarner-f7f21bd6757fb54f23d828a8396042da41a06ea9.zip | |
changes
Diffstat (limited to 'Homework6ReeceWarner')
| -rw-r--r-- | Homework6ReeceWarner/contacts.cpp | 242 | ||||
| -rw-r--r-- | Homework6ReeceWarner/contacts.h | 35 | ||||
| -rw-r--r-- | Homework6ReeceWarner/program.cpp | 10 |
3 files changed, 146 insertions, 141 deletions
diff --git a/Homework6ReeceWarner/contacts.cpp b/Homework6ReeceWarner/contacts.cpp index 81992fe..ab29692 100644 --- a/Homework6ReeceWarner/contacts.cpp +++ b/Homework6ReeceWarner/contacts.cpp @@ -1,169 +1,102 @@ #include "contacts.h" -#include <iostream> -#include <string> -using std::cout; -using std::cin; -using std::endl; - -contact contacts[MAX] = {}; +//contact contacts[MAX] = {}; contact emptycontact[MAX] = {}; //this is what I am comparing strings with in the print function int numberofcontacts = 0; +contact* contacts = new contact[MAX]; -contact InputNewContact() +contact InputNewContact(const size_t size) { - cout << "Avoid Using Spaces While Entering Your Street Name" << endl; - contact newcontact = {}; - cout << "First Name: "; - cin >> newcontact.FirstName; - - cout << "Last Name: "; - cin >> newcontact.LastName; - - cout << "Email: "; - cin >> newcontact.Email; - - cout << "Street Number: "; - cin >> newcontact.StreetNumber; + contact* contacts = new contact[size]; + + cout.flush(); + cin.ignore(); + + cout.flush(); + Prompts("What is your name?"); + + cout.flush(); + cin.get(contacts->Name, 50, '\n'); + cin.ignore(); + Prompts("What is your Email?"); + + cin.get(contacts->Email, 100, '\n'); - cout << "Street Name: "; - cin >> newcontact.StreetName; + Prompts("What is your Street"); + cin.ignore(); + cin.get(contacts->Street, 100, '\n'); - cout << "City: "; - cin >> newcontact.city; + Prompts("What is your city?"); + cin.ignore(); + cin.get(contacts->city, 30, '\n'); - cout << "State: "; - cin >> newcontact.State; + Prompts("What is your state?"); + cin.ignore(); + cin.get(contacts->State, 04, '\n'); - cout << "Zip: "; - cin >> newcontact.Zip; + Prompts("What is your Zip?"); + cin.ignore(); + cin.get(contacts->Zip, 50, '\n'); - cout << "\n" << endl; - - return newcontact; + return *contacts; } - - void printcontacts() { for (int x = 0; x < numberofcontacts; x++) { contact empty = emptycontact[x]; - + contact existingcontacts = contacts[x]; int result; - result = strcmp(existingcontacts.FirstName, empty.FirstName); //Compares the cString with an empty one, when it is equal, it prints nothing!!! - if (result == 0) - { - cout << " "; - } - else - { - _strupr_s(existingcontacts.FirstName); //permanently changing the string to uppercase - _strupr_s(existingcontacts.LastName); - cout << "Name " << x << ": " << existingcontacts.FirstName << " " << existingcontacts.LastName << endl; - } - result = strcmp(existingcontacts.Email, empty.Email); - if (result == 0) - { - cout << " "; - } - else - { - _strupr_s(existingcontacts.Email); - cout << "Email " << x << ": " << existingcontacts.Email << endl; - } - result = strcmp(existingcontacts.StreetNumber, empty.StreetNumber); - if (result == 0) - { - cout << " "; - } - else - { - _strupr_s(existingcontacts.StreetName); - cout << "Street Address " << x << ": " << existingcontacts.StreetNumber << " " << existingcontacts.StreetName << endl; - } - result = strcmp(existingcontacts.city, empty.city); + result = strcmp(existingcontacts.Name, empty.Name); //Compares the cString with an empty one, when it is equal, it prints nothing!!! if (result == 0) { - cout << " "; - } - else - { - _strupr_s(existingcontacts.city); - cout << "City " << x << ": " << existingcontacts.city << endl; - } - result = strcmp(existingcontacts.State, empty.State); - if (result == 0) - { - cout << " "; - } - else - { - _strupr_s(existingcontacts.State); - cout << "State(xx) " << x << ": " << existingcontacts.State << endl; - } - if (existingcontacts.Zip == 0) - { - cout << " " << endl; + Prompts(" "); + cout << endl; } else { + OutputContacts(_strupr(existingcontacts.Name), "Name", x); + OutputContacts(_strupr(existingcontacts.Email), "Email", x); + OutputContacts(_strupr(existingcontacts.Street), "Street Address", x); + OutputContacts(_strupr(existingcontacts.city), "City", x); + OutputContacts(_strupr(existingcontacts.State), "State", x); cout << "Zip Code " << x << ": " << existingcontacts.Zip << endl; cout << "//////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////\\\\////" << endl; //little divider } - } - } - - contact UpdateContact() { printcontacts(); - - cout << "\n\nWhich contact would you like to update?" << endl; - - int i = 0; - cin >> i; - while (i >= numberofcontacts) //This while loop prevents the user from overwriting onto an undefined array. I tested it without it and I could write onto any integer that I input - { - cout << "Invalid Input, Try again" << endl; - cin >> i; - } + int i = ReadInt("Which contact would you like to update?"); + + contacts[i] = InputNewContact(MAX); + contact updatedcontacts = contacts[i]; - contacts[i] = InputNewContact(); - contact updatedcontacts = contacts[i]; - cout << "\n" << endl; + cout << "\n" << endl; return updatedcontacts; } - - contact DeleteContactInformation() { printcontacts(); - - cout << "\n\nWhich contact would you like to delete?" << endl; - - int i = 0; //I didn't add the while loop from the update contact here because there were no negative consequences from inputing an integer larger than the numberofcontacts counter - cin >> i; - contact deletedcontact = contacts[i]; - contacts[i] = {NULL}; + + contact deletedcontact = contacts[ReadInt("Which contact would you like to delete?")]; + deletedcontact = {'\0'}; - for (i; i < numberofcontacts; i++) //shifts deleted contact to the top - { - contacts[i] = contacts[i + 1]; - } - - return deletedcontact; + for (int i = 0u; i < numberofcontacts; i++) //shifts deleted contact to the top + { + contacts[i] = contacts[i + 1]; + } + + return deletedcontact; } - void menu() { char options = '\0'; @@ -179,7 +112,15 @@ void menu() { switch (options) { case('1'): - contacts[numberofcontacts++] = InputNewContact(); + + if (numberofcontacts==3) + { + contacts[numberofcontacts++] = InputNewContact(DoubleArraySize(MAX)); + } + else + { + contacts[numberofcontacts++] = InputNewContact(MAX); + } break; case('2'): UpdateContact(); @@ -189,10 +130,11 @@ void menu() { break; case('4'): DeleteContactInformation(); - + break; case('5'): cout << "Have a wondeful day!" << endl; + DeleteEverything(); break; default: cout << "Invalid Input, Try Again!" << endl; @@ -201,3 +143,59 @@ void menu() { } while (options != '5'); }; +void Prompts(const char* phrase) +{ + cout.flush(); + cout << phrase << " "; + cout.flush(); + +} + +void OutputContacts(char* arrays, const char* prompt, int x) +{ + cout << prompt << " " << x << ": "; + + cout << arrays << endl; +} + +const size_t DoubleArraySize(const size_t& size) +{ + contact* newArray = nullptr; + + newArray = new contact[size * 2]; + + for (auto i = 0u; i < size; i++) + { + newArray[i] = contacts[i]; + } + + delete[] contacts; + + contacts = newArray; + + const size_t newsize = size * 2; + return newsize; + +} + +int ReadInt(const char* Prompt) +{ + int Value; + + cout << endl << Prompt << ": "; + cin >> Value; + + while (!cin) + { + cin.clear(); + cin.ignore(MAX_STREAM_SIZE, '\n'); + cout << "That is not a number, please try again!" << endl; + cout.flush(); + cin >> Value; + } + return Value; +} +void DeleteEverything() +{ + delete[] contacts; +}
\ No newline at end of file diff --git a/Homework6ReeceWarner/contacts.h b/Homework6ReeceWarner/contacts.h index 9fec259..da1b48e 100644 --- a/Homework6ReeceWarner/contacts.h +++ b/Homework6ReeceWarner/contacts.h @@ -1,30 +1,43 @@ #ifndef contactsheader #define contactsheader +#define _CRT_SECURE_NO_WARNINGS -#define MAX 100 +#include "contacts.h" +#include <iostream> +#include <cstring> + +using std::numeric_limits; +using std::streamsize; +using std::cout; +using std::cin; +using std::endl; + +constexpr size_t MAX_STREAM_SIZE = numeric_limits<streamsize>::max(); +constexpr size_t MAX = 3; struct contact { - char FirstName[50] = {}; - char LastName[50] = {}; + char Name[50] = {}; char Email[100] = {}; - char StreetNumber[50] = {}; - char StreetName[100] = {}; + char Street[100] = {}; char city[30] = {}; char State[4] = {}; - int Zip = 0; - - + char Zip[50] = {}; }; void menu(); -contact InputNewContact(); -contact UpdateContact(); -void printcontacts(); +contact InputNewContact(const size_t size); +contact UpdateContact(); +int ReadInt(const char* Prompt); +void Prompts(const char* phrase); +void OutputContacts(char* arrays, const char* prompt, int x); +void printcontacts(); +const size_t DoubleArraySize(const size_t& size); +void DeleteEverything(); #endif diff --git a/Homework6ReeceWarner/program.cpp b/Homework6ReeceWarner/program.cpp index a888184..770f20b 100644 --- a/Homework6ReeceWarner/program.cpp +++ b/Homework6ReeceWarner/program.cpp @@ -1,13 +1,7 @@ //Name:Reece Warner -//Date:Feb 5th 2024 -//Homework 5 +//Date:Feb 13th 2024 +//Homework 6 #include "contacts.h" -#include <iostream> - - -using std::cout; -using std::cin; -using std::endl; int main() |