0

I have a question asking me to prompt a user for first and last names. Declare a third array to hold last and first name separated by a comma and a space. Use loops to iterate through the names one character at a time, storing them into the full name array.

It sound easy enough, but I'm having so much trouble with it. I've tried multiple ways to do it, even ways the question doesn't ask for, like strncpy or memcpy or strncat. Maybe I'm just not understanding correctly.

This is what I currently have:

char FirstName[15];
char LastName[15];
char FirstLast[30];

cout << "Enter your first name: ";
cin >> FirstName;
cout << "Enter your last name: ";
cin >> LastName;


for (int i = 0; i<30; i++)
{
    if (i < 15) {
        FirstLast[i] = LastName[i];
    }
    else {
        FirstLast[i] = FirstName[i - 5];
    }
}


cout << FirstLast;

Thanks for any help.

MRB
  • 3,752
  • 4
  • 30
  • 44
Pownguin
  • 3
  • 1
  • 3
    Why are you using plain arrays? Appending would be very easy with [`std::string`](http://en.cppreference.com/w/cpp/string/basic_string) (and you wouldn't have to worry about buffer overflows). – Some programmer dude Jan 24 '17 at 08:10
  • 1
    As for the code you show, there are two problems: The first is `FirstName[i - 5]`. I assume you mean `15` instead of `5`? The second problem is that you forget that C-style strings are *terminated*. You neither check for the terminator, or add it to the destination. – Some programmer dude Jan 24 '17 at 08:12
  • 2
    And lastly, to solve your problem *with* arrays, read about [`std::strcpy`](http://en.cppreference.com/w/cpp/string/byte/strcpy) and [`std::strcat`](http://en.cppreference.com/w/cpp/string/byte/strcat). – Some programmer dude Jan 24 '17 at 08:13

3 Answers3

0

Use std::string

string FirstName, LastName, FirstLast;

cout << "Enter your first name: ";
cin >> FirstName;
cout << "Enter your last name: ";
cin >> LastName;

FirstLast = LastName + ", " + FirstName;

cout << FirstLast;
Shreevardhan
  • 12,233
  • 3
  • 36
  • 50
0
for (int i = 0; i<30; i++)
{
    if (i < 15) {
        FirstLast[i] = LastName[i];  
        //if LastName is less than 15 characters then garbage and probably '\0' will be appended to FirstLast and this will mark end of FirstLast string.
    }
    //else {
    //  FirstLast[i] = FirstName[i - 5];
    //}
}

Correct way would be:

    int i = 0, j = 0;
    while(LastName[j])
    {
        FirstLast[i] = LastName[j];     
        ++i;
        ++j;
    }
    FirstLast[i] = ',';
    ++i;
    FirstLast[i] = ' ';
    ++i;
    j = 0;
    while(FirstName[j])
    {
        FirstLast[i] = FirstName[j];    
        ++i;
        ++j;
    }
    FirstLast[i] = '\0';        

Ofcourse, LengthOf(FirstLast) should be atleast LengthOf(FirstName) + LengthOf(LastName) + 2(for comma and space) + 1 (null terminating character)

sameerkn
  • 2,209
  • 1
  • 12
  • 13
0

One possible answer without using any of the library functions would be:

// copy last name
int i = 0;
for (i = 0; LastName[i] != 0; ++i)
    FirstLast[i] = LastName[i];

// add , and space
FirstLast[i++] = ',';
FirstLast[i++] = ' ';

// append first name
for (int j = 0; FirstName[j] != 0; ++j)
    FirstLast[i++] = FirstName[j];

// Terminate the string
FirstLast[i] = 0;
mac
  • 81
  • 5