2

I'm in my first semester of university and I have to make a text adventure game in c++. So far we've done arrays, structs, and pointers. I've tried to google my problem, however most other users use classes which we have not yet done.

The professor would like us to use commands like Go North, open door with key etc.
I've managed to make it work by using hotkeys like n to go north, but obviously I would like to do it like he wants us to.

So my question is; how can I make a command consisting of several strings?

The problem is that we need to create libraries for the command, the object and (if there is the possibility in this room to combine two things) the preposition with another object. In each library there should be the words to use, for example: Commands are: Use, go, talk, read, etc...

Textmode
  • 509
  • 3
  • 18

5 Answers5

1

Taken from http://www.cplusplus.com/reference/iostream/istream/getline/

#include <iostream>
using namespace std;

int main () {
  char name[256], title[256];

  cout << "Enter your name: ";
  cin.getline (name,256);

  cout << "Enter your favourite movie: ";
  cin.getline (title,256);

  cout << name << "'s favourite movie is " << title;

  return 0;
}
zmf
  • 9,095
  • 2
  • 26
  • 28
0

Try search for the getline or scanf function. Both reads a formatted string from the stdin(in your case the prompt command). You could use cin directly too, the problem is that cin return a string composed of the characters until the first whitespace.

kist
  • 590
  • 2
  • 8
  • The problem is that we have to create libraries for the command, the object and (if there is the possibility in this room to combine two things) the preposition with another object. In each library there should be the words to use, for example: Commands are: Use, go, talk, read, etc... – Michael Blatz Dec 07 '11 at 17:42
0

If you're asking how to read the multi-word commands, then you can use getline() to read a line of input as a string, and then a stringstream to read each word from that line, something like this:

std::string line;
while (std::getline(std::cin, line)) {
    std::istringstream stream(line);
    std::string word;
    while (stream >> word) {
        // do something with this word
    }
}
Mike Seymour
  • 249,747
  • 28
  • 448
  • 644
0
  1. Use something like std::string input << std::cin; to input a line of text.
  2. Split your string by whitespace/the space character.

Next steps:

  1. Do command tokenization into verb/noun pairs, like Open/Door and Go/North
  2. Make classes that represent your verbs, and design them to operate on objects or specific nouns

Some subjects to look up:

Community
  • 1
  • 1
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
0

I guess you are using cin to read the commands? In that case get input into a string, use find to find the spaces in it, and substr to extract the command and its arguments. Try to convert all substrings to either lower- or upper-case, as it will be easier to compare them later.

Now take the command substring and compare it to all commands you have. When you find a match, call a special function that executes the command, passing the arguments to the function. E.g., for the "go" command you could have a go function, which takes the direction as argument.

Some programmer dude
  • 400,186
  • 35
  • 402
  • 621