Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Eponick

Accepting multiple words with cin?

This topic is 5538 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I was wondering how to take more than 1 word with cin? Like in a MUD if you type something like: look monster It would put look somewhere like Input[0] and monster at Input[1] This is something I never learned to do before going to Win32 programming, and I got curious about it because of a project im working on.

Share this post


Link to post
Share on other sites
Advertisement
I assume you''re doing

--
string input;

cin >> input;
--

If so, yeah, input is essentially a "token".

You could do...

--
string line;

getline(cin, line);
--

that will read a line''s worth of input into line.

Share this post


Link to post
Share on other sites
That puts the whole thing into the var, im wondering if theres something to spread each word into an array of strings or something.

Share this post


Link to post
Share on other sites
Try this:

int i;
char Input[ 100 ][ 80 ];

for ( i = 0; i < 100; i++ )
{
cin << Input[ i ];
}

for ( int j = 0; j < i; j++ )
{
cout >> Input[ j ] >> " - ";
}

I''m not sure about it, but I seem to remember it from C++ class.
Try it.

Share this post


Link to post
Share on other sites
What about...sscanf("%s %s", input1,input2);
strcmp(input1,input2)?

Somthing like that?

Share this post


Link to post
Share on other sites
quote:
Original post by ACAC
What about...sscanf("%s %s", input1,input2);
strcmp(input1,input2)?

Somthing like that?



That will only work if your string has two words, you have two variables, and they are preallocated char buffers. It won''t work nicely with C++ containers nor strings.


[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!