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.)
#include <cstring>
#include <cctype>
#include <iostream>
usingnamespace std;
constint 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];
}
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.
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.
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.
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):
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.
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.
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.