   Text Interface Question for text based adventure exercise
  Hi there... I am trying to write a small text bases adventure as an exercise to consolidate some of the information I have learnt over the last 5 days or so and was wondering if anyone can help me with a design problem I am facing.Basically how can I get user input and have the program understand it.My original idea was to have collections of word lists in separate functions. Searching these word lists and then using operators on them I could direct the code to seam like the computer understood what the user was saying ....EG:word list array 1)- SHOUTSCREAMYELLHOLLERBELLOWCALLCRYEXCLAIMetc etc etc etcword list array 2) - HELP ASSISTANCE AID HAND SAVED SAVEuser input - Scream for help.I was then planning to convert all the string input from user into uppercase, so as to minimise any typ=o errors from user input. Then test to see if anywhere in the user input are any words in the word lists. In this case the words HELP and SCREAM are both in the word lists.. so by using an && operator I can then test the string and run loops, if statements etc etc etc etc.Well that was the plan.. I am facing a few problems.. I can not seam to figure out how to search a single string. The user input is stored as a single string "Scream for help."... not as separate things in an array like the word list "[Scream], [for], [help], [.]"Is there a way to do this.. or is there a way better way to achieve the same goal. I understand that it isn't perfect and I will be reling on some tricky writting to "suggest" the correct words to use in any given user input....Any help would be appreciated.Jynks   
