I am using fgets to get a character array, and then looping through to split it into words by spaces with strtok. For the next step, I am creating a control structure based upon those words. I seem to be able to get the first word but not the next using _strnicmp. It doesn't reuturn 0 when there is a match. I am assuming there is some control character involved that I'm not seeing. Part of the byte is that it has to be C (not C++) and use any non-standard libraries.
So, my question is - how do I do that
and 2, isn't tjhere a better way like a case statement with strings?
I would appreciate any help I can get. Thanks.
Here's the code I have so far:
<br />
<br />
#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
#include <mbstring.h><br />
<br />
<br />
char waitkey [10];<br />
char *pch;<br />
<br />
struct map {<br />
char *name;<br />
char *description;<br />
int north;<br />
int south;<br />
int west;<br />
int east;<br />
int up;<br />
int down;<br />
int other;<br />
};
struct map my_map[20] = {<br />
{"Location0", "Dummy Location", 0,0,0,0,0,0,0},<br />
{"Location1", "Admissions", 2,0,0,0,0,0,0},<br />
{"Location2", "Lovely Lounge", 3,1,0,0,0,0,0},<br />
{"Location3", "Main Hallway", 3,1,0,0,0,0,0},<br />
{"Location4", "TV Room", 0,0,0,0,0,0,0},<br />
{"Location5", "Mess Hall", 0,0,0,0,0,0,0},<br />
{"Location6", "Crafts Room", 0,0,0,0,0,0,0},<br />
{"Location7", "Therapy Room", 0,0,0,0,0,0,0},<br />
{"Location8", "Padded Cell", 0,0,0,0,0,0,0},<br />
{"Location9", "Mike's Room", 0,0,0,0,0,0,0},<br />
{"Location10", "Nurse's Station", 0,0,0,0,0,0,0},<br />
{"Location11", "Group Therapy Room", 0,0,0,0,0,0,0},<br />
{"Location12", "Fenced Patio", 0,0,0,0,0,0,0},<br />
{"Location13", "Showers", 0,0,0,0,0,0,0},<br />
{"Location14", "Violent Ward", 0,0,0,0,0,0,0},<br />
{"Location15", "Honest Politician's Ward", 0,0,0,0,0,0,0},<br />
{"Location16", "Depressed College Student's Ward", 0,0,0,0,0,0,0},<br />
{"Location17", "Phone Booth", 0,0,0,0,0,0,0},<br />
{"Location18", "Secret Ritual Room", 0,0,0,0,0,0,0},<br />
{"Location19", "Psychiatrist's Lounge", 0,0,0,0,0,0,0}<br />
};
<br />
int current_room = 1;<br />
<br />
int main(int argc, char* argv[])<br />
{ <br />
printf("*** Josh's fantastic escape from the C Sanitarium for burnt out Coderz ***\n");<br />
printf("\n\n");<br />
printf("LOCATION : %s\n", my_map[current_room].name);<br />
printf("DESCRIPTION : %s\n",my_map[current_room].description);<br />
printf("WHAT'S HERE :\n");<br />
printf("VISIBLE EXITS:");<br />
<br />
if (my_map[current_room].north >= 1)<br />
{<br />
printf("North ");<br />
}<br />
<br />
if (my_map[current_room].south >= 1)<br />
{<br />
printf(" South ");<br />
}<br />
if (my_map[current_room].west >= 1)<br />
{<br />
printf(" West ");<br />
}<br />
if (my_map[current_room].east >= 1)<br />
{<br />
printf(" East ");<br />
}<br />
if (my_map[current_room].up >= 1)<br />
{<br />
printf(" Up ");<br />
}<br />
if (my_map[current_room].down >= 1)<br />
{<br />
printf(" Down ");<br />
}<br />
if (my_map[current_room].other >= 1)<br />
{<br />
printf(" Other ");<br />
}<br />
printf("\n");<br />
printf("\nWhat would you like to do?\n");<br />
char user_command[250];<br />
fgets(user_command, sizeof(user_command), stdin);<br />
fflush(stdin);
printf("\n");<br />
char *words[11];<br />
int wordnumber = 0;<br />
char *pch;<br />
pch = strtok (user_command, " ");<br />
words[wordnumber]=pch;<br />
while ((pch != NULL) && (wordnumber < 10))
{<br />
wordnumber++;<br />
words[wordnumber]=pch;<br />
pch = strtok (NULL, " ,.");<br />
}<br />
int count;<br />
for(count=1;count<=wordnumber;count++)<br />
{<br />
printf("**%s**\n",words[count]);<br />
}<br />
<br />
int result;<br />
char go[] = "go";<br />
result = _strnicmp(words[0], go, 2 );<br />
<br />
if (result==0)<br />
{<br />
printf("You want to go somewhere\n");<br />
char north[] = "north";<br />
char south[] = "south";<br />
char west[] = "West";<br />
char east[] = "east";<br />
char up[] = "up";<br />
char down[] = "down";<br />
char other[] = "other";<br />
<br />
int result;<br />
result = _strnicmp(words[1], north, 5 );<br />
if (result==0)<br />
{<br />
printf("You want to go north\n");<br />
}<br />
result = _strnicmp(words[1], south, 5 );<br />
if (result==0)<br />
{<br />
printf("You want to go south\n");<br />
}<br />
result = _strnicmp(words[1], west, 4 );<br />
if (result==0)<br />
{<br />
printf("You want to go west\n");<br />
}<br />
result = _strnicmp(words[1],east, 4 );<br />
if (result==0)<br />
{<br />
printf("You want to go east\n");<br />
}<br />
result = _strnicmp(words[1], up, 2 );<br />
if (result==0)<br />
{<br />
printf("You want to go up\n");<br />
}<br />
result = _strnicmp(words[1], down, 4 );<br />
if (result==0)<br />
{<br />
printf("You want to go down\n");<br />
}<br />
result = _strnicmp(words[1], other, 5 );<br />
if (result==0)<br />
{<br />
printf("You want to go north\n");<br />
}<br />
}<br />
char blow[]="blow";<br />
result = _strnicmp(words[0], blow, 4 );<br />
if (result==0)<br />
{<br />
printf("blow");<br />
}<br />
<br />
<br />
return 0;<br />
}<br />
|