C++ string help
Hi everyone, I'm a first year computer science major, and my prof. gave us small homework project to do. Basically we have to write a program to let the user input their full name and then the program should display the first, middle, and last name seperately. example, when the user inputs "John Jack Smith" the program should display
"First name: John Middle name: Jack Last name: Smith"
I did fine by using this code
int main() { cin >> First >> Middle >> Last;
cout << "First Name: " << First << endl
<< "Middle Name: " << Middle << endl
<< "Last Name: " << Last << endl;
}
My question, though, is this…is there a better way to do this? This works fine in that if the user doesn't input 3 names the program will continue to ask for another input. The problem, however, is if the user has 4 names. The fourth name would get cut off. I'm curious how I could have the user input the entire name as a single variable, and then seperate it at the first three spaces. ex. "John Ronald Reuel Tolkien" would become
"First name: John Middle name: Ronald Last name: Reuel Tolkien"
I tried setting a single char variable called name and then used this code
cin >> name;
while (name != '*')
{
cout << name;
cin >> name;
}
I can't this to get past the first name though.
After that I tried to use getline(cin, name) to get the full name into a single string. The problem is I can't figure out how to seprate it at the spaces. I've searched through my text book for anything that would do this for me, and as of yet I haven't found it. Any help any of you can offer I would appreciate it. I've googled it to no avail.
Why not have it sort the arguments as an array, then call each of them seperatly. Does it have to be C++? If it can be any language you can use perl.
if(@ARGV !=3){
print ("Usage perl: $0 <first name> <Middle name> <Last name>\n");
exit;
}
($first, $middle, $last) = @ARGV;
print "$first\n";
print "$middle\n";
print "$last\n";```
That works pretty well. If you have perl2exe you can turn it into an exe.
Bl4ckC4t
The edit was a spell check.
God I feel dumb now that I figured this out. Thanks for all your help guys.
First I redid the character method like this
cout << "Please enter your full name:";
cin >> name;
while (name != ' ') { smeg = smeg+name; cin.get(name); } cout << "First Name: " << smeg <<endl;
smeg=""; cin >> name2;
while (name2 != ' ') { smeg = smeg+name2; cin.get(name2); } cout << "Middle Name: " << smeg <<endl;
smeg="";
cin >> name3;
while (name3 != '\n') { smeg = smeg+name3; cin.get(name3); } cout << "Last Name: " << smeg <<endl;
}
After I did that I had a much better understand of how variables are stored and how it all works; so that led me to realize I could do the same thing using the following code:
cout << "Please enter your full name:";
cin >> First >> Middle; getline(cin, Last); cout << "First Name: " << First << endl << "Middle Name: " << Middle << endl << "Last Name: " << Last << endl;
Again guys(and girls if any of you are female), thanks for all your help. I feel like my brain has grown.