Hi, I have been working on a little text based interactive fiction game for a decent amount of time, but I just can't seem to figure out how I can cut up strings so that my program can understand simple commands. Is there a simple way to do this?

For instance, I want to be able to give a string to a function (a class might work better) like "get apple" so that my program can figure out that I want to use a "get" command on a "apple" item(if found). It doesn't have to be very complicated, I'm probably going to have the command only accept at most 3 words.

I've tried making a for loop so that it tries to find the first space in the string, then copy all the characters up to that point, but it just doesn't seem to be working at all. Plus it only captures the first word, so the end result won't help much in the first place.

If you want to see it however, here it is, I honestly want to just start from scratch again though.

string getFirstWord(string text)
{
       bool firstWordFound = false;
       string firstWord = "";
       
       for(int i = 0; i <= text.length() && firstWordFound == false; i++)
       {
             if (text[i] = ' ')
             {
                         firstWordFound = true;
                         
                         for (int j = 0; j <= i; j++)
                         {
                             firstWord += text[j];
                         }
             }
       }
       
       return firstWord;
}

Edited 5 Years Ago by w1mark: n/a

Since you are using a C++ string, use the string functions .find(), .substr(), .erase()

With a simple GOOGLE search I found this

Well, here is what I would basically do, it takes advantage of some of the methods that std::string contains:

string *BreakUp(string in, int *num)//This translates a command into an array of strings, num will become the length of said array
{
    int numWords=0;//This is the number of words in the command
    for (int i=0; i<in.length(); i++)//loop through the word
    {
        if (in[i]==' ')//if it is a strength
        {
            numWords++;
        }
    }
    *num=numWords;//set num
    string *ret=new string[numWords];//Create the return vector
    for (int i=0; i<numWords; i++)
    {
        ret[i]=in.substr(0, in.find_first_of(' '));//Grab the first word
        in=in.substr(in.find_first_of(' ')+1);//Strip the first word from the string
    }
    return ret;//return the array of strings
}

Of course you would have to take over from there though.

EDIT: DANG WALTP BEAT ME WHILE I WAS WRITING THE CODE!

Edited 5 Years Ago by Labdabeta: WaltP Answered while I was writing the code :(

If I'm understanding the problem correctly the solution is pretty simple.

want all your "words" in the string in different strings or something?

#include <iostream>
#include <string>
#include <sstream>
#include <vector>

typedef unsigned int uint;

//Returns, Num. words.
uint getWords(std::string &in, std::vector<std::string> &words){
	uint count = 0;
	std::stringstream ss(in);
	std::string currentWord;
	while( ss >> currentWord ){
		words.push_back(currentWord);
		count++;
	}
	return count;
}

int main()
{
	std::string commandData = "GET all_my_data IN all_the_base base... base.. base";
	std::vector<std::string> words;
	uint wordCount = getWords(commandData,words);

	std::cout << "Word Count: " << wordCount << std::endl;
	for(std::vector<std::string>::iterator it = words.begin(); it != words.end(); ++it){
		std::cout << *it << std::endl;
	}
}

Really it can be reduced to:

std::stringstream ss(someString);
std::string temp;
while( ss >> temp )
{
//..
}
Comments
If he can't use a STRING yet, how can he use a VECTOR?

I knew I forgot about some of the string functions and it's been helpful, however the code you've shown me doesn't really help as much as there are some things that I don't understand yet.(like vectors)

Anyways, I think I got farther than I was two days ago, but Im still having problems.

Just to start out, I decided to just make a program that prints out each word individually in a string.(Later on I will implement it into my IF game) However, it acts up when I enter 3 words or more, im not sure why it just takes part of the next word with it when I don't want it to.

For instance if I put in:
"red green blue yellow"
it gives me:
"red | blue gre | green yellow | yellow"

(The "|" showing where the words are divided into substrings)

Heres the code, can you help me why it's doing this?

#include <iostream>
#include <string>
using namespace std;

int main()
{
    string command;
    
    getline(cin, command);
    int wordStart = 0;
    int wordCount = 1;
    
    cout << endl;
    for(int wordEnd = 0; wordEnd <= command.length(); wordEnd++)
    {
            if (command[wordEnd] == ' ' || wordEnd == command.length())
            {
                                 cout << command.substr(wordStart, wordEnd) << " | ";
                                 wordCount++;
                                 wordStart = wordEnd + 1;
            }
    } 
    
    cout << endl;
    system("pause");
    return 0;
}

Edited 5 Years Ago by w1mark: n/a

pseudorandom21 solution is clean and simple, spend some time to understand it

#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::stringstream strm("it your move");

    std::string token;
    while(strm >> token)
    {
        std::cout << token << std::endl;
    }

    return 0;
}

Edited 5 Years Ago by template<>: n/a

What are the values of wordStart, wordEnd and command[wordEnd] during the loop? cout is your best debugging tool.

Hmmm, well I have been looking at this problem for a while, and I found out a way to do it pretty efficently using just the iostream and string libraries. My solution allows you to get the "Xth" word by every time a space is found, it deletes up to that space, untill you get to the word you were looking for and returns it. Alot simpler than I expected. The best part is it allows me to parse as many words as I want, and if theres not enough words it just returns an empty string.

I guess it just takes a little thinking outside of the box sometimes. Thanks for the help anyways.

Here's the function if you want to look at it.

string getXWord(int wordCount, string command)
{
       string word;
       
       for(int i = 1; i < wordCount; i++)
       {
               command.erase(0, command.find(' ')+ 1);
       }
       
       word = command.substr(0, command.find(' '));
       
       return word;
}

How do you mark threads as solved?

Edited 5 Years Ago by w1mark: n/a

This question has already been answered. Start a new discussion instead.