I am a beginner but my problem with this game is that when i enter east it said it going to north.
here my code for my main and parser also when i enter a word then it quit the game so how do i make it stay open to allow me to enter other words
Main.CPP

#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

#ifndef parse_h
#define parse_h
#include <string>

//Class definition for Parser Class
class Parser
{
    char* commands [50];
    char* objects[50];
    //how many commands can be used
    int numcommands;
    //how many objects can be used
    int numobjects;

public:
    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];
        }
    }

    char* LowerCase (char* st);

    char* RemoveThe(char* sen);

    char* GetVerb(char* sen);

    char* GetObject(char* sen);
    void SortCommands();
    void SortObjects();
    void PrintCommands();
    void PrintObjects();
    bool IsWordinCommands(char* target);
    bool IsWordinObjects(char* target);
};
#endif

Recommended Answers

All 18 Replies

if ("north") is always true. You need to compare the variable word (I think).

if (word == "north")

thank for the reply but when i did that and open the game in cmd. tried to type in north/south but it didn't say any thing like "You Went North and found yourself in similar postion". i tpye in north and it come up saying "press any key to continue ..." so what should i do?

this is what happen when i type in north 9819de32c102df55844073bc6a42354f

not sure what u mean by changed main but i paste my main code

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


    system("pause");
    return 0;
}

The system("pause") shows that message, meaning nothing matched. Try adding this between lines 26 and 27:

word = trim(word);

well as i am beginer.. the trim identifier not been found/undefined
also do u have something that we can chat online so it would be helpful

I have search for this problem but tried to add #include <cstdlib> in my main one but still didn't work.. still don't know it =(

still haven't solve it yet, been trying to define it. any help or maybe a link to make me search what to include this =(

thank for the lin. i think this made the trim work but there red line under word which is saying expression must be a modifiable lvalue
here the new code now

#include <iostream>
#include <fstream>
 #include <cstdlib>
#include <vector>
#include <string>

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

using namespace std;

string trim(const string &str);
string ltrim(const string &str);
string rtrim(const string &str);

int main(int argc, char *argv[])
{
    string str1 = " Hello World ";
    cout << "[" << str1        << "]" <<endl;
    cout << "[" << ltrim(str1) << "]" <<endl;
    cout << "[" << rtrim(str1) << "]" <<endl;
    cout << "[" <<  trim(str1) << "]" <<endl;
}
string ltrim(const string &str)
{
    size_t at = str.find_first_not_of(" \t\r\n\0\a\b\f\v");
    return at == string::npos ? str : str.substr(at);
}
string rtrim(const string &str)
{
    size_t at = str.find_last_not_of(" \t\r\n\0\a\b\f\v");
    return at == string::npos ? str : str.substr(0, at+1);
}
string trim(const string &str0)
{
    string str = str0;
    size_t at2 = str.find_last_not_of(" \t\r\n\0\a\b\f\v");
    size_t at1 = str.find_first_not_of(" \t\r\n\0\a\b\f\v");
    if (at2 != string::npos) str.erase(at2+1);
    if (at1 != string::npos) str.erase(0,at1);
    return str;
}
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;

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


    system("pause");
    return 0;
}

I know that you are only a beginner and may not be comfortable with such things, but I would recommend that you a) come up with a class that holds the area descriptions, rather than hard-coding each one in, and b) storing the data for each of the areas in a file or files, which the program could load to memory as needed. This would avoid having a lot of long if/else if/else combinations like the one you have here.

hmm how would i start that?

Consider all the things that go into representing a given locale. Here are some of the basic ones:
* A full description of the locale itself, to be used the first time the player enters it. At the player's request, this should be repeated, as well.
* An abridged description, for repeated entries.
* A description of each manipulable item in the area.
* A set of links or pointers to other areas, one for each of the cardinal points (north, west, south, east), as well as up and down. This includes handling the cases where there is no path in a given direction.

Number three brings up another issue which hasn't come up yet: representing the items that player can pick up, drop, change, etc. that are in the rooms. That will also require a class, possibly more than one.

You can't compare char*s using the == operator. You need strcmp from the cstring header for that, but a better solution would be to use std::strings instead of char*s. std::strings can be compared using ==.

but a better solution would be to use std::strings instead of char*s

and since this is C++, it would be advised to use std::string when dealing with strings. std::string class offers a lot of string manipulation methods and other handy features. Have a look at them:
Click Here

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.