evaluating each word in a sentence

I'm working on a text game to get myself back into the basics of c++ and hopefully learn new things along the way. I want to be able to process two-part commands like 'open door' without having an IF statement for each thing I can open. Instead, there should be a function called for open that then checks for what is being opened. Is it possible to use an array of characters for this, or do I need to use something like substring? It looks like the standard string type only stores the first word. Defining char arr[15] and using cin to store 'open door' still only stores the first word. I'm not sure where to start on this one. Any and all help is appreciated.

Here's my code so far.
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
#include <iostream>
#include <string>

using namespace std;

void process(string com);
void waitcommand();

int mapx;
int mapy;

bool exit = 0;

int main(){
    while (exit == 0){
        waitcommand();
    }
    return 0;
}

void waitcommand(){
    string command;
    cout << ">";
    cin >> command;
    process(command);
}

void process(string com){
    if (com == "help"){
        cout << "Command list: " << endl;
        cout << "walk [N/S/E/W] - move one square in specified direction" << endl;
        cout << "take [object]  - add object to inventory" << endl;
        cout << "use  [object]  - interact with object" << endl;
    }
    else if (com == "exit"){
        char x;
        cout << "Are you sure? y/n ";
        cin >> x;
        if (x == 'y' || x == 'Y'){
            exit = 1;
        }
        else{
            return;
        }
    }
    else{cout << "command '" << com << "' not recognized" << endl;}
}
Last edited on
What you need immediately:
std::getline(): http://www.cplusplus.com/reference/string/getline/
std::istringstream: http://www.artima.com/cppsource/streamstrings3.html


> hopefully learn new things along the way

Hopefully, there are a few more new things here:
std::map<>: http://www.cplusplus.com/reference/map/map/
std::function<>: http://en.cppreference.com/w/cpp/utility/functional/function
std::bind(): http://en.cppreference.com/w/cpp/utility/functional/bind
initializer lists: http://www.informit.com/guides/content.aspx?g=cplusplus&seqNum=424
range based for: http://www.stroustrup.com/C++11FAQ.html#for
type deduction: http://www.stroustrup.com/C++11FAQ.html#auto

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
#include <iostream>
#include <string>
#include <map>
#include <functional>
#include <sstream>

void open_door() { std::cout << "open the door\n" ; /* ... */ }
void take_object() { std::cout << "add object to inventory\n" ; /* ... */ }
void use_object() { std::cout << "do something with object\n" ; /* ... */ }
enum direction { N = 'N', S = 'S', E = 'E', W = 'W' } ;
void walk( direction dir )
{ std::cout << "move one square towards " << char(dir) << '\n' ; /* ... */ }

// lookup maps commands as pair of strings to function
const std::map< std::pair< std::string, std::string >, std::function< void() > > cmd_map =
{
    { { "open", "door" }, open_door },
    { { "take", "object" }, take_object },
    { { "use", "object" }, use_object },
    { { "walk", "n" }, std::bind( walk, N ) },
    { { "walk", "e" }, std::bind( walk, E ) },
    { { "walk", "s" }, std::bind( walk, S ) },
    { { "walk", "w" }, std::bind( walk, W ) }
};

bool process( const std::string& command )
{
    std::istringstream stm(command) ;
    std::string verb, noun ;

    if( stm >> verb >> noun && stm.eof() )
    {
        auto iter = cmd_map.find( std::make_pair( verb, noun ) ) ;
        if( iter != cmd_map.end() ) { iter->second() ; return true ; }
    }

    return false ;

}

int main()
{
    std::string cmd ;
    while( std::cout << "command> " && std::getline( std::cin, cmd ) )
    {
        for( char& c : cmd ) c = std::tolower( c, std::cin.getloc() ) ;
        if( cmd == "exit" ) break ;
        else if( !process(cmd) )
            std::cerr << "do not understand the coomand: '" << cmd << "'\n" ;
    }
}
Thank you. Looks like I have a lot of reading to do.
Topic archived. No new replies allowed.