Archived

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

Scanf that scans even white space?

This topic is 6365 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

In my game, the player can enter commands as long as 4 words, but sometimes a command only has one word, for example: 4 word command is: put 40 platinum bag 1 word command is: west so i''m setting up scanf as: char command1[23], command2[23], command3[23], command4[23]; scanf("%s %s %s %s", command1, command2, command3, command4); but the problem with this is that if a player enters one word command or any command that''s noy 4 words, the computer will keep asking you for the the other commands and would not process the entered commands. I am looking for a better way to do this, or maybe something that''s not "%s" so that it can also like include in white space, so I can do manipulation afterwards. Thanks for your help I want to work for Squaresoft ;)

Share this post


Link to post
Share on other sites
using scanf like this is dangerous. You''re only allocating 23 bytes for a command. What happens if the user enters 24 bytes before a space? use fgets instead. That way you can chop the input string up yourself as need be.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Thanks guys!

I just found a great function to parse the command, it''s strtok();



I want to work for Squaresoft ;)

Share this post


Link to post
Share on other sites
FordPerfect, using it that way is dangerous, but since the scanf() function is very flexible, you can make it stop reading when it reads in a certain number of characters... and plus, we''re not talking about what is safe and what isn''t here... we''re trying to convert a char to it''s equivalent number.. or some $)-(it like that..


..-=gLaDiAtOr=-..

Share this post


Link to post
Share on other sites