Find and Erase a "," without for loops etc...

I'm trying to find a string of code to find and erase the comma after the user inputs their last name. The user is supposed to enter their name in the form of lastname, middle name first name.

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

const string GREET = "Welcome to my program! Please enter your full name (last, Middle First.) using only upper and lower case letters press enter after each entry.";
const string REPLY = "Your name is ";
const string END   = "Thank you for using my program!";
int main()
{
	string firstName;
	string middleName;
	string lastName;
	cout << GREET << endl;
	getline (cin, lastName);
	getline (cin, middleName);
	getline (cin, firstName);
	cout << REPLY << firstName << ' ' << middleName << ' ' << lastName << '.' << endl;
	cout << END;
}
Are you sure that they are supposed to enter "last name, middle name first name" and not "last name, first name, middle name"? If it's the former separating the last name is easy, just use the optional third parameter to getline() but the middle name and first name will be problematic, unless all middle names and first names can only contain one word. If the latter then you can use the third parameter to separate the first two entries from the third.

Hello RiggityWrecked,

When I ran your program the instructions tell me to enter (lastname, middlenme firatname). When I do all of that goes into the variable lastName and at the next getline I have no idea what it is waiting for.

You could use a different variable to get the full input and a string stream to extract each part into the correct variable, or just try 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
#include <iostream>
#include <string>

//  These could just as easily be defined inside main.
const std::string GREET = "Welcome to my program! Please enter your full name (last, Middle First.)\nusing only upper and lower case letters press enter after each entry.";
const std::string REPLY = "Your name is ";
const std::string END = "\nThank you for using my program!";

int main()
{
	std::string firstName;
	std::string middleName;
	std::string lastName;

	std::cout << GREET << std::endl;
	std::cout << "\nEnter last name: ";
	std::getline(std::cin, lastName);
	std::cout << "\nEnter first name: ";
	std::getline(std::cin, firstName);
	std::cout << "\nEnter middle name: ";
	std::getline(std::cin, middleName);

	std::cout << "\n\n" << REPLY << firstName << ' ' << middleName << ' ' << lastName << '.' << std::endl;
	std::cout << END;

	//  This is a good place to put some code to keep the console window from closing down to soon.
        //  If you need it.

	return 0;
}


I would also change the instructions to say each name on a separate line or remove the example based on the prompts.

jlb makes a good point. It may not be of much use here, but in the future you will find it very handy.

BTW I would say the more accepted way to enter a name is (last name, first name middle name). And do not be afraid to use blank lines or spaces to meke your program easier to read and work on.

Hope that helps,

Andy
Last edited on
Hello RiggityWrecked,

After some thought I realize I had this web page open http://www.cplusplus.com/reference/string/string/

You could the find function to find the "," followed by the replace function to replace the "," with a space. This way if the input is in the same order as the output that you have thee output would be correct. this will also allow you put your input into one variable and not three.

Another possibility would be to put the input into one variable and use the "sstream" header file to use a string steam to extract each part of the names into each variable.

Hope that helps,

Andy
Thanks all! Yeah I misread the problem in the book. Last, First middle. I thought it was weird when I was working on it. The main problem is getting rid of the comma, but I love the prompt after each name entry. I should have thought of that myself.
How does the replace function work? We haven't really gone over that in class.
Last, First middle


Then all you need is this:
1
2
3
4
5
    string last;
    string first_middle;

    getline(cin, last, ',');
    getline(cin, first_middle);


(well, you might have to take care of leading and/or trailing spaces afterwards, depending on how the user actually typed the input).
Last edited on
the user is inputting last, first middle. The program is supposed to return "Your name is first middle last." removing the comma.
Then add this to my previous suggestion:
 
    cout << "Your name is " << first_middle << ' ' << last << ".\n";


Complete (minimal) working example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
#include <string>

