Click here to Skip to main content
16,007,932 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralConvert to UCS-2 .. Pin
rasha20038-May-04 23:11
rasha20038-May-04 23:11 
GeneralRe: Convert to UCS-2 .. Pin
Michael Dunn9-May-04 5:51
sitebuilderMichael Dunn9-May-04 5:51 
GeneralRe: Convert to UCS-2 .. Pin
rasha20039-May-04 8:20
rasha20039-May-04 8:20 
GeneralRe: Convert to UCS-2 .. Pin
Michael Dunn9-May-04 20:11
sitebuilderMichael Dunn9-May-04 20:11 
GeneralRe: Convert to UCS-2 .. Pin
rasha200310-May-04 5:09
rasha200310-May-04 5:09 
Generalabout allocator Pin
vividtang8-May-04 21:16
vividtang8-May-04 21:16 
GeneralRe: about allocator Pin
toxcct9-May-04 5:54
toxcct9-May-04 5:54 
GeneralNewbie string question Pin
MikeUofPhx8-May-04 17:00
MikeUofPhx8-May-04 17:00 
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 />
// Text Adventure-Console.cpp : Defines the entry point for the console application.<br />
<br />
#include <stdio.h><br />
#include <stdlib.h><br />
#include <string.h><br />
#include <mbstring.h><br />
<br />
//Variables - global//<br />
<br />
char waitkey [10];<br />
char *pch;<br />
//enum directions {//<br />
<br />
// Defenitions - global //<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 />
}; //Define the Map structure//<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 />
	}; // Instantiate map structure with 19 "Rooms" as my map - 0 is a dummy//<br />
<br />
//Set current room//<br />
int current_room = 1;<br />
<br />
int main(int argc, char* argv[])<br />
{	<br />
//Display current room//<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 />
//What exits are visible?//<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 />
//What would you like to do?//<br />
printf("\nWhat would you like to do?\n");<br />
char user_command[250];<br />
//Want whole sentence with EOL and secure buffer, so I go through the whole baloney here//<br />
fgets(user_command, sizeof(user_command), stdin);<br />
fflush(stdin); //Get rid of overflow above buffer limit//<br />
printf("\n");<br />
//Process Command//<br />
//Split in to words//<br />
 char *words[11];<br />
 int wordnumber = 0;<br />
char *pch;<br />
 pch = strtok (user_command, " ");<br />
 //Load the first word//<br />
 words[wordnumber]=pch;<br />
 //Load each additional word into words[]//<br />
 while ((pch != NULL) && (wordnumber < 10))// and word # is acceptable - add later//<br />
 {<br />
	 wordnumber++;<br />
	 words[wordnumber]=pch;<br />
	 pch = strtok (NULL, " ,.");<br />
 }<br />
//loop through//<br />
int count;<br />
 for(count=1;count<=wordnumber;count++)<br />
{<br />
	//Get last character, strcomp with enter, chomp<br />
	printf("**%s**\n",words[count]);<br />
}<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 />
	//They're going - what direction?//<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 />

GeneralRe: Newbie string question Pin
Anonymous8-May-04 20:27
Anonymous8-May-04 20:27 
GeneralRe: Newbie string question Pin
John R. Shaw8-May-04 22:13
John R. Shaw8-May-04 22:13 
GeneralRe: Newbie string question Pin
MikeUofPhx9-May-04 13:41
MikeUofPhx9-May-04 13:41 
GeneralOLE DB 'IColumnsInfo2' problem Pin
KalliMan8-May-04 13:06
KalliMan8-May-04 13:06 
QuestionWhich of these methods is more efficient? Pin
Joe Smith IX8-May-04 12:48
Joe Smith IX8-May-04 12:48 
AnswerRe: Which of these methods is more efficient? Pin
Gary R. Wheeler8-May-04 13:39
Gary R. Wheeler8-May-04 13:39 
AnswerRe: Which of these methods is more efficient? Pin
Prakash Nadar8-May-04 22:35
Prakash Nadar8-May-04 22:35 
AnswerRe: Which of these methods is more efficient? Pin
Paul Ranson9-May-04 1:15
Paul Ranson9-May-04 1:15 
AnswerRe: Which of these methods is more efficient? Pin
Ravi Bhavnani9-May-04 4:29
professionalRavi Bhavnani9-May-04 4:29 
AnswerRe: Which of these methods is more efficient? Pin
Joe Woodbury9-May-04 7:59
professionalJoe Woodbury9-May-04 7:59 
GeneralRe: Which of these methods is more efficient? Pin
Tim Smith9-May-04 9:23
Tim Smith9-May-04 9:23 
GeneralRe: Which of these methods is more efficient? Pin
Joe Woodbury9-May-04 9:42
professionalJoe Woodbury9-May-04 9:42 
GeneralRe: Which of these methods is more efficient? Pin
Tim Smith9-May-04 13:47
Tim Smith9-May-04 13:47 
AnswerRe: Which of these methods is more efficient? Pin
Tim Smith9-May-04 9:27
Tim Smith9-May-04 9:27 
GeneralUsing MFC Source Code Question Pin
nm_1148-May-04 8:26
nm_1148-May-04 8:26 
GeneralRe: Using MFC Source Code Question Pin
Joe Woodbury8-May-04 8:41
professionalJoe Woodbury8-May-04 8:41 
GeneralRPC: Problem in sending data Pin
ahsan_a8-May-04 8:17
ahsan_a8-May-04 8:17 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.