stuck on this exercise

Write a program that reads a person's name in the following format.: first name, then middle name, or initial, and then last name. The program then outputs the name in the following format:

last name, first name, middle initial

ex)
input:Mary Average User
output: User, Mary A.

or
input: Mary A. User
output: User, Mary A.


I'm only supposed to use cstring. I'm confused on the changing part.
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
#include <iostream>
#include <cstring>

using namespace std;

void input(char first_name, char middle_name, char last_name);
void output(char first_name, char middle_name, char last_name);
void change(char middle_name, char last_name);

int main()
{
    char first_name;
    char middle_name;
    char last_name;

    input(first_name, middle_name, last_name);
    change(middle_name, last_name);
    output(first_name, middle_name, last_name);
}

void input(char first_name, char middle_name, char last_name)
{
    cout << "Enter the name using the following order. (First name, middle name, last name):" << endl;
    cin >> first_name >> middle_name >> last_name;
}

void output(char first_name, char middle_name, char last_name)
{
    cout << "Here is the formatted name of the name you chose." << endl;
    cout << last_name << " " << first_name << " " << middle_name << endl;
}

void change(char middle_name, char last_name)
{
    if(last_name == " ")
    {
        last_name = middle_name + ',';
        middle_name = " ";
    }
    else
    {
        last_name = last_name + ',';
        middle_name = middle_name.strlen(0,1) + ".";
    }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
using namespace std;

int main(){

string lastname;
string firstname;
string middle;

cout << "Enter the persons first name: ";
getline(cin, firstname);
cout << "Enter the persons middle name, or initial: ";
getline(cin, middle);
cout << "Enter the persons last name: ";
getline(cin, lastname);

cout << "You have entered: "<<lastname<<", "<<firstname<<" "<<middle;

    return 0;
}


What you need may be more complicated than that because that seems very simple compared to what you have wrote, or if it's a template. Either way, I wrote this up in case it help.
I can't use <string>. I have to use <cstring>.
1
2
3
    char first_name;
    char middle_name;
    char last_name;

Each of these variables is a single character (just one letter). But as you said above, "I have to use <cstring>".
So those declarations become something like:
1
2
3
    char first_name[20];
    char middle_name[20];
    char last_name[20];

and throughout your code you need to change the function definitions to pass an array rather than a single character.
see Arrays as parameters in the tutorial page, http://www.cplusplus.com/doc/tutorial/arrays/
You may also find the next tutorial page on "Character Sequences" useful,
http://www.cplusplus.com/doc/tutorial/ntcs/
Topic archived. No new replies allowed.