hey guys, so I am creating an address book for practice. But I need some help with arrays. So when the user goes to add a new contact it will ask them for a few things.
First Name, Last Name, Phone (eventually more)...
OK, so lets say now they enter the data...Charlie Doe 9534874000.
How would I make it so the data they enter can become separated and each character or number be entered into an array that it corresponds to i.e.
1 2 3
char holdFirst[10];
char holdLast[10];
int holdPhone[10];
I am putting them into arrays because I want the max length of each data type to be stated such as above. Now after the data is put it in the array how would i make it so when done on the entering function it will be added to the currently loaded contacts data i.e. loadedContactsFirst = holdFirst; ???
Practically now I have to do the reverse of the first because in the first I do it so it limits what they enter.
If you need a clearer insight on anything just comment. Please help if you can.
Also if there is another way I can limit the data they put in it would be great to know(but I would still like to know both methods)
It sounds like you need a collection class of some kind. You could use the vector class to store all your contact details.
Also, your life would be made much easier if you converted each address to either a struct or a class. Using a struct would be easier than a class, since a contact detail is just a collection of data, and has no need for methods.
The struct:
1 2 3 4 5 6 7 8 9 10
struct ContactDetail
{
char first[10];
char last[10];
int phone[10];
// or if you were to use strings:
// string first;
// string last;
// int phone;
};
And in the reading section of the code:
1 2 3 4 5 6 7 8 9
vector<ContactDetail> contactList;
ContactDetail contact;
cout << "Enter first name: ";
cin >> contact.first;
// validation and what not has been omitted because I'm lazy
// and to enter it into the vector:
contactList.pushBack(contact);
I didn't test this and it's been a while since I've used vectors, so you may encounter issues with that side of things.
Ok, @abstractionAnon
Good idea, and I will definitely use it for the stuff like names and addresses, but how would I do that with numbers, Do you think numbers (phone numbers, zipcodes) should also be used as a string? And btw its actually loadedContactsFirstName[currentLoaction] its storign data at the specifc part of the array accross multiple arrays.
@PalmTree Magician
This doesn't seem to limit anything at all. As in I can put anything I want in there, and it also doesn't seem to hold the idea that it has to write the data it is holding into an array so that the array can save its data to a file. And besides I haven't learned what vectors are nor struct, I think of vector and struct as a little more advanced.
if(loadedContactsId[currentLocation-1] != NULL) when currentLocation is zero.
The Palm Tree Magician's advise is going to make your life much easier.
Why are you so concerned about the length of the string when you are using a string? Also, if you use getline(), you can get someone's full name is one line (then maybe split it into first/last later).
How could I make it look for what location it is at? How could I make it search to see if the first number it looks at is or not available..Actually let me try something first!