typedef (*FN_PTR)(void);std::map<std::string, FN_PTR> actions;std::stringstream sentence;std::string word;actions["call"] = Call();actions["exclaim"] = Exclaim();actions["cry"] = Emo();get(std::cin, word, '\n');while(sentence >> word) {    actions[words]();}I don't know if it helps you but you sure can come up to something with that code.   ;  I am very new so pleae be patient.typedef (*FN_PTR)(void);std::map<std::string, FN_PTR> actions;std::stringstream sentence;std::string word;actions["call"] = Call();actions["exclaim"] = Exclaim();actions["cry"] = Emo();get(std::cin, word, '\n');while(sentence >> word) {    actions[words]();}I have never used the >> operator before... it "shifts bits around" but I do not really understand it. Quote:The >> operator shifts the bits of expression1 right by the number of bits specified in expression2. The sign bit of expression1 is used to fill the digits from the left. Digits shifted off the right are discarded. For example, after the following code is evaluated, temp has a value of -4: 14 (11110010 in binary) shifted right two bits equals -4 (11111100 in binary). var temptemp = -14 >> 2Mabey if you explain in words wat this code is doing I might be able to grasp it.   ;  I found this thing but I am not sure how to inplement it.http://www.codeproject.com/string/stringsplit.asp[Edited by - jyujinkai on December 8, 2005 1:25:35 PM];  EDIT: We are assuming C++, here.I'm fairly certain this info is correct, but you should always verify what others tell you. And I'm not the most experienced guy in the world. Well, that's the end of the disclaimer.The >> operator has been overloaded for streams. This means that it does something different in a certain context (for stringstreams, in this case). Stream objects use the shift operators (cout uses <<, cin uses >>) to ouput or input data. White Scorpion creates a stringstream object called "sentence" which can do input and output (like cout and cin) of strings of text (a string is basically a word).while (sentence >> word)This line gets input from the user (like cin does) and stores it in the string called "word". The while loop just means that it will keep getting input until something other than a string is entered.actions[words]();This line calls the function that corresponds to the text entered by the user. actions is an std::map, which is kind of like an array. The index (or key) can be whatever you want, though. Not just numbers. In this case the key is a string, and the data type is a function pointer. You can use the key (whatever word was entered) to call the function that has been mapped to it. For example he has mapped the string "cry" to a function named Emo(). If the user entered "cry" then the function Emo() will be called by this line (the line above, I mean).As for your original question, you will probably want two or three different types of words. At least: verb (action) and noun (object). Read the verb in first, then the noun. Check the verb, if it is "scream" (or "bellow" or "holler", etc.)  then check the noun, if it is "help" (or "aid", "assistance", etc.) then scream for help. You should ignore other words like "the", "a", "for", etc. I wish I could help more, I know that isn't really enough to get going. And I think I see massive nested if blocks in your future.   ;  Hi there... thanks for the tips guys.... I think I have almost got it sorted. here is the code for what I am doing at the moment. It splits up the string and extracts the words you choose.#include &lt;iostream&gt;#include &lt;string&gt;using namespace std;int main (){    string thisIsTheString = "Dream your little dreams.";    cout &lt;&lt; "\nThe String is:- " &lt;&lt; thisIsTheString;    cout &lt;&lt; "\nThe String Size is:- " &lt;&lt; thisIsTheString.size();    cout &lt;&lt; "\nPick a word to find 0 = your -&gt; 2 = dreams.\n";    int wordToFind = 0;    cin &gt;&gt; wordToFind;    const char WHITESPACE = ' ';    int subString_start = thisIsTheString.find_first_of ( WHITESPACE );   while (wordToFind &gt; 0) {   wordToFind--;   subString_start = thisIsTheString.find_first_not_of( WHITESPACE, subString_start );   subString_start = thisIsTheString.find_first_of( WHITESPACE, subString_start );    }    int subString_end = thisIsTheString.find_first_of( WHITESPACE, (subString_start+1) );   cout &lt;&lt; "\nThe final result:\n" &lt;&lt; thisIsTheString.substr( subString_start +1, subString_end - subString_start -1 );    cout &lt;&lt; "\n\n\n\nPlease press enter to exit program.";    cin.ignore(cin.rdbuf()-&gt;in_avail() + 1);return 0;}There is still some improvement I can do to it.. I need to add an if statement to return the first word in the string... also I need to add a bit of code to strip out all (if any) punctuation (. , ; : ' " - + = etc etc etc) but that is not wat I am looking for answers for. Also I need to add the code I have to convert it all to upper case.Like I said I am very new to all this and was wondering how I would set up the function for this thing....Basically I need to input two things into the function... the string I want to test and the number of the word I wish to extract.eg:-string = "Hello, I need some help thanks."findnumber = 1Q - How do I set up a function to accept arguments.. I have only done void ones so far....Also if you can figure it out why is "0" returning the 2nd word in the code I have written.. and is there a way to fix that? I was planning to use a if statement in the function with a return in it to automatically return the first word. And any thoughts as to how I can automaticaly strip out the words into a word list array.. any thoughts on that would be welcome.   ;  I'd like to point out another alternative to you, even if it doesn't directly affect your last question. You're writing something like a subset-of-natural-language parser, by hand. Designing parsers is a relatively well-understood task and we have specialized tools that help with it, namely lex and yacc (or flex and bison). Despite not having much practical experience with these, I know that they're widely used, so I'll dare refer you to them. There is some initial overhead involved with learning how to write definition files for those programs, but once you get the hang of it, it will be easier to expand your text adventure input "language" later on. So the trade-off is more complexity now for more robust code and less complexity later on. (Or so it seems to me)   ;  if you want to find out about functuion-parameters and return-values i suggest you read your C++-book carefully. you got a C++-book don't you? if not checkt the book-section here on GameDev for some good tiltes. find one that sais "... for dummmies" (not offending ;) ) or "... in 21 days" or similar. usually good books for beginners.to convert a string to upper-/lowercase there usually exist a function. in C you would use strupr([const?]char*);. with Borland CB 1.0 there is the AnsiString-Class with a function UpperCase();.your code returning position '0' for the first object in any list of objects is standard behaviour. Arrays are always indexed [0...n-1] for n entries, so the index 1 refers to the second object.   ;  I have got the phraser working now!!! Thanks for the help// Text Phraser Code (temp) [10/12/2005] - Jynks//Takes any user sentence input//removes any punctuation//Converts it to upper case//splits the sentence into individual words//puts each word into a vector array.#include <iostream>#include <string>#include <vector>using namespace std;string userInput();string ParseWord( const string& userInput, int wordNumber );int main (){cout << "Please enter any sentence, you may use punctuation.\n\n";string calledfunction1 = userInput();int wordNumber = 0;string str1 = "temp";vector<string> wordlist;// [START] Put the return value of the user input into a vector araydo{str1 = ParseWord( calledfunction1, wordNumber );wordlist.push_back(str1);++wordNumber;}while (str1 != "");wordlist.pop_back();// [END] Put the return value of the user input into a vector araycout << "\n\nWord Number is: " << (wordNumber - 1) << endl;cout << "\n\nVector contains:\n";for (int i = 0; i < wordlist.size(); ++i)cout << wordlist << endl;cout << "\nVector has these many items - " << wordlist.size();cout << "\n\n\n\nPlease press enter to exit program.";cin.ignore(cin.rdbuf()->in_avail() + 1);return 0;}// [START FUNCTION] (User input) - Gets user input string, removes punctuation, converts to Capsstring userInput(){string str1;getline(cin, str1);for (int j=0; j<str1.length(); ++j){str1[j]=toupper(str1[j]);}int i = str1.find_first_of("`~!@#$%^&*()-_=+|}][{';:/?.>,<\\\"");while (i != string::npos){    str1.erase(i, 1);    i = str1.find_first_of("`~!@#$%^&*()-_=+|}][{';:/?.>,<\\\"");}return str1;}// [END FUNCTION] (User Input) - Gets user input string, removes punctuation, converts to Caps// [START FUNCTION] (Phrase Word) - Returns a single word from sentence string//userInput = Sentence String from user//wordNumber = Number of word in string to findstring ParseWord( const string& userInput, int wordNumber ){const string WHITESPACE = " ";    int string_start = userInput.find_first_not_of( WHITESPACE );while( wordNumber > 0 ) {   wordNumber--;   string_start = userInput.find_first_of( WHITESPACE, string_start );   string_start = userInput.find_first_not_of( WHITESPACE, string_start );    }int string_end = userInput.find_first_of( WHITESPACE, string_start );if( string_start == string::npos ) {   string_start = 0;   string_end = 0;    }   return userInput.substr( string_start, string_end - string_start );}// [END FUNCTION] (Phrase Word) - Returns a single word from sentence stringI probably will not post for a bit as I have to start at the bottom of figuring out how to look around a room and do things and conpare vector arrays to each other and then do things is items found etc etc... but I will keep you posted.   