last name switch

hello, Im just trying to do a simple switch of last name using a string function.
I can simply put the function in the main and it will come out good, but when I'm trying to make in a function on its own. The function can only return the first name. Any help? this is my code below
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
 #include <iostream>
#include <string>
#include <sstream>
using namespace std;
string swapLastName(string o_n,string n_l_n);
int main()
{
cout << "Type your full name: ";
 string original_name;
 getline(cin, original_name);
 cout << "Enter your new last name: ";
 string new_last_name;
 getline(cin,new_last_name);
  cout << "Your new name: " << swapLastName(original_name, new_last_name);
 return 0;
}
string swapLastName(string o_n,string n_l_n){
string a[100];
string b[100];
int i = 0;
stringstream ssin(o_n);
stringstream ssin2(n_l_n);
while(ssin.good() && i <100)
{
    ssin >> a[i];
    ssin2 >> b[i];
    i++;
}
for (i = 0; i < 100; i++)
{
    a[i+1] = b[0+i];
}
for (i = 0; i < 100; i++)
{
   return a[i];
}
}
A function can only return one value but there is no reason you can't have 2 functions.
this takes into a/c (multiple) middle names as well though if there are multiple last names only the last of these last names will be switched:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>

std::string swapLastName(const std::string o_n, const std::string n_l_n)
{
    auto found = o_n.find_last_of(" ");
    return o_n.substr(0, found) + std::string (" ") + n_l_n;
}
int main()
{
    std::cout << "Type your full name: ";
    std::string original_name;
    getline(std::cin, original_name);
    std::cout << "Enter your new last name: ";
    std::string new_last_name;
    getline(std::cin, new_last_name);
    std::cout << "Your new name: " << swapLastName(original_name, new_last_name);
}
hi gunner, is there any way to keep the spaces of the new name to one single space if the user input a name with a lot of spaces between them since with your code if there are a lot of spaces between the original name then it will read for that many spaces.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
using namespace std;


string newName(string o_n, string n_l_n){

	// Set the substring until the white space as the first name
	string new_name = o_n.substr(0, o_n.find(" "));
	// Add the new last name
	new_name += " " + n_l_n;
	return new_name;
}
int main(){

	string original_name, new_last_name;
	cout << "Enter full name\n";
	getline(cin, original_name);
	cout << "Enter new last name\n";
	getline(cin, new_last_name);
	cout << newName(original_name, new_last_name) << '\n';
}
is there any way to keep the spaces of the new name to one single space
i think i see what you mean - you want to leave open the possibility that the new last name might be, for e.g. "parker bowles" etc but yet not let the user enter a whole paragraph! in that case frame the new_last_name entry around a do ... while loop:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <iostream>
#include <string>
#include <algorithm>

std::string swapLastName(const std::string o_n, const std::string n_l_n)
{
    auto found = o_n.find_last_of(" ");
    return o_n.substr(0, found) + std::string (" ") + n_l_n;
}
int main()
{
    std::cout << "Type your full name: ";
    std::string original_name;
    getline(std::cin, original_name);
    std::string new_last_name;
    do
    {
        std::cout << "Enter your new last name: ";
        getline(std::cin, new_last_name);
    }   while ((std::count(new_last_name.begin(), new_last_name.end(), ' ') > 1));
    std::cout << "Your new name: " << swapLastName(original_name, new_last_name);
}


Last edited on
Topic archived. No new replies allowed.