First, my program works.

I'm posting my code for my own personal benefit with the help of your guidance to know of any better way of handling this code. I was considering the use of

while(cin >> grabname)

to possibly continue grabbing from the input stream until '\n' was reached (hoping <enter> would made an eof approach). Seeing how this made an infinite loop, I had then chosen to switch from strings to char then back to strings. It appeared this was the only solution that I could come up with that allowed a check for any additional values of non-whitespace or '\n'. This approach doesn't seem very smooth to me, any recommendations with string manipulations?

I know I could try getline(cin, FullName), then split it them all through a function with vector<string> but the text did not recommend this and suggesting 3 string variables, mentioned it would be easier that way.

The point of the program is to output a persons name LastName, FirstName MiddleInitial. If no middle name or initial was given then output would look as follows, LastName, FirstName.

Just as a refresh statement for any that did not know -this is not for any class.-
Home-study 100%.

If this is an inappropriate post please let me know.

Thanks in advance.

int main(){

	char aChar;

	string FirstName, MiddleName, LastName;
	
	cout << "What is your first and last and possibly middle name?\n";
	
	cin >> FirstName >> LastName;

	cin.get(aChar);

	while(aChar != '\n' && aChar == ' ')
		cin.get(aChar);

	if(aChar != '\n' && aChar != ' ') {
		string temp;
		cin >> temp;
		MiddleName = LastName;
		LastName = aChar + temp;
	}

	output(FirstName, MiddleName, LastName);

	return 0;
}

since you have varying possibilities of user input, i believe the better approach to this problem would be to read in the entire chunk of user input, test the input, and parse into individual substrings as desired.

Here is one possible method using the versitle <sstream> library (very useful for string parsing) based on this tutorial:

#include<string>
#include<sstream>

string full_name,
       first_name,
       last_name,
       middle_name;

stringstream temp;

cout << "What is your first and last and possibly middle name?\n";
cin >> full_name;

temp << full_name;

fist_name << temp;
last_name << temp;

if(temp)
{
     middle_name << temp;
}

cout << "Ye' first name is: " << first_name << endl;
cout << "Ye' last name is: "  << last_name  << endl;

if(!middle_name.empty())
{
     cout << "Yo' middle name be:  " << middle_name << endl;
}

If you know you are getting reliable & standard input from the user (which is quite a big assumption), then you'd be able to chain together your extraction operations.. but since you don't, I'd take what you get, figure out what you got, and handle it accordingly.

>>hoping <enter> would made an eof approach

Yeah, cin is special, it basically never has an end, because it can always wait for the user to input more stuff. But, in any case, that kind of while(some_stream >> value) loop is pretty usual for other "normal" streams (like file streams and string streams). However, in this case you have heterogeneous data, so a loop is generally inappropriate for that (it is appropriate when you have sequential and homogeneous data, like a sequence of numbers).


For the rest, I pretty much agree with Clinton Portis. In these cases, you are usually better off inputting whole lines into a string stream and then deal with what you got in there (it is easier for inspection of the content, validating it, etc.). Of course, as with your code, you CAN do it without, but that doesn't mean you should.

Another useful trick with string parsing is to flip between string-stream and string. You can use the string-stream for sequentially extracting (formatted) information. And you can switch to string to have random-access (to count words or commas, or whatever defines the 'validity' of your input).

> I know I could try getline(cin, FullName), then split it them all
> but the text did not recommend this and suggesting 3 string variables

This would be one way of doing it:

std::string FirstName, LastName ;
	std::cout << "What is your first and last name? " ;

	if( std::cin >> FirstName >> LastName >> std::skipws )
        {
            std::string MiddleName ;
	    std::cout << "What is your middle name? "
                         "enter EOF (Posix CTRL+D, Microsoft CTRL+Z) if none: " ;
	    std::cin >> MiddleName ;

            std::cout << '\n' << LastName << ", " << FirstName ;
            if( !MiddleName.empty() ) std::cout << ' ' << MiddleName ;
	    std::cout << ".\n" ;
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.