int main()
{
    std::string last;
    std::string first_middle;
    
    std::cout << "Please enter your full name (last, Middle First.)\n";
    
    getline(std::cin, last, ',');
    getline(std::cin, first_middle);
    
    std::cout << "Your name is " << first_middle << ' ' << last << ".\n";
}

Of course, the original question, though perhaps not necessary to solve this problem, should not be ignored, it may be needed elsewhere, or as an alternative solution to the present problem.

What you could do is to read the entire line using an ordinary getline().

Then use the find() function to locate the comma.
http://www.cplusplus.com/reference/string/string/find/

Check that it actually exists.

Then use the substring function to get the piece from the start up to the comma, and another substring to get the part from after the comma to the end
http://www.cplusplus.com/reference/string/string/substr/

There is an erase() function too, but that won't help you here, as you want two new strings, not the original string with the comma removed.
Last edited on
Hello RiggityWrecked,

The first thing to decide on is if you want one single variable for input or three separate variables. With a single variable for everything it is very easy to use find and erase to remove the ','. If after that you need the one line broken up into three separate variables that can be done.

If you input to three separate variables then there is no need for the comma.

Here is one idea:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main()
{
std::string line{"Jones, John Paul"};  // <---Initialized here for testing.
std::string firstName{}; // <--- Always initialize your variables.
std::string middleName{};
std::string lastName{};

//std::getline(std::cin, line);  // <--- Commented out for testing.

size_t pos = line.find(',', 0);
line.erase(pos, 1);

// This breaks a single line into three variables.
std::istringstream ss(line);  // <--- Needs header file <sstream>
std::getline(ss, lastName, ' ');
std::getline(ss, firstName, ' ');
std::getline(ss, middleName);
.
.
.
return 0;
}


It is a bit confusing to give the user an example to enter everything o one line and then try to retrieve that information with three separate getline statements.

Figure out how you want the user to input the data and I can comme up with something from there.

Hope that helps,

Andy
The first thing to decide on is if you want one single variable for input or three separate variables.

Those are not the appropriate alternatives. The reason it is worded as "Last, First Middle" is because not everyone has three names. Some may go by a single name, a much larger number have two, the 'middle' doesn't exist for many people. Hence the use of "Last, First Middle" which will default to "Last, First" for those people with two names only.
Thank you Chervil you got me thinking in a different direction an I came up with this:

1
2
3
4
5
std::cout << GREET << std::endl;
std::getline(std::cin, lastName, ',');
std::cin >> std::ws;
std::getline(std::cin, firstName, ' ');
std::getline(std::cin, middleName);


Although to leave the middle name blank I had to enter a space which made the output look bad with an extra space. Have an idea I want to try to fix this.

Andy
Andy, what happens if a first name happens to be multiple words?

@ jlb,

Thought about that, but have not figured it out yet.

The problem is taking (lastName, firstName middleName) as one input and putting it into three different variables. How to break up firstName with space in when first and middle are separated by a space?

Sometimes I think we are all making more out of this than it needs to be trying to account for every possibility.

@RiggityWrecked,

One possible solution for now:

1
2
3
4
const std::string GREET = "Welcome to my program! Please enter your full name (Last, First (Middle, use space for blank).)\nusing only upper and lower case letters press enter after each entry.";

std::cout << "\n\n" << REPLY << firstName << ' ';
((middleName.size() > 0) ? std::cout << middleName << ' ' : std::cout << middleName) << lastName << '.' << std::endl;


This eliminated the extra space if there was no middle name.

Hope that helps,

Andy
The problem is taking (lastName, firstName middleName) as one input and putting it into three different variables. How to break up firstName with space in when first and middle are separated by a space?

Sometimes I think we are all making more out of this than it needs to be trying to account for every possibility.

Probably true.

This is why I asked the OP for more clarification, which was provided in a later post.
the user is inputting last, first middle.

Which is then easier than what was requested in the first post, as long as you don't need to worry about separating the first and middle name. If you also need to separate the middle name and first name you're going to have to make assumptions that may not always be correct.


Topic archived. No new replies allowed.