Advertisement
squelch

Untitled

Dec 5th, 2014
191
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.75 KB | None | 0 0
  1. // old code //
  2. void readInput (char* &command, const int MAXL, string &noun, string &verb) // take in the user's command
  3. {
  4.     int     i = 0;      ///< iterator 1
  5.     int     n = 0;      ///< iterator 2
  6.    
  7.     char    cstrnoun[MAXL];         ///< cstring for the inbound noun
  8.     char    cstrverb[MAXL];         ///< cstring for the inbound verb
  9.     char    *nounptr = cstrnoun;    // cstring pointer, noun
  10.     char    *verbptr = cstrverb;    // cstring pointer, verb
  11.     int     inputLength;            // total length of the string
  12.        
  13.     cin.clear();
  14.     cout    << "\nPlease enter your command. Use a verb followed by a noun such as 'ask name.'" << endl
  15.             << "> ";
  16.     cin.getline(command,MAXL);          ///< get your command
  17.     inputLength = cin.gcount();         ///< how long was last input?
  18.     cout    << "\nYour command was: " << command << endl;
  19.        
  20.     // Parse the command into a noun or a verb.
  21.    
  22.     do {
  23.         cstrverb[i]=command[i];        
  24.         i++;
  25.     } while (command[i] != ' ');
  26.     cstrverb[i] = '\0';             // place a delimiter character at the end of the noun
  27.     i++;
  28.    
  29.     do {
  30.         cstrnoun[n]=command[i];
  31.         i++;
  32.         n++;
  33.     } while (i < inputLength);
  34.     for (i=0;i<n;i++) {
  35.         if (cstrnoun[i]==' ') {
  36.             cstrnoun[i] = '\0';
  37.             i=n;
  38.         }
  39.     }          
  40.     noun = nounptr;
  41.     verb = verbptr;
  42. }
  43.  
  44. // new code //
  45.  
  46. ParseCmd::ParseCmd(string command) {
  47.     regex ex("^\\s*(((?<verb>[a-z]*)\\s(?<noun>[a-z]*)|((?<verb>[a-z]*))))\\s*$");
  48.     smatch what;
  49.     if (regex_match(command,what,ex)) {
  50.         noun = what["noun"];
  51.         verb = what["verb"];
  52.         valid = true;
  53.     } else {
  54.         valid = false;
  55.     }
  56.    
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement