combining c-strings and outputting the final result

Hi there guys im trying to combine the first,last and middle names so that they are organized as follows
lastname,',' ' ' firstname, middle name (quotes are the characters comma and space) im just having issues in combining them and when i output it i get some gibberish. please help. this is c++.
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
#include<iostream>
#include<cstring>
#include<string>
using namespace std;

int main()
{
	//Declare/initialize variables/arrays
	const int MAX_SIZE = 100;
	char firstName[MAX_SIZE];
	char lastName[MAX_SIZE];
	char middleName[MAX_SIZE];
	char combinedNames[MAX_SIZE];
	char comma[] = { ',' };
	char space[] = { ' ' };
	char nullTerminator[] = { '\n' };

	cout << "This program will ask for your first , last and middle names." << endl;
	cout << "Then it will properly arrange them as follows last, first and middle." << endl << endl;

	//get input from user
	cout << "Please enter your first name :";
	cin.getline(firstName, MAX_SIZE);
	cout << endl;

	cout << "Please enter your last name :";
	cin.getline(lastName, MAX_SIZE);
	cout << endl;

	cout << "Please enter your middle name :";
	cin.getline(middleName, MAX_SIZE);
	cout << endl;
	//combine all names
	strncpy_s(combinedNames, lastName,strlen(lastName));
	strncpy_s(combinedNames, comma,strlen(comma));
	strncpy_s(combinedNames, space,strlen(space));
	strncpy_s(combinedNames, firstName,strlen(firstName));
	strncpy_s(combinedNames, space,strlen(space));
	strncpy_s(combinedNames, middleName,strlen(middleName));
	strncpy_s(combinedNames, nullTerminator, strlen(nullTerminator));
	cout << combinedNames << endl;
	system("pause");
	return 0;
}
Q: Why do we have #include<string> in our code?
A: So that we can use std::string

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

int main()
{
	//Declare/initialize variables/arrays
	// const int MAX_SIZE = 100;

	// char firstName[MAX_SIZE];
	// char lastName[MAX_SIZE];
	// char middleName[MAX_SIZE];
	std::string firstName ;
	std::string lastName ;
	std::string middleName ;

	// char combinedNames[MAX_SIZE];

	// char comma[] = { ',' };
	// char space[] = { ' ' };
	// char nullTerminator[] = { '\n' };
        const char comma = ',' ;
        const char space = ' ' ;

	std::cout << "This program will ask for your first , last and middle names.\n" ; // << endl;
	std::cout << "Then it will properly arrange them as follows last, first and middle.\n\n" ; // << endl << endl;

	//get input from user
	std::cout << "Please enter your first name :";
	//cin.getline(firstName, MAX_SIZE);
	std::cin >> firstName ;

	//cout << endl;
	std::cout << "\nPlease enter your last name :";
	// cin.getline(lastName, MAX_SIZE);
	std::cin >> lastName ;
	// cout << endl;

	std::cout << "\nPlease enter your middle name :";
	// cin.getline(middleName, MAX_SIZE);
	std::cin >> middleName ;
	// cout << endl;

	//combine all names
	/*
	strncpy_s(combinedNames, lastName,strlen(lastName));
	strncpy_s(combinedNames, comma,strlen(comma));
	strncpy_s(combinedNames, space,strlen(space));
	strncpy_s(combinedNames, firstName,strlen(firstName));
	strncpy_s(combinedNames, space,strlen(space));
	strncpy_s(combinedNames, middleName,strlen(middleName));
	strncpy_s(combinedNames, nullTerminator, strlen(nullTerminator));
	*/

	const std::string combinedName = lastName + comma + space + firstName + space + middleName ;
	std::cout << '\n' << combinedName << '\n' ; // endl;

	// system("pause");
	// return 0;
}
And if using c-strings is a requirement:
1
2
3
//combine all names
snprintf(combinedNames, MAX_SIZE, "%s, %s %s", lastName, firstName, middleName);
std::cout << combinedNames << '\n';
Last edited on
example by looping
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
#include <iostream>
#include <cstring>

int main(){	
	char lastName[4] = "xyz";
	char middleName[5] = "mnop";
	char firstName[4] = "abc";			
	char fullName[13];
	
	int i,j,k;
	
	for(i=0; i<strlen(lastName); i++){
		fullName[i]= lastName[i];
	}
	
	fullName[i] = ' ';
	
	for(j=i+1, k=0;  k<strlen(firstName); j++, k++){	
		fullName[j] = firstName[k];		
	}	
	fullName[j]= ' ';
	
	for(i=j+1, k=0;  k<strlen(middleName); i++, k++){
		fullName[i]= middleName[k];
	}	
	fullName[i]= '\0';
	
	std::cout << fullName << "\n" << strlen(fullName) << "\n";
	

    return 0;
}
Topic archived. No new replies allowed.