How to pass two c-strings to a third c-string

I want to accept a first and last name separately, and then pass them to a third c-string which will output the full name with a space in between. I wish that strcpy would allow me to copy more than one parameter, but it does not. Which path should I take from here? Thanks.

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
  #include <iostream>
#include <cstring>
using namespace std;



int main()
{
	char First[25];
	char Last[25];
	char Full[50];
	
	cout << " Please enter your first name " << endl;
	cin >> First;

	cout << " Please enter your last name " << endl;
	cin >> Last;

	strcpy(Full, First); //I wish it would take more parameters here
	

	cout << Full << endl;

	return 0;

}
You'll probably want to use a combination of strcpy() and strcat() to combine these strings.

1
2
3
strcpy(Full, First);
strcat(Full, " ");
strcat(Full, Last);


Also note that if you were using std::strings instead of C-strings this would be much much easier and less error prone to boot.

1
2
3
4
5
6
string first, last, full;
...

full = first + " " + last;

...

Thank you, I just found the strcat, and it was helpful.

And yes, I will have to gain a better understanding of when to use std::strings, and when to use C-strings.

Thank you.
Use std::strings whenever possible. They are much less error prone and much easier to use. There really should be few times where you must use C-strings in a C++ program and there is a member function to convert the string to a C-string if you "must" use a C-string to interface with so external function.
Topic archived. No new replies allowed.