How do I get the rest of the string in my code?

Write your question here.
My code successfully gets user input for a first input of full name, then gets user input for a new last name no matter how many spaces and is supposed to output the user's first name followed by the entire new last name. Everything works except it doesn't capture the first word of the new last name. I've tried removing the first cin and just using getline, but that breaks everything.

I'm so close to finishing this program, but I'm stuck on this problem.
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
  #include <iostream>
#include <sstream>
using namespace std;

string Getfirstname(void)
{
    string full_name, old_last_name;
    cout << "Enter full name: ";
    cin >> full_name >> old_last_name;
    
    return full_name;
}

string GetNewLastName(void)
{
    string new_last_name;
    cout << "Enter new last name: ";
    cin >> new_last_name;
    getline (cin,new_last_name);
    return new_last_name ;
}

int main()
{
    string full_name = Getfirstname();
    string new_last_name = GetNewLastName();
    
    cout << full_name << new_last_name << endl;
    
    
    
   
   return 0;
}
What is the reason you're using both cin and getline to get a name? Just simply remove the getline, the cin is enough.

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
#include <iostream>
#include <sstream>
#include <string> // include string...
using namespace std;

string Getfirstname()
{
    string full_name, old_last_name;
    cout << "Enter full name: ";
    cin >> full_name >> old_last_name;
    
    return full_name;
}

string GetNewLastName()
{
    string new_last_name;
    cout << "Enter new last name: ";
    cin >> new_last_name;
    
    return new_last_name ;
}

int main()
{
    string full_name = Getfirstname();
    string new_last_name = GetNewLastName();
    
    cout << full_name << " " <<new_last_name << endl; // added space between names

   return 0;
}

Topic archived. No new replies allowed.