aboutsummaryrefslogtreecommitdiff
path: root/Homework6ReeceWarner/contacts.cpp
blob: ab296920d14ed9ae3d39330496ee411fcf634fc7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
#include "contacts.h"

//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(const size_t size)
{
	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');

		Prompts("What is your Street");
		cin.ignore();
		cin.get(contacts->Street, 100, '\n');

		Prompts("What is your city?");
		cin.ignore();
		cin.get(contacts->city, 30, '\n');

		Prompts("What is your state?");
		cin.ignore();
		cin.get(contacts->State, 04, '\n');

		Prompts("What is your Zip?");
		cin.ignore();
		cin.get(contacts->Zip, 50, '\n');
	
	return *contacts;

}
void printcontacts()
{
	for (int x = 0; x < numberofcontacts; x++)
	{
		contact empty = emptycontact[x];
		
		contact existingcontacts = contacts[x];
		int result;
		
		result = strcmp(existingcontacts.Name, empty.Name); //Compares the cString with an empty one, when it is equal, it prints nothing!!!
		if (result == 0)
		{
			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();
	int i = ReadInt("Which contact would you like to update?");
	
	contacts[i] = InputNewContact(MAX);
	contact updatedcontacts = contacts[i];
	
	cout << "\n" << endl;

		return updatedcontacts;
	
}
contact DeleteContactInformation()
{
	printcontacts();
		
	contact deletedcontact = contacts[ReadInt("Which contact would you like to delete?")];
	deletedcontact = {'\0'};
	
		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';

	do
	{
		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";
		cin >> options;
		switch (options)
		{
		case('1'):
			
			if (numberofcontacts==3)
			{
				contacts[numberofcontacts++] = InputNewContact(DoubleArraySize(MAX));
			}
			else
			{
				contacts[numberofcontacts++] = InputNewContact(MAX);
			}
			break;
		case('2'):
			UpdateContact();
			break;
		case('3'):
			printcontacts();
			break;
		case('4'):
			DeleteContactInformation();
		
			break;
		case('5'):
			cout << "Have a wondeful day!" << endl;
			DeleteEverything();
			break;
		default:
			cout << "Invalid Input, Try Again!" << endl;
		}

	} 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;
}