Follow us on Twitter!
Understanding is the answer, hatred is the problem, and hackers are the slaves abused and destroyed in the process of peace online - Deshouleres
Wednesday, April 16, 2014
Navigation
Home
HellBoundHackers Main:
HellBoundHackers Find:
HellBoundHackers Information:
Learn
Communicate
Submit
Shop
Challenges
HellBoundHackers Exploit:
HellBoundHackers Programming:
HellBoundHackers Think:
HellBoundHackers Track:
HellBoundHackers Patch:
HellBoundHackers Other:
HellBoundHackers Need Help?
Other
Members Online
Total Online: 19
Guests Online: 15
Members Online: 4

Registered Members: 82810
Newest Member: TheDuke777
Latest Articles
View Thread

HellBound Hackers | Computer General | Programming

Author

C++ string help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-10-06 16:30
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 != '*'Wink
{
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.
Author

RE: C++ string help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 16-10-06 18:30
You could make a loop to parse the strings. When it reaches a space it should store into another variable etc.


Author

RE: C++ string help

Uber0n
Member



Posts: 1963
Location: Sweden‭‮
Joined: 13.06.06
Rank:
Hacker Level 3
Posted on 16-10-06 19:18
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()


img230.imageshack.us/img230/724/uber0nsig3hj6.gif
http://uber0n.web. . .
Nope http://uber0n.webs.com/
Author

RE: C++ string help

bl4ckc4t
Member

Your avatar

Posts: 591
Location: /etc/
Joined: 07.03.06
Rank:
Wiseman
Posted on 16-10-06 20:26
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.

Code
#!/usr/bin/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.




Edited by bl4ckc4t on 16-10-06 20:30
Author

RE: C++ string help


Member

Your avatar

Posts:
Location:
Joined: 01.01.70
Rank:
Guest
Posted on 19-10-06 15:07
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 != ' 'Wink
{
smeg = smeg+name;
cin.get(name);
}
cout << "First Name: " << smeg <<endl;

smeg="";
cin >> name2;

while (name2 != ' 'Wink
{
smeg = smeg+name2;
cin.get(name2);
}
cout << "Middle Name: " << smeg <<endl;

smeg="";

cin >> name3;

while (name3 != '\n'Wink
{
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.
Author

RE: C++ string help

Uber0n
Member



Posts: 1963
Location: Sweden‭‮
Joined: 13.06.06
Rank:
Hacker Level 3
Posted on 19-10-06 15:24
Well then the problem solved as good as it could ever do Grin


img230.imageshack.us/img230/724/uber0nsig3hj6.gif
http://uber0n.web. . .
Nope http://uber0n.webs.com/