Functions using different programs to compile

Hello,
I am trying to use a string to convert a person's name to be in this format last name followed by a comma followed by the rest of the name. "Last name" is determined to be everything after a space. This is what I have so far. I would greatly appreciate any help.

string lastNameFirst(string name)
{
string firstname;
string lastname;
for (int i = 0; i < name.length(); i++)
{
if (name[i] !== '')
word
}

return firstname + "," + lastname;
}
cout << endl;
testLastNameFirst("Blake Shelton"); (should return Shelton, Blake)
testLastNameFirst("Hillary Rodham Clinton"); (should return "Clinton, Hillary Rodham")
testLastNameFirst("Barack Hussein Obama II"); (should return "II, Barack Hussein Obama")
testLastNameFirst("Beyonce"); (should return "Beyonce")
Hello :)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
string lastNameFirst(string name)
{
string firstname;
string lastname;
for (int i = 0; i < name.length(); i++)
{
if (name[i] !== '')
word	
}

return firstname + "," + lastname; 
}
cout << endl; 
testLastNameFirst("Blake Shelton"); (should return Shelton, Blake) 
testLastNameFirst("Hillary Rodham Clinton"); (should return "Clinton, Hillary Rodham")
testLastNameFirst("Barack Hussein Obama II"); (should return "II, Barack Hussein Obama")
testLastNameFirst("Beyonce"); (should return "Beyonce")


add code blocks to improve legibility
Last edited on
Hello. Yes that is what I have. I was wondering how to make it output those names like it says in the parentheses. If you would help. I would greatly appreciate it.
try this:

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
#include <iostream>
#include <string>

using namespace std;

string lastName()
{

}

int main()
{
    while(true) {
    string first;
    string last;
    cout << "type zero to quit" << endl;
   cout << "first name:";
   cin >> first;

   if (first == "0") { break; }

   cout << "last name:";
   cin >> last;

   if (last == "0") { break; }

   cout << endl << endl << last << "," << first << endl << endl;

    }
   return 0;
}


as you can see, I fit everything into the main function. Unless you are using this to make a much bigger program or put the result as a string, there is no need for a separate function. You can also remove the "cin's" if you yourself want to add the name in. PM me if you need more help
Last edited on
1
2
3
if (name[i] !== '')
word	
}


What is "word"?

EDIT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
string firstname;
string lastname;
string fullname = NULL;
string lastFirstName(string first, string last)
{
fullname = lastname + ", " + firstname;
return fullname;
}

int main() 
{
lastFirstName(Test, TEST);
return 0;
}
Last edited on
Topic archived. No new replies allowed.