Advertisement
KeithS

Console text command input

Oct 25th, 2012
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.86 KB | None | 0 0
  1. // WK Shearer Oct 2012.
  2. // Simple console application, C++.
  3. // Sample command interface, like a receptor for text based adventure game.
  4. // Takes a command, splits it into words, eliminates blanks and spaces, and
  5. // converts the words to upper case for later comparison to verb / noun bank.
  6. #include <iostream>
  7. #include <string>
  8. #include <cctype>
  9. #include <vector>
  10.  
  11. using namespace std;
  12.  
  13. int main()
  14. {
  15.     string mystr;
  16.     char search = ' ';
  17.     string sub_str;
  18.     vector<string> words;
  19.  
  20.     cout << "Enter command: ";
  21.     getline(cin, mystr);
  22.     cout << "Command is " << mystr << endl;
  23.     //cout << mystr.length() << endl;
  24.     size_t i = 0, j = 0;
  25.  
  26.     for(i = 0; i < mystr.size(); i++)
  27.     {
  28.         if(mystr.at(i) != search)
  29.         {
  30.             sub_str.insert(sub_str.end(), mystr.at(i));
  31.         }
  32.         if(i == (mystr.size() - 1))
  33.         {
  34.             words.push_back(sub_str);
  35.             sub_str.clear();
  36.         }
  37.         if(mystr.at(i) == search)
  38.         {
  39.             words.push_back(sub_str);
  40.             sub_str.clear();
  41.         }
  42.     }
  43.     // clean out any accidental blank words (extra spaces)...
  44.     //cout << "initial words.size() " << words.size() << endl;
  45.     for(i = words.size() - 1; i > 0; i--)
  46.     {
  47.         if(words.at(i) == "")
  48.             words.erase(words.begin() + i);
  49.     }
  50.     //cout << "words.size() after clean up " << words.size() << endl;
  51.  
  52.     for(i = 0; i < words.size(); i++)
  53.     {
  54.         cout << words.at(i) << endl;
  55.     }
  56.  
  57.     // Make words upper case
  58.     for(i = 0; i < words.size(); i++)
  59.     {
  60.         for(j = 0; j < words.at(i).size(); j++)
  61.         {
  62.             if(islower(words.at(i).at(j)))
  63.                 words.at(i).at(j) = toupper(words.at(i).at(j));
  64.         }
  65.     }
  66.  
  67.     for(i = 0; i < words.size(); i++)
  68.     {
  69.            cout << words.at(i) << endl;
  70.     }
  71.  
  72.     return 0;
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement