Strings and modifyng them

I have the following exercise:

3. Write a program that contains four arrays. Three of the arrays should contain your
first name, middle initial, and last name. Use the string-copying function presented
in today’s lesson to copy these strings together into the fourth array, full name.

Problem is, I'm using "strcpy" but I can only copy one string in the other, and if I do it again, it overwrites the first one, I'm out of solutions, any ideas?
Last edited on
You should use strncat instead of strcpy.
Thank you very much, I curse the day I bought this book, but I'll keep on trying to finish it since I started, forgot to ask though, if you have any other material that could help me, I would appreciate it a lot.
strcat(dest, src) is the same as strcpy(dest+strlen(dest), src), if you must.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>

using namespace std;

main ()
{
     char cArray1 [] = "Jesse";
     char cArray2 [] = "Micheal";
     char cArray3 [] = "Leonard";
     char FullName [100];
     strcat(FullName, cArray3, cArray2, cArray1);
     cout << "This is my full name " << FullName <<endl;
     system("PAUSE");
     return 0;
}     

When I try it, I get an error and I'm sent to the string.h, what am I doing wrong?

The error is here, _CRTIMP char* __cdecl strcat (char*, const char*);, it says there are too many arguments, do I need to write the command in some other manner?
Last edited on
Check out this tutorial:
http://www.learncpp.com

Last edited on
Thanks Wisely Done, I will try to use it in tandem with my book, but do you have any idea why my code isn't functioning?
Try this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <cstdlib>
#include <cstdio>
#include <string>
#include <cstring>


using namespace std;

main ()
{
     char cArray1 [] = "Jesse ";
     char cArray2 [] = "Micheal ";
     char cArray3 [] = "Leonard ";
     char FullName [100];
     strcat(FullName, cArray1 );
     strcat(FullName, cArray2 );
     strcat(FullName, cArray3 );
     cout << "This is my full name " << FullName <<endl;
     system("PAUSE");
     return 0;
}
Nvm, got it, thanks guys, so it's the same as strcpy if just doesn't overwrite.

Edit: :)) right when I saw it, thanks Wisely Done.
Last edited on
strcpy is usually used when copying to an empty string. While strcat appends one string to another. You should use strncat instead of strcat because strncat has a length check preventing buffer overflow.
Last edited on
Topic archived. No new replies allowed.