[Help] Writing a simple address book

Good day, I am new to c++ and have decided to do a project that can start of simple and can be upgraded as I get better to help me learn. I have chosen to make an address book. Well the problem I am having is with arrays and how to pass them to functions correctly, I am also interested in how to form a char array from 2 smaller char arrays. Here is the code I have come up with so far:

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
#include <iostream>
#include <string>

using namespace std;

void add_contact();

class Person
{
	char first_name[13];
	char second_name[13];
	char name[26];
	char number[11];
	public:
	void set_name(char[], char[]);
	char get_name();
};

void Person::set_name(char x[], char y[])
{
	first_name = x;
	second_name = y;
	name = first_name+' '+second_name; // how to make a single char array from 2 smaller char arrays

}

char Person::get_name()
{
	return name;
}

void add_contact()
{
	Person newp;
	char fname[13], sname[13];
	printf("Enter first name:\n");
	cin>>fname;
	printf("Enter second name:\n");
	cin>>sname;
	newp.set_name(fname, sname);// pass first and second name
	cout<<newp.get_name();// display full name to check it has worked
}

int main()
{
	bool end = false;


	do{

		printf("------------------------Address Book------------------------\n\nWhat would you like to do?\n1.Search address book\n2.View contacts\n3.Add contact\n");
		char option = 'a';
		cin>>option;
		if (option == 1)
		{
		}else if (option == 2)
		{
		}else if (option == 3)
		{
			add_contact();
		}
		
		
		printf("Are you finished with the address book? (Y/N)\n");
		{
			char finish = 'a';
			cin>>finish;
			if (finish == 'y')
			{
				end = true;
			}else
			{
				end = false;
			}
		}


	}while(end == false);

	return 0;
}


I have tried googling but have not come up with any results that I understand. I would really appreciate if someone could tell me how to solve the above issues and explain why it works.

Thanks a lot
Last edited on
One error I immediately got is in the void Person::set_name(char x[], char y[]) function.

It does not specify what char in the array to overwrite. You need to specify what char in the array to overwrite, as follows:

first_name[placeholder] = x;
second_name[placeholder] = y;

Placeholder is whatever you want it to be. I would use a counter or static variable if you want placeholder to be changeable.

Here is an example of how to split an array:

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
#include <iostream>

using namespace std;

void main()
{
	char tobesplit [11]={'1','2','3','4','5','6','7','8','9','0'}; // The null terminating character, \0, takes up the last spot of the array
	char split1 [6]; 
	char split2 [6];

	for(int x=0;x<5;x++) //This is the code that actually does the splitting
	{
		split1[x]=tobesplit[x];

		split2[x]=tobesplit[x+5];
	}

	for(int x=0;x<10;x++) // From this line down, it is just an example to show the code works
	{
		cout<<tobesplit[x]<<' ';
	}

	cout<<endl;

	for(int x=0;x<5;x++)
	{
		cout<<split1[x]<<' ';
	}

	for(int x=0;x<5;x++)
	{
		cout<<split2[x]<<' ';
	}

	cout<<endl;
}


Good luck.
Thanks! here is my implementation for it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
void Person::set_name(char x[], char y[])
{
	for (int i = 0; i <13; i++)
	{
		first_name[i] = x[i];
		second_name[i] = y[i];
	}

	for (int i = 0; i < 13; i++)
	{
		name[i] = first_name[i];
	}

	name[14] = ' ';

	for (int i = 15; i <28; i++)
	{
		name[i] = second_name[i];
	}

	cout<<first_name<<' '<<second_name<<endl;
}


I am sure there are much better ways of doing this but I will stick with this and refine later on.
Topic archived. No new replies allowed.