string class problem

Hey guys, sorry if this is a really really noob question, but i can't find any help in my textbook or online. I've searched everything I can and I just cant make sense of it, even though I feel like it's something very simple. I'm in a programming 1 class right now with only maybe 10 weeks of practice under my belt, FYI.

So, I have this extra credit and it's based on string class functions. It asks to:
1. Write a program that reads a person’s first and last name without any spaces, but with the * symbol between the 2 names.
2. In the main function, use string class functions to extract the first name from the string and store it in a second string object.
3. Then extract the last name and store it in a third string object.
4. Test your program to make sure it works correctly by displaying all three strings.

I understand how to use the functions insert, assign, etc., but I am lost as to how to know the position of the '*'. I know I can use the find function (wholeName.find('*')) to find the asterisk, but how do I know the position of it? This is what I have so far:

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

using namespace std;

int main ()
{
	string wholeName;
	string firstName;
	string lastName;
	char index;

	cout << "Please enter your whole name, with a '*' symbol instead of a space: ";
	cin >> wholeName;

	wholeName.find(index); 
	firstName.assign(wholeName, 0, index);


	cout << firstName;


	cout << "Please press enter once or twice to continue...";
	cin.ignore().get();    			// hold console window open
	return EXIT_SUCCESS;           	// successful termination
}


Again, I apologize if this seems really stupid, I just can't wrap my head around it. If someone would like to actually code the program, just so i can see how it's done and not to cheat, that would also work. Any help is GREATLY appreciated. Thank you.
The find method returns an unsigned int that coresponds to the character found. For example, if I entered Sam*Smith the find function would return 3. This number can then be used as an argument to the substr function to extract the names.
Last edited on
Thanks for the reply. But how would I know the position would be 3? I want to get the position of the *, and then assign whatever is before it to another string and whatever is after it to another string. I understand that it would return position 3, but how am I supposed to know that if everyone's name is a different length? Wouldn't it be at a different position depending on what name was entered? I feel like I can't explain what's going on in my head so I apologize if I'm confusing.
http://www.cplusplus.com/reference/string/string/find/
1
2
3
4
5
6
7
8
9
10
size_t indexOfStar = wholeName.find('*');

if (indexOfStar == string::npos)
{
    cout << "Didn't find a star." << endl;
}
else
{
    cout << "Found star at index: " << indexOfStar << endl;
}
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>
using namespace std;

int main()
{
	string wholeName;
	string firstName;
	string lastName;
	size_t starIndex;

	cout << "Please enter your first and last name separated by a *: ";
	getline(cin, wholeName);

	starIndex = wholeName.find('*');

	if (starIndex != std::string::npos)
	{

		firstName = wholeName.substr(0, starIndex);
		lastName = wholeName.substr(starIndex + 1);
		cout << "First name is: " << firstName << endl;
		cout << "Last Name is: " << lastName << endl;
	}
	else
	{
		cerr << "Please separate your name with a *";
	}

	cin.ignore();
	return 0;
}
Topic archived. No new replies allowed.