Concatenating Strings

How do I concatenate the strings so that there is a space between the words?

And then what would be the best method for passing the pointer to a display 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
88
89
90
91
92
93
94
95
96
//=============================================================================
// Program Requirements
// 1. Prompt User for Last, 1st, and Middle Name
// 2. Place each entry into separate fixed size array
// 3. Dynamically allocate an array to store all name parts.
// 4. Copy name parts in order into new array.
// Note: Be sure to have a space between names.
// 5. Pass new array into function to display name
// 6. Deallocate the array.
//=============================================================================
//*****************************************************************************
// 				BEGINNING OF PROGRAMMING CODE
//****************************************************************************

#include <iostream>					// for cin,cout
#include <cstring>					// for string manipulation
 
using namespace std;
// =============================================================================

int main()
{
		  // declare local constant(s), variable(s), and array(s)
	char LName[32];					// fixed size array
	char FName[16];					// fixed size array
	char MName[16];					// fixed size array
	int go;							// input to start
	char *namePtr;          		// pointer
	char fullname;					// new array for full name


	
		// Start program
	cout << "Enter 1 to Continue 0 to Quit" << endl;		
	cin >> go;
	cin.ignore();					// clear input buffer

		// Prompt User for Last, 1st, and Middle Name
	 while (go > 0)
	 {
		cout << "\nEnter Last Name\n";
		cin.getline ( LName, 32, '\n');			// set 1st array
		cout << "\nEnter First Name\n";		
		cin.getline ( FName, 16, '\n');			// set 2nd array
		cout << "\nEnter Middle Name\n";
		cin.getline ( MName, 16, '\n');			// set 3rd array

		//Dynamically allocate an array to store all name parts.
												// determine size of new array
		namePtr = new char [strlen( FName )+1 + strlen ( MName )+1 + strlen ( LName )+1];

		
		// Copy name parts in order into new array.
		strcpy (namePtr, FName);
		strcat (namePtr, MName);
		strcat (namePtr, LName);

		// Function to Display Name
		cout<<"\nHello "<< namePtr <<endl;		// not a function but it'll do for now

		// De-allocate array
		delete []namePtr;						// free memory pointed to by name


		// Option to Continue
		cout << "Enter 1 to Continue 0 to Quit" << endl;
		cin >> go;
	 }

		
 
	return 0;
}

/*
OUTPUT
Enter 1 to Continue 0 to Quit
1


Enter Last Name

Smith


Enter First Name

Elizabeth


Enter Middle Name

Jean
Hello ElizabethJeanSmith
Enter 1 to Continue 0 to Quit
*/

mommabear wrote:
How do I concatenate the strings so that there is a space between the words?

The best way to concatenate strings is to use strings rather than character arrays.

http://www.cplusplus.com/reference/string/string/

mommabear wrote:
And then what would be the best method for passing the pointer to a display function?

Don't. Pass a string instead.
Topic archived. No new replies allowed.