Question based on C - String. C++

Write a program that asks for the user‟s first, middle, and last names. The names should be stored in three different character arrays. The program should then store, in a fourth array, the name arranged in the following order: the last name followed by a comma and a space, followed by the first name and a space, followed by the middle name. Then display the name.

For example, if the user entered “Carol Lynn Smith”, the fourth array should contain “Smith, Carol Lynn”.

(I know this question has been posted before but I don't quite understand what the other person is doing as it still seems a little complex to me.)

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
#include <cstring>
#include <cctype>
#include <iostream>
using namespace std;
const int SIZE = 30;

char arrName(char [], char [], char []);

int main()
{
    //Variable declaration
    char firstName[SIZE], middleName[SIZE], lastName[SIZE], fullName[SIZE * 3];

    //Prompt user for his/her first, middle and last name
    cout << "Please enter your first name here:" << endl;
    cin >> firstName;

    cout << "Please enter your middle name here:" << endl;
    cin >> middleName;

    cout << "Please enter your last name here:" << endl;
    cin >> lastName;

    fullName[SIZE * 3] = arrName(firstName, middleName, lastName);

    //Display full name
    for (int i = 0; i < (SIZE * 3); i++)
    {
        cout << "Your full name is: " << fullName[i];
    }


    return 0;
}

char arrName(char firstName[SIZE], char middleName[SIZE], char lastName[SIZE])
{
    char fullName[SIZE * 3];
    
    for (int i = 0; i < (SIZE * 3); i++)
    {
        ( I don't know how to continue my program.)
    }

    return fullName[SIZE * 3];
} 
Last edited on
Helen,

The simplest way is probably just to concatenate your character strings (e.g.
"Smith" + ", " + "Carol" + " " + "Lynn")

If you are using Cstrings (which is what you are doing) then see
http://www.cplusplus.com/reference/cstring/strcat/


If you insist on looping as you are doing in arrName then you will need multiple do-while or while loops. Each will add characters from one of your three name arrays to the fourth array until they hit the null character ending each string. If you don't understand that I suggest you use the strcat() function above.
Last edited on
That's just an example though. The name in the string is not a constant. I also have to create a new array to contain all 3 of my character string arrays.
1
2
3
4
for (int i = 0; i < (SIZE * 3); i++)
{
    ( I don't know how to continue my program.)
} 

Maybe use a nested for loop?
1
2
3
4
5
for( int i = 0; i < 3; i++ ) {
    for( int j = 0; j < 30; j++ ) {

    }
}
Yeah I thought of that too but what should be inside the nested for loop?
Use gets() to input the character arrays from the user. Then use strcat to append them one by, with accompanying commas and space, to the 4th array that you have called fullName.

It matters not one iota whether they are constants or not.

You don't need a separate function to do this, but if you insist then it will have to return more than the one character of your example.
1
2
3
4
5
6
   strcpy( fullName, lastName   );
   strcat( fullName, ", "       );
   strcat( fullName, firstName  );
   strcat( fullName, " "        );
   strcat( fullName, secondName );
   cout << "Full name is " << fullName << endl;



Have you been told that you need to use loops? If so then you would have to use do-while or while loops (since your names would have differing numbers of characters) and you would have to test for the null character that ended your c-string.


I am still making the assumption (from the original question) that you are obliged to use c-strings (aka character arrays, or null-terminated strings). If you were allowed to use the string class itself then you would just have (with straightforward + replacing a sequence of strcat calls):
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;

#define SIZE 30

int main()
{
   string firstName, secondName, lastName, fullName;
   cout << "Input first name: ";
   cin >> firstName;
   cout << "Input second name: ";
   cin >> secondName;
   cout << "Input surname: ";
   cin >> lastName;
   fullName = lastName + ", " + firstName + " " + secondName;
   cout << "Full name is " << fullName << endl;
}

Last edited on
Unfortunately I am not allowed to use the string class and only allowed to use the cstring because our lecturer has not touched on the string class yet.

And no my lecturer did not specify on using loops, just have to complete the program according to what the question says.
And no my lecturer did not specify on using loops, just have to complete the program according to what the question says.

Then why are you using loops in your code??
1
2
3
4
for (int i = 0; i < (SIZE * 3); i++)
{
        ( I don't know how to continue my program.)
} 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
char firstName[100], middleName[100], lastName[100];

cout << "Input first name : "; cin >> firstName;
cout << "Input middle name : "; cin >> middleName;
cout << "Input last name : "; cin >> lastName;

char resultName[300];
strcpy(resultName, lastName);
strcat(resultName, ", ");
strcat(resultName, firstName);
strcat(resultName, " ");
strcat(resultName, middleName);

cout << "The result name : " << resultName << endl;


Edit : Added a line. Now the code is perfect.
Last edited on
Saw some examples here and there, so I thought it was a good idea to implement it here. But anyway, thanks for all the help and tips @SakurasouBusters and @lastchance.
You can also do this :
1
2
3
4
5
6
7
8
9
10
char firstName[100], middleName[100], lastName[100];

cout << "Input first name : "; cin >> firstName;
cout << "Input middle name : "; cin >> middleName;
cout << "Input last name : "; cin >> lastName;

char resultName[300];
strcat(strcat(strcat(strcat(strcpy(resultName, lastName), ", "), firstName), " "), middleName);

cout << "The result name : " << resultName << endl;
Seems a little bit messy, the first solution you gave seems better.
Edit : Added a line. Now the code is perfect.

Not really. You have possible buffer overflows waiting to happen.

When dealing with C-strings you should never use a method that retrieves the C-string that doesn't limit the number of characters it will try to retrieve. To limit the extraction operator>> you need to use the setw() manipulator before the C-string: cin >> setw(100) >> firstName;. And you really should consider istream.getline() because it enforces using the limiter, and it will retrieve names that happen to have spaces within them.

You should also avoid putting multiple statements on the same line.

Topic archived. No new replies allowed.