Output last name of first name

I have to create a program where I ask the user for a full name, then find the last name and print it out using the members of <string>



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



using namespace std;

int main()
{
	
	
	char choice;
	int n;
	string st;

	
	do
	{

		cout << "Please enter your full name: " << endl;
		getline(cin, st);

		//find last name
		int n = st.find

		cout << st << ", your last name is: ";
	

	system("PAUSE");
	return 0;	
}



I'm not too sure on how to find the last name, should I try and find the character of a space then input the string after that?
I think yeah you'll need to find space character first and use this www.cplusplus.com/reference/string/string/substr/
To find character in string
www.cplusplus.com/reference/string/string/find_first_of/
Last edited on
I managed to get the program working, but when I pick Y to continue the program and reloop it, I get an
error box stating:

Debug Assertion Failed!
Expression: String subscript out of range!

Also, when I enter a name for example: Mike Jones, it will display the last name "Jones" correctly.

But if someone enters a middle name, for example, Mike Larry Jones, it will say the last name is "Larry Jones"

How do I correct 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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <iostream>
#include <string>
#include <array>
#include <algorithm>


using namespace std;

int main()
{
	
	
	char choice;
	string st;
	
	
	do
	{


		cout << "Please enter your full name: " << endl;
		getline(cin, st);

		int n = st.length();
		int q = st.find(' ');


		cout << st << ", your last name is: ";


		for (int i = q; i <= n; i++)
		{
			cout << st[i];
		}


		cout << endl;

		cout << "CONTINUE? (Y/N): " << endl;
		cin >> choice;

		choice = toupper(choice);

	} while (choice == 'Y');

	system("PAUSE");
	return 0;	
}
I heard something about don't mix cin with getline, using cin.ignore maybe able to fix it

Find another occurrence of space, string.begin to the first space is first name and last space to string.end is last name
Could you clarify on the last part?

Logically I have to find the last word before the return key and after the space, but how would I input this into code with using only the members of the string library?

1
2
3
4
5
6
7
8
9
string first,last,name;
size_t space[2];
space[0]=name.find(' ');
space[1]=name.find(' ',space[0]+1);
first=name.substr(0/*name.begin() doesn't work*/,space[0])
if(space[1]!=string::npos)
  last=name.substr(space[1],name.size())
else
  last=name.substr(space[0],name.size()/*end() doest work*/)

I don't know if its works I'm online from my phone right now
Last edited on
What about for the error report? When i pick y for choice, to continue the loop, the error message which I mentioned earlier keeps displaying.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
using namespace std;
int main()
{
    string s ="i have a big name";
    int i;
    for ( i = s.length()-1; s[i] != ' '; i-- ) ;
    
    i++;
    
    string lastName;
    for ( ; i<= s.length()-1; i++ )
    	lastName += s[i];
    	
    cout <<lastName;
	  
}
Sorry for really late answer ._.)'
I found something on the net
1
2
3
4
5
6
7
8
9
int main(){
int age = 0;
string name = "";
cout<<"Enter your age ";
cin>>age;
cout<<"Enter your name ";
getline(cin, name);
return 0;
}

If you run this program and press enter after
entering your age, then that's it. That '\n'
character will go and get saved as a string in the
variable name.


They said using cin.ignore might help

EDIT: sorry I forget to put the link https://answers.yahoo.com/question/index?qid=20091206224451AAkAIji
Last edited on
@anup30

That is a good method, I was thinking of how to construct the for loop and your input has helped very much!

I have figured out the program correctly, however a problem lies in my do-while loop.

Whenever I ask the user to pick Y/N to continue, if they pick Y then the program executes an error which breaks the compiler.

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 <string>
#include <array>
#include <algorithm>


using namespace std;

int main()
{
	
	
	char choice, c;
	string st;
	string LastName;
	
	do
	{


		cout << "Please enter your full name: " << endl;
		getline(cin, st);

		int n = st.length();
		int q = st.find(' ');
		int z = st.find(' ', q + 1);


		cout << st << ", your last name is: ";
		string LastName = st.substr(z, st.size());

		cout << LastName; 

		cout << endl;

		cout << "CONTINUE? (Y/N): " << endl;
		cin >> choice;

		c = toupper(choice);

	} while (c == 'Y');

	system("PAUSE");
	return 0;	
}


error:
Unhandled exception at 0x763F1D4D in (project destination)
exception: std::out_of_range at memory location 0x009AF6600


with option to break or continue the program

another error:

Debug assertion failed!
program: c:\ file destination
line: 1685

Expressoin: string subscript out of range


Abort Retry Ignore
Topic archived. No new replies allowed.