How to use strcpy() and strcat() functions to create a full name?

Hello,

I need to use the strcpy() and strcat() functions to assign a full name to the variable fullName. For example, if firstName holds Tom, and lastName holds Lee, then after using these functions, fullName should hold Tom Lee (with a space between the names). It needs to be inside the main (no functions are used). How would I go about doing this?

Also I'm getting an error inside strlen(friendsName) that says: "No suitable conversion function from string to const char* exists."
How do I get around that? I don't even know what that means.

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
#include<iostream>
#include<iomanip> 
#include <string> 

using namespace std;

int main()
{
	
	char firstAndMiddle[20] = "";
	char lastName[20] = "";
	char fullName[40] = "";
	string friendsName;
	int fullNamelength = strlen(fullName);
	int friendsNamelength = strlen(friendsName);

	cout << "Enter your first and middle names: ";
	cin.getline(firstAndMiddle, 20);

	cout << "Enter your last name: ";
	cin.getline(lastName, 20);

	cout << "How is your love life" << fullName << "?" << endl;
	cout << "By the way, your full name has" << fullNamelength << "characters." << endl;

	//strcpy() & strcat() go here

	cout << "Enter your friend's full name: ";
	getline(cin, friendsName);
	
	cout << "How is" << friendsName << "love life" << fullName << "?" <<endl;

	cout << "By the way, your friend's full name has" << friendsNamelength << "characters." << endl;

}
Last edited on
What's stopping you making this instead.
1
2
3
4
5
6
7
8
9
10
string firstAndMiddle;
string lastName;
string fullName;
cout << "Enter your first and middle names: ";
getline(cin,firstAndMiddle);

cout << "Enter your last name: ";
getline(cin,lastName);

fullName = firstAndMiddle + string(" ") + lastName;
Because we are supposed to use cin.getline for cstrings.
@elsa

If you can avoid char[] arrays then please do as salem suggests.

However, if you cannot use std::string for your assignment, then:

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
int main()
{
  char fullName      [ 100 ];  // enough chars to hold all parts of the name plus a space
  char firstAndMiddle[  50 ];  // enough for a first and middle name
  char lastName      [  50 ];  // enough for a last name

  cout << "Enter your first and middle names: ";
  cin.getline( firstAndMiddle, sizeof(firstAndMiddle) );

  cout << "Enter your last name: ";
  cin.getline( lastName, sizeof(lastName) );

  // strcpy() simply copies a char array until it hits (and copies) a zero value (null, or '\0')
  // It does not care what is in the target array -- it just writes over it.
  // You must make sure that the target array is big enough to hold the source data.
  strcpy( fullName, firstAndMiddle );

  // strcat() is similar, but it first finds the end of the target array (finds the zero),
  // then starts copying from where the zero is. 
  // Again, you must make sure that there is enough room left in the target array to
  // hold both its current content and the additional data in the source.
  strcat( fullName, " " );       // (first we will add that space between the names)
  strcat( fullName, lastName );  // (then we will add the last name)

  cout << fullName << "\n";
}

Using char[] arrays is tricky on a number of levels.

The first and most important is to make sure you have enough space to copy things around, and to keep your strings null-terminated, for which there must also be space. So, if you say
    char s[ 100 ]

then you have a string big enough to hold a maximum of 99 characters + 1 null terminator (zero). An empty string would have
    s[0] = '\0';.

In the example above, you have two arrays holding up to 49 characters each. 49 + 49 is 98. Plus a space = 99. The combined string needs to be at least 100 elements long (99 characters + 1 null terminator).


Another useful thing, which I did in the code above, is use the sizeof operator to help track the number of elements in the array. It only works on ARRAYS. Not pointers!

What this means that I only need to state the array's size once, where I declare it:
1
2
  char s[ 100 ];
  cout << "s has " << sizeof(s) << " characters.\n";

But I cannot use it the same way with a char*:
1
2
3
4
5
6
7
8
9
10
11
12
13
  char* s;

  int n = 100;
  s = malloc( n );

  cout << "s is a pointer. The pointer's size is " << sizeof(s) << " bytes.\n";
  cout << "I do not know how many characters s can hold.\n";

  cout << "(Unless I keep track. The number I used with malloc() is " << n << ",\n";
  cout << "so s has " << n << " characters.\n";

  // Don't forget me!
  free( s );

It also doesn't work with arrays of things larger than one byte wide:
1
2
3
4
5
6
  int xs[ 100 ];

  cout << "xs is " << sizeof(xs) << " bytes long.\n";

  // We need an additional trick to determine the number of elements:
  cout << "xs has " << (sizeof(xs) / sizeof(x[0])) << " elements.\n";

The same caveat as above applies with malloc()ed arrays of stuff:
1
2
3
4
5
6
7
8
9
10
11
  int* xs;

  int n = 100;
  xs = malloc( n * sizeof(int) );

  cout << "*xs has n elements (n = " << n << ").\n";
  cout << "which means that *xs is " << (n * sizeof(xs[0])) << " bytes long.\n";
  cout << "xs itself (a pointer) is " << sizeof(xs) << " bytes.\n";

  ...
  free( xs );

Hope this helps.
Last edited on
It does help - thank you very much. Though I must say, we have hard limits on the char size. So 20 20 40 was a limit not a personal decision. Also, did you happen to know a solution to the "no suitable conversion function from string to const char"? Any help would be appreciated. Thanks.

- Elsa
Oh, sorry, forgot about that part of your question.

Yes, don’t use it with std::string.
strlen() is meant to be used with char[] arrays (C-strings).

To get the length of a std::string, use the member function:
friendsName.length().

I recommend you avoid mixing them, because the concepts for using one aren’t quite the same as the concepts for using the other. The point of std::string is to do all the extra stuff you have to do with c-strings for you, so you don’t have to.

Hope this helps.
closed account (E0p9LyTq)
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
// to stop Visual Studio 2017 from whinging about std::strcpy & std::strcat
#define _CRT_SECURE_NO_WARNINGS

#include <cstring>
#include <iostream>
#include <string>

int main()
{
   char firstAndMiddle[20] = "";
   char lastName[20]       = "";

   std::cout << "Enter your first and middle names: ";
   std::cin.getline(firstAndMiddle, 20);

   char fullName[40] = "";

   std::strcpy(fullName, firstAndMiddle);

   std::strcat(fullName, " ");

   std::cout << "\nEnter your last name: ";
   std::cin.getline(lastName, 20);

   std::strcat(fullName, lastName);

   int fullNamelength = strlen(fullName);

   std::cout << "\nHow is your love life, " << fullName << "?\n";
   std::cout << "By the way, your full name has " << fullNamelength << " characters.\n";

   std::string friendsFirstName;

   std::cout << "\nEnter your friend's first and middle name: ";
   std::getline(std::cin, friendsFirstName);

   std::cout << "\nEnter your friend's last name: ";
   std::string friendsLastName;
   std::getline(std::cin, friendsLastName);

   std::string friendsFullName = friendsFirstName + " " + friendsLastName;

   std::cout << "\nHow is " << friendsFullName << "'s love life, " << fullName << "?\n";

   std::cout << "By the way, your friend's full name has " << friendsFullName.size() << " characters.\n";
}

Enter your first and middle names: Joe D.

Enter your last name: Ragman

How is your love life, Joe D. Ragman?
By the way, your full name has 13 characters.

Enter your friend's first and middle name: Fred G.

Enter your friend's last name: Middleton

How is Fred G. Middleton's love life, Joe D. Ragman?
By the way, your friend's full name has 17 characters.

If you don't use Visual Studio 2017 you don't need the #define.

BTW, I would seriously suggest not using a lower case friend string of characters as part of a variable name. friend is a C++ keyword.
Last edited on
FurryGuy wrote:
BTW, I would seriously suggest not using a lower case friend string of characters as part of a variable name. friend is a C++ keyword.

Disagree.

friend” and “friendsName” are absolutely not the same token, and there is no likelihood of mixing them.

I do consider it unfortunate that both “friend” and “default” are keywords in C++.
I recommend you avoid mixing them


you cannot safely mix unless you know both systems really well. string objects behave oddly if you do anything that puts a zero char within its length() range, and do not function if you attempt to change its size (strcat etc is risky). grabbing &stringvar[0] as a char* is a good way to crash or break something, and c_str() is constant for this very reason (to avoid damage to the object).

There isn't much point. string can do anything cstrings can, and mixing them is risky at best, and convoluted / hard to follow and maintain at best. (Says the guy who was using strstr earlier...). It was an exception to the rule, don't worry about such things until you have at least mastered string fully, then you can revisit edge cases.
Last edited on
Topic archived. No new replies allowed.