Turning a whole array into an entity for another!

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)

Thanks Again :D
You really should consider the use of the string class.
http://www.cplusplus.com/reference/string/

It won't automatically limit the length to 10 as in your example, but you can check the size of what was entered
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
string holdFirst;
string loadedContactsFirst;
bool first_ok = false;

while (! first_ok)
{ holdFirst.clear();
   cout << "Enter first name: " << endl;
   cin >> holdFirst;
   if (holdFirst.size() > 10)
    cout << "First name too long" << endl;
   else
      first_ok = true;
}
loadedContactsFirst = holdFirst;
...etc


p.s. Don't forget to check if the field is empty.

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.
Well thanks for the help guys. After some thought I came up with this as my add contact function.
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
void  addContact()
{
	std::string holdFirst;
	std::string holdLast;
	std::string holdPhone;
	bool continueAsking = 1;
	while(continueAsking = 1)
	{
		std::cout << "What is contact's first name? Up to 10!" << std::endl;
		std::cin >> holdFirst;

		if(holdFirst.size() > 10){
			std::cout << "Keep the length under 10 characters!" << std::endl;
			bool continueAsking = 1;
		

		}
		else{continueAsking = 0;}

	}

	continueAsking = 1;
	while(continueAsking = 1)
	{
		std::cout << "What is contact's last name? Up to 10!" << std::endl;
		std::cin >> holdLast;

		if(holdLast.size() > 10){
			std::cout << "Keep the length under 10 characters!" << std::endl;
			bool continueAsking = 1;

		}
		else{continueAsking = 0;}

	}

	continueAsking = 1;
	while(continueAsking = 1)
	{
		std::cout << "What is phone Number? No spaces no dashes i.e. 1234567890"  << std::endl;
		std::cin >> holdPhone;

		if(holdPhone.size() > 10){
			std::cout << "Keep the length under 10 characters!" << std::endl;
			bool continueAsking = 1;

		}
		else{continueAsking = 0;}
	}


	int currentLocation = 0;

	bool findFirstOpenSpot = 1;


	while(findFirstOpenSpot = 1)
	{
		if(loadedContactsId[currentLocation]==0)
		{
			
			if(loadedContactsId[currentLocation-1] != NULL)
			{
				loadedContactsId[currentLocation] = loadedContactsId[currentLocation-1]+1;
				loadedContactsFirstName[currentLocation] = holdFirst;
				loadedContactsLastName[currentLocation] = holdLast;
				loadedContactsPhone[currentLocation] = holdPhone;
				findFirstOpenSpot = 0;
			}
			if(loadedContactsId[currentLocation-1] == NULL)
			{
				loadedContactsId[currentLocation] = 1;
				loadedContactsFirstName[currentLocation] = holdFirst;
				loadedContactsLastName[currentLocation] = holdLast;
				loadedContactsPhone[currentLocation] = holdPhone;
				findFirstOpenSpot = 0;


			}


		}

		currentLocation++;
	}

}

Last edited on
Also, I am getting an error at line 59 that says

First-chance exception at 0x624acb06 (msvcr100d.dll) in AddressBook.exe: 0xC0000005: Access violation writing location 0x00000000.
Last edited on
Any Ideas?
You are likely reading past the bounds of an array. Also, several of your comparisons (in while loop conditions) are actually assignments.
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).
Last edited on
Here

while(continueAsking = 1)

instead of comparing (==) values you are assigning (=) 1 to continueAsking, It would be more simply to write the statement as

while( continueAsking )


Here

1
2
3
4
		if(holdFirst.size() > 10){
			std::cout << "Keep the length under 10 characters!" << std::endl;
			bool continueAsking = 1;
	}



you declare a new local variable bool continueAsking = 1; which will be destroyed after leaving the if statement.

Here

1
2
3
4
		if(loadedContactsId[currentLocation]==0)
		{
			
			if(loadedContactsId[currentLocation-1] != NULL)

you will get memory violation because you are trying access unexistent element with the index of -1



Thanks vlad, but on the second bit of code
1
2
3
4
if(loadedContactsId[currentLocation]==0)
		{
			
			if(loadedContactsId[currentLocation-1] != NULL)

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!
You can compare currentLocation with 0 before the next if.
Ok I fixed some of those errors and some other errors in my code, but I am still getting an access violation error just at about that same location :3
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
void  addContact()
{
	std::string holdFirst;
	std::string holdLast;
	std::string holdPhone;
	bool continueAsking = 1;
	while(continueAsking)
	{
		std::cout << "What is contact's first name? Up to 10!" << std::endl;
		std::cin >> holdFirst;
		continueAsking = 0;
		if(holdFirst.size() > 10){
			std::cout << "Keep the length under 10 characters!" << std::endl;
			continueAsking = 1;
		

		}

	}

	continueAsking = 1;
	while(continueAsking)
	{
		std::cout << "What is contact's last name? Up to 10!" << std::endl;
		std::cin >> holdLast;
		continueAsking = 0;
		if(holdLast.size() > 10){
			std::cout << "Keep the length under 10 characters!" << std::endl;
			continueAsking = 1;

		}

	}

	continueAsking = 1;
	while(continueAsking)
	{
		std::cout << "What is phone Number? No spaces no dashes i.e. 1234567890"  << std::endl;
		std::cin >> holdPhone;
		continueAsking = 0;
		if(holdPhone.size() > 10){
			std::cout << "Keep the length under 10 characters!" << std::endl;
			continueAsking = 1;

		}
	}


	int currentLocation = 0;

	bool findFirstOpenSpot = 1;


	while(findFirstOpenSpot = 1)
	{
		if(loadedContactsId[currentLocation]==0)
		{
			if(currentLocation == 0)
			{
				loadedContactsId[currentLocation] = 1;
				loadedContactsFirstName[currentLocation] = holdFirst;
				loadedContactsLastName[currentLocation] = holdLast;
				loadedContactsPhone[currentLocation] = holdPhone;
				findFirstOpenSpot = 0;
				
			}
			if(currentLocation > 0)
			{
				loadedContactsId[currentLocation] = loadedContactsId[currentLocation-1]+1;
				loadedContactsFirstName[currentLocation] = holdFirst;
				loadedContactsLastName[currentLocation] = holdLast;
				loadedContactsPhone[currentLocation] = holdPhone;
				findFirstOpenSpot = 0;
			}


		}

		currentLocation++;
	}

}
Last edited on
Ok, I think I got it int he if statement I couldn't do if(currentLocation is > 0) i had to use
if(currentLocation >= 1)...Anyone know why?
Topic archived. No new replies allowed.