I'm trying to strcpy two cstring arrays into one. When I output my name it gives me this "╠".

//final review
//====================================================

#include <iostream>
#include <cstring>
using namespace std;

//=====================================================

void displayInfo(char fullName[20], int & age);

int main()
{
int age;

char firstName[10] = "Allen";
char lastName[10];
char fullName[20];

cout << "Enter your age: " << endl;
cin >> age;
cout << "Enter your last name: " << endl;
cin.getline(lastName, 10);
cin.ignore();

strcpy(fullName, firstName); //<-----I'm pretty sure this is where my mistake is.
strcpy(fullName, " "); //
strcpy(fullName, lastName);

displayInfo(fullName, age);

return 0;
}

//====================================================

void displayInfo(char fullName[20], int & age)
{
cout << "Hello " << fullName[20] << ". ";
cout << "You are " << age << " years old. " << endl;
}

/*output:
Enter your age:
27
Enter your last name:
McGowan
Hello ╠. You are 27 years old.
Press any key to continue . . .*/



I think what you want is to append not copy. I'm not sure, but if I remember correctly copy will overwrite any previous value. You could use simply concatenation I believe, like this:

fullName = firstName + " " + lastName;

The reason you are getting the funky output for name though is that you are using fullname[20] as your output. Since you fullName array is declared as having size 20, it has elements from 0 to 19. 20 is outside the array. Also, remember that the array is an array of characters, so trying to output a single element of the array will only display a single character. To output the entire array simply use fullName.

Edit: Actually, <cstring> has a function called strcat that should work for you too.

http://www.cplusplus.com/reference/cstring/strcat/

All you have to do is pass the name of the destination array, then the name of the string to be added. For you that call would look like:

1
2
3
strcat(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, lastName);
Last edited on
Thank you so much:)
Last edited on
Note that if you strcat, you must make sure the destination string (fullName) is initialized or you may overstep array bounds.

I recommend first doing a strcpy, followed by strcats:

1
2
3
strcpy(fullName, firstName);  // <- strcpy first
strcat(fullName, " ");  // <- then strcat
strcat(fullName, lastName);



Of course this is much much easier and safer if you just use strings instead of char arrays... but this looks like a homework problem and I'm assuming you're not allowed to use strings.
I used this code and it crashed after inputting the data, I changed what you told me to, maybe I put it in the wrong order:/
//final review
//====================================================

#include <iostream>
#include <string>
#include <cstring>
using namespace std;

//=====================================================

void displayInfo(char fullName[20], int & age);

int main()
{
int age;

char firstName[10] = "Allen";
char lastName[10];
char fullName[20];

cout << "Enter your age: " << endl;
cin >> age;
cout << "Enter your last name: " << endl;
cin.getline(lastName, 10);
cin.ignore();

strcat(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, lastName);

displayInfo(fullName, age);

return 0;
}

//====================================================

void displayInfo(char fullName[20], int & age)
{
cout << "Hello " << fullName << ". ";
cout << "You are " << age << " years old. " << endl;
}

When I used strcpy the program ran but it output nothing, and this was after changing fullName in the displayInfo function.



Last edited on
Ah yup, Disch was right. Destination string should be initialized before you pass it to strcat. His method works well to using copy then cat.
Last edited on
strcpy(fullName, firstName);
strcat(fullName, " ");
strcat(fullName, lastName);

This only gives me the first name, I've tried several variations but I can't get the last name to output. The space outputs as well, but not the lastName array.
Last edited on
Works for me:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

#include <iostream>
#include <cstring>
using namespace std;

int main()
{
    char firstname[] = "John";
    char lastname[] = "Doe";
    char fullname[20];

    strcpy(fullname,firstname);
    strcat(fullname," ");
    strcat(fullname,lastname);

    cout << fullname; // outputs "John Doe" as expected
}


Make sure your 'lastName' array actually contains a name and isn't empty.


EDIT: ah, yes... that's the culprit:

1
2
3
4
cin >> age;  // using the >> operator leaves the newline in the buffer
cout << "Enter your last name: " << endl;
cin.getline(lastName, 10);  // so this returns immediately with an empty string
cin.ignore();


This is one of the many reasons I hate using iostream. It's super confusing and filled with these kinds of gotchas. I still don't fully understand how it all works. Anyway, you might be able to fix this by moving the ignore() call so it's ABOVE the getline(lastName call.
Last edited on
SUCCESS!! That was the problem sir:) Thanks a million!
Topic archived. No new replies allowed.