text adventure game

I am making text adventure game but i seem can't made an option when the user type in north then in the game it will make the user to go north. this work but when i type in east but it said north

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>

#ifndef Parse_h
#define Parse_h
#include "Parser.h"
#endif

using namespace std;

int main()
{
	char sentence[50];
	char* verb;
	char* object;
	Parser textparser;
	cout<<"                         Welcome To Text Adventure Game!					 	 "<<endl;
	cout <<"To move in the game, You will need to type in north, east, south, west";
	cout <<"There another command you can use in the game.\nWhich are : 'pick', 'open', 'read', 'drop', 'eat', 'close', 'look', 'search ";
	cout <<""<<endl;
	cout <<""<<endl;
	cout <<"\nYou Woke up and found yourself in Deep Dark Forest and you need to get out" << endl;

	cout <<"\nNow what you going to type in >  " ;

	cin.getline(sentence,50);
	verb = textparser.GetVerb(sentence);
	object = textparser.GetObject(sentence);my s
	if("north")
			{
				cout <<"\nYou Went North and found yourself in postion";
				cout <<"\nWhat should you do now?";
			}
	else if("east")
			{
				cout <<"\nYou went East and saw House";
				cout <<"\nWhat should you do now?";
			}
	else if("south")
			{
				cout <<"\nYou cannot go backward beacuse there a wall blocking the path";
				cout <<"\nWhat should you do now?";
			}
	else if("west")
			{
				cout <<"\nYou went to west but there are nothing in there.";
				cout <<"\nWhat should you do now?";
			}

	cout << "Verb in the sentence is " << verb << endl;
	cout << "Object in the sentence is " << object << endl;

	system("pause");
	return 0;
}

Thank
if("north") will always evaluate to true.
You can rewrite it as
1
2
const char x[] = "north"; //x cannot be null here
if (x) //always true 
Thank for the reply.
but not sure if i understand it since i am beginner
i tried
1
2
const char* cmds[] = "north";
if ("north")

but didn't work or i am wrong?
also it show c++, "Error: initialization with '{...}' expected for aggregate object" under "north"
Last edited on
when i input the command to allow it go normal which it will go north but when i try to input east,south,west but it sill go north
and am trying to get program stay when the user input one word then the next sentence come out then the user enter new word which it will go other location but it don't work for me since when i input one word the sentence come out but when i enter any key it end the program.
i have include Main.cpp and parser.h
Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <fstream>

#ifndef Parse_h
#define Parse_h
#include "Parser.h"
#endif

using namespace std;

int main()
{
	
	Parser textparser;
	char word[20];
		cout<<"                         Welcome To Text Adventure Game!					 	 "<<endl;
	cout <<"To move in the game, You will need to type in north, east, south, west";
	cout <<"\nThere another command you can use in the game.\nWhich are : 'pick', 'open', 'read', 'drop', 'eat', 'close', 'look', 'search ";
	cout <<""<<endl;
	cout <<""<<endl;
	cout <<"\nYou Woke up and found yourself in Deep Dark Forest and you need to get out" << endl;

	cout <<"\nNow what you going to type in >  " ;


	cin >> word;
	textparser.IsWordinCommands(word);
	textparser.IsWordinObjects(word);
			
const char cmds[] = "north";
if ("north")
			{
				cout <<"\nYou Went North and found yourself in similar postion";
				cout <<"\nWhat should you do now? >  ";
			}
else if ("east")
			{
				cout <<"\nYou went East and saw House";
				cout <<"\nWhat should you do now? >  ";
			}
else if("south")
			{
				cout <<"\nYou cannot go backward beacuse there a wall blocking the path";
				cout <<"\nWhat should you do now? >  ";
			}
else if("west")
			{
				cout <<"\nYou went to west but there are nothing in there.";
				cout <<"\nWhat should you do now? >  ";
			}

	
	system("pause");
	return 0;
}

Parser.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#ifndef parse_h
#define parse_h
#include <string>

//Class definition for Parser Class
class Parser
{
	//class 2d array to store dictionary of commands
	char * commands [50];
	//char 2d array to store dicitionary of oobjects
	char* objects[50];
	//number of commands
	int numcommands;
	//number of objects
	int numobjects;

public:
	//default constructor
	Parser()
	{
		numcommands = 12;
		numobjects = 12;
		//a word to do something
		char* cmds[] = {"north", "east", "south",
						"west", "pick", "open",
						"read", "drop", "eat",
						"close", "look", "search"};
		//an object that can be used to intract with object.
		char* objs[] = {"fork", "knife", "sword",
						"enemy", "monster", "shield",
						"armour", "spoon", "table",
						"door", "room", "key"};
		//initialise commands array with these valid command words
		for(int i=0 ; i<numcommands ; i++)
		{
			commands[i] = cmds[i];
		}
		//initialise objects array with these valid object words
		for(int i=0 ; i<numobjects ; i++)
		{
			objects[i] = objs[i];
		}
	}
	//return a char array which is lowercase version of input char array 
	char* LowerCase (char* st);
	//This method will remove all instance of "the" from a sentence
	// and return a pointer to the new stripped sentence
	char* RemoveThe(char* sen);
	//This method will return a pointer to a char array with the acition of the input
	//sentence after it has been parsed of articles
	char* GetVerb(char* sen);
	//This method will return the object of the input
	//sentence after it has been parsed of articles
	char* GetObject(char* sen);
	void SortCommands();
	void SortObjects();
	void PrintCommands();
	void PrintObjects();
	bool IsWordinCommands(char* target);
	bool IsWordinObjects(char* target);
};
#endif 
Last edited on
can anyone help am still stuck on this part
Topic archived. No new replies allowed.