Why doesn't my program cout the first word of the new last name?

Feb 27, 2016 at 11:03pm
Write your question here.
My program is supposed to print out any number of "new last names" separated by spaces. It prints out every single last name separated by a space except for the first one. I've tried using getline, but that makes it impossible to enter the last name. And if I just use cin I can only get the first last name and none of the others after the space. I'm going to post both code versions and say why they're wrong/don't work.
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// this code produces every last name separated by a space except for the first // name
#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;
}

// this code only produces the first 'new last name' but not the rest after a    // space
#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;
}


Feb 27, 2016 at 11:21pm
Can you give an example of your desired output?
Feb 27, 2016 at 11:28pm
My desired output would be
Enter full name: John Doe
Enter new last name: Von Baron Duke
Your new name is : John Von Baron Duke
Topic archived. No new replies allowed.