Scanf that scans even white space?
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 ;)
I want to work for Squaresoft ;)
use gets() and then parse it using strstr() or strchr()
..-=gLaDiAtOr=-..
..-=gLaDiAtOr=-..
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.
July 14, 2000 02:28 PM
Thanks guys!
I just found a great function to parse the command, it''s strtok();
I want to work for Squaresoft ;)
I just found a great function to parse the command, it''s strtok();
I want to work for Squaresoft ;)
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=-..
..-=gLaDiAtOr=-..
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement