Advertisement
KeithS

text adventure listing part 1

Nov 4th, 2012
2,721
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <vector>  // For the command handling function.
  4. #include <cctype>  // Will be used to eliminate case sensitivity problems.
  5.  
  6. using namespace std;
  7.  
  8. // -------------------------------------------------------------------------------------------------
  9.  
  10. void section_command(string Cmd, string &wd1, string &wd2)
  11. {
  12.     string sub_str;
  13.     vector<string> words;
  14.     char search = ' ';
  15.     size_t i, j;
  16.  
  17.     // Split Command into vector
  18.     for(i = 0; i < Cmd.size(); i++)
  19.     {
  20.         if(Cmd.at(i) != search)
  21.         {
  22.             sub_str.insert(sub_str.end(), Cmd.at(i));
  23.         }
  24.         if(i == Cmd.size() - 1)
  25.         {
  26.             words.push_back(sub_str);
  27.             sub_str.clear();
  28.         }
  29.         if(Cmd.at(i) == search)
  30.         {
  31.             words.push_back(sub_str);
  32.             sub_str.clear();
  33.         }
  34.     }
  35.     // Clear out any blanks
  36.     // I work backwords through the vectors here as a cheat not to invaldate the iterator
  37.     for(i = words.size() - 1; i > 0; i--)
  38.     {
  39.         if(words.at(i) == "")
  40.         {
  41.             words.erase(words.begin() + i);
  42.         }
  43.     }
  44.     // Make words upper case
  45.     // Right here is where the functions from cctype are used
  46.     for(i = 0; i < words.size(); i++)
  47.     {
  48.         for(j = 0; j < words.at(i).size(); j++)
  49.         {
  50.             if(islower(words.at(i).at(j)))
  51.             {
  52.                 words.at(i).at(j) = toupper(words.at(i).at(j));
  53.             }
  54.         }
  55.     }
  56.     // Very simple. For the moment I only want the first to words at most (verb / noun).
  57.     if(words.size() == 0)
  58.     {
  59.         cout << "No command given" << endl;
  60.     }
  61.     if(words.size() == 1)
  62.     {
  63.         wd1 = words.at(0);
  64.     }
  65.     if(words.size() == 2)
  66.     {
  67.         wd1 = words.at(0);
  68.         wd2 = words.at(1);
  69.     }
  70.     if(words.size() > 2)
  71.     {
  72.         cout << "Command too long. Only type one or two words (direction or verb and noun)" << endl;
  73.     }
  74. }
  75.  
  76. // ----------------------------------------------------------------------------------------
  77.  
  78. int main()
  79. {
  80.     string command;
  81.     string word_1;
  82.     string word_2;
  83.    
  84.     while(word_1 != "QUIT") // I have provided an escape condition from the loop here
  85.     {
  86.         command.clear();
  87.         cout << "What shall I do? ";
  88.         getline(cin, command);
  89.         cout << "Your raw command was " << command << endl;
  90.  
  91.         word_1.clear();
  92.         word_2.clear();
  93.  
  94.         // Call the function that handles the command line format.
  95.         section_command(command, word_1, word_2);
  96.        
  97.         // For test purposes, output the command after formatting by the function.
  98.         if(word_1.size() > 0)
  99.         {
  100.             cout << word_1 << " " << word_2 << endl;
  101.         }
  102.         cout << endl;
  103.        
  104.     }
  105.     return 0;
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement