Reading name

Not outputting right, the programs supposed to let you enter a full name then tell you the last name and then ask if you want to continue(y/n)?

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

using namespace std;

int main()
{
	int space=0, length=0;
	string name;
	string last(name, space+1, length);
    char response;

do{
    cout << "Enter your full name: ";
    getline(cin, name); // get their full name, including spaces.
    // display last name
    cout << "\t" << name << ", your last name is " << last; 
    getline(cin, last, ',');

	cout << "CONTINUE(y/n)? ";
	cin >> response;
	cin.ignore(80, '\n');
	} while(response == 'Y' || response=='y') ;
  
 
	system ("pause");
	return 0; 
}


Also did it like this I'm not understanding how to get last name though.
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
#include <iostream> 
#include <iomanip> 
#include <string>

using namespace std;

int main()
{
	string name;
	string last;
    char response;

do{
    cout << "Enter your full name: ";
    getline(cin, name); // get their full name, including spaces.
    // display last name
    cout << "\t" << name << ", your last name is " << last << endl; 

	cout << "CONTINUE(y/n)? ";
	cin >> response;
	cin.ignore(10, '\n');
	} while(response == 'Y' || response=='y') ;
  
 
	system ("pause");
	return 0; 
}




1
2
3
4
5
6
7
Enter your full name: Bill Clinton
        Bill Clinton, your last name is
CONTINUE(y/n)? y
Enter your full name: Bill Clinton
        Bill Clinton, your last name is
CONTINUE(y/n)?


Any solutions??
Last edited on
I did it like this and it somewhat works but I have problem with the middle name such as Barrack Hussein Obama. How do I make it so it ignores the middle name and just gives me the last name???

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

using namespace std;

int main()
{
	string name;
	string first_name, last_name;
    char response;

do{
    cout << "Enter your full name: ";
    cin >> first_name; cin >> last_name; // get their full name, including spaces.
    // display last name
    cout << "\t" <<first_name << " "<< last_name << ", your last name is " << last_name << endl; 

	cout << "CONTINUE(y/n)? ";
	cin >> response;
	cin.ignore(50, '\n');
	} while(response == 'Y' || response=='y') ;
  
 
	system ("pause");
	return 0; 
}
1) Read the whole name as a complete string
2) Count the number of words in the string
3) Output only the first and last words in the string
you mean just getline(cin, name); that ?
Also what do you mean by counting the # of words?
like this: string Name = first_name + " " + last_name; ?
need last name only
????????
I hand you a red ball and a green ball. You only want the green ball. Why is there a problem?
Can't take both
just take 1.
Last edited on
for real? ?
Why don't you use ignore the thing you don't want? You know, as in not deal with it at all?
so do I put cin.ignore(name, ' ') ? what about the middle name too?? If it was Barrack Hussein Obama ?
@Dkob1

Here is your program, able to find last name, and display it.

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

using namespace std;

int main()
{
	int len, length;
	string name;
	string last="";
	char response;

	do{
		 
		name = "";
		last = "";
		len = 0;
		length = 0;
		cout << "Enter your full name: ";
		getline(cin, name); // Get their full name, including spaces.
		// display last name
		len = name.length(); // Get total length of inputted name
		
		for (int x = len; x > 0; x--) // Start from end of input and find first space
		{
			if (name[x] != ' ')
				length++;
			else
				x = 0; // end loop
		}
		for (int x = len - length; x < len; x++)// Start where last name begins
			last += name[x]; // Assign last to name ending, one letter at a time
		
		cout << "\t" << name << ", your last name is " << last << endl;
		
		cout << "CONTINUE(y/n)? ";
		cin >> response;
		cin.ignore(20, '\n');
		
	} while (response == 'Y' || response == 'y');

	system("pause");
	return 0;
}
Ok thanks very much got very frustrated trying to do it.
Topic archived. No new replies allowed.