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:
#include <iostream>
#include <string>
usingnamespace 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)
{
}elseif (option == 2)
{
}elseif (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.
#include <iostream>
usingnamespace 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;
}