Welcome to HBH! If you have tried to register and didn't get a verification email, please using the following link to resend the verification email.

C++ string help


ghost's Avatar
0 0

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.


ghost's Avatar
0 0

You could make a loop to parse the strings. When it reaches a space it should store into another variable etc.


Uber0n's Avatar
Member
0 0

J2383 wrote:

cin >> name; while (name != '*') { cout << name; cin >> name; }

Well in (name != '*') the * doesn't mean an asterisk… ^^ In fact, when you use cin it will continue until you give any valid input. To be able to store a space, use cin.getline()


bl4ckc4t's Avatar
Banned
0 0

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 (&quot;Usage perl: $0 &lt;first name&gt; &lt;Middle name&gt; &lt;Last name&gt;&#92;n&quot;);
exit;
}
($first, $middle, $last) = @ARGV;
print &quot;$first&#92;n&quot;;
print &quot;$middle&#92;n&quot;;
print &quot;$last&#92;n&quot;;```

That works pretty well. If you have perl2exe you can turn it into an exe.

Bl4ckC4t


The edit was a spell check.

ghost's Avatar
0 0

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.


Uber0n's Avatar
Member
0 0

Well then the problem solved as good as it could ever do :D