Wednesday, September 18, 2013

C - separating strings in the input stream

C - separating strings in the input stream

My program is supposed to be able to create new structures and store them
in an array, however, the commands for storing and displaying pose
difficulty.
To create a new variable struct in the array, the user inputs "set varname
varcontents
To display the contents of a variable, the user inputs "set varname"
To display all variables, the user inputs "set"
I can't quite figure out how to check if there are multiple strings ("set"
"varname" "varcontents") or if there is only "set"
char command[2][5] = { "set", "clear"};
printf("prompt> ");
scanf("%s",inputString);
if(strncmp(inputString,command[0],5) == 0 )
{
//code to create new struct, display structs etc...
}
else if(strncmp(inputString,command[1],5) == 0 )
{
//code to clear struct
}
Right now the if loop only passes if the user inputs "set". I could
probably take the comparison of the first few letters, and then take the
full comparison and subtract the first few characters to generate the name
of the new struct, but this seems too complicated, there must be an easier
solution.
Any help is appreciated!

No comments:

Post a Comment