Name Format Assignment Trouble

In this assignment were supposed to take the first middle and last name that are input and format them a certain way. For example Pat Silly Doe would format to Doe, P.S. or Julia Clark would format to Clark, J. I've been trying to separate the string into just the two or three names but I can't figure it out, can someone please help? This is what I've got so far.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>
using namespace std;

int main() {
   string userInput;
   string firstName;
   string firstInitial;
   string middleInitial;
   string lastName;
   int spaceIndex1;
   int spaceIndex2;
   
   getline(cin, userInput);
   spaceIndex1 = userInput.find(' ');
   spaceIndex2 = userInput.find(' ', spaceIndex1 + 1);






return 0;
}
Last edited on
Are there always 3 names - first, middle and last - input, or can there just be two - first and last? You say take the first middle and last - but then you give an example with just first last??

When you have the indexes of where ' ' are, you can use .substr() to create a new string. See http://www.cplusplus.com/reference/string/string/substr/

so eg

 
firstName = userInput.substr(0, spaceIndex1);

Last edited on
Hello cole274,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button. This will not automatically indent your code. That part is up to you.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Can you use a string stream?

Andy
thank you for responding so fast seeplus, theres not always three names. The code is graded by a first and last name being input and then first, middle, and last name being input so it would have to work when using both types.

sorry about that Andy I think I fixed it thank you for letting me know. We havent learned about string streams yet but if its pretty simple Im sure thats fine.
Using a string stream is just like using >> on a stream (cin or file etc) except that it works on a string.

Using stringstream consider as a starter:

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

int main()
{
	std::string first, last, middle, inp;

	std::cout << "Enter name as either <first> <middle> <last> or <first> <last>: ";
	std::getline(std::cin, inp);

	std::istringstream iss(inp);    // Make iss an input string stream so that >> can be used

	iss >> first >> middle;

	if (!(iss >> last)) {    // Are there 3 names? If no then set accordingly
		last = middle;
		middle.clear();
	}

	std::cout << last << ", " << first[0] << ". ";

	if (!middle.empty())
		std::cout << middle[0] << '.';

	std::cout << std::endl;
}


it works, thank you so much!!
Hello cole274,

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

using namespace std;

int main()
{
    string userInput{ "Pat Silly Doe" };  // <---Changed for testing.
    string firstName;
    string firstInitial;
    string middleInitial;
    string lastName;
    size_t spaceIndex1;  // <---Changed.
    size_t spaceIndex2;  // <---Changed.

    //getline(cin, userInput);  // <--- Commented out for testing.

    spaceIndex1 = userInput.find(' ');
    spaceIndex2 = userInput.find(' ', spaceIndex1 + 1);

    firstInitial = userInput.substr(0, 1);

You have a good start to line 19 with one change I noted;

After line 19 you can use the "substr()" function to break up the input string.

You will also need to understand how to use "std::string::npos" to determine if there is a middle name or not. You will also use this to print out the changed name.

The "find" function returns a "size_type", which is the same as "size_t". http://www.cplusplus.com/reference/cstddef/size_t/ An "int" will work, but it is better to use the proper type.

Andy
Thank you so much Andy, do you know of any good youtube videos or website links that have good examples of how to use "std::string::npos". I'm starting to fall behind in the class I'm taking and could use all the help I can get.
Hello cole274,

No I do not know of any videos.

"std::string::npos" is just a value meaning that the end of the string is reached.
http://www.cplusplus.com/reference/string/string/npos/

"spaceIndex2 != std::string::npos)" is simiiar to saying if (spaceIndex2 != -1).

The "std::string::npos" is a more portable way to use what could be different with a different compiler and header files.

When you start with http://www.cplusplus.com/reference/string/string/ you can look at many of the member functions of the string class and find examples of "npos".

Andy
Another website link for std::string::npos:
https://en.cppreference.com/w/cpp/string/basic_string/npos
Ideally, you should account for names that are just one word, or several words.
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
#include <iostream>
#include <string>
#include <vector>
#include <sstream>

using std::cin;
using std::cout;
using std::vector;
using std::string;
using std::istringstream;

int main()
{
    string line;
    while (true) {
	cout << "Please enter a name, given name(s) first, surname last: ";
	if (!getline(cin, line)) break;

	// Read the names into a vector
	istringstream iss(line);
	vector<string> names;
	string name;
	while (iss >> name) {
	    names.push_back(name);
	}

	// Print out the name as Surname, Initial1.Initial2. ...
	if (names.size()) {
	    cout << names.back();

	    // Be sure to handle names that are just one word
	    if (names.size() > 1) {
		cout << ", ";
		for (unsigned i=0; i<names.size()-1; ++i) {
		    cout << (char)toupper(names[i][0]) << '.';
		}
	    }
	    cout << '\n';
	}
    }
}

Please enter a name, given name(s) first, surname last: Dave Hayden
Hayden, D.
Please enter a name, given name(s) first, surname last: Elmer E. Fudd
Fudd, E.E.
Please enter a name, given name(s) first, surname last: Pat Silly Doe
Doe, P.S.
Please enter a name, given name(s) first, surname last: Julia Clark
Clark, J.
Please enter a name, given name(s) first, surname last: Rhianna
Rhianna
Please enter a name, given name(s) first, surname last: Paula Patch Newell Doty Attridge
Attridge, P.P.N.D.
Please enter a name, given name(s) first, surname last:

Topic archived. No new replies allowed.