Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // old code //
- void readInput (char* &command, const int MAXL, string &noun, string &verb) // take in the user's command
- {
- int i = 0; ///< iterator 1
- int n = 0; ///< iterator 2
- char cstrnoun[MAXL]; ///< cstring for the inbound noun
- char cstrverb[MAXL]; ///< cstring for the inbound verb
- char *nounptr = cstrnoun; // cstring pointer, noun
- char *verbptr = cstrverb; // cstring pointer, verb
- int inputLength; // total length of the string
- cin.clear();
- cout << "\nPlease enter your command. Use a verb followed by a noun such as 'ask name.'" << endl
- << "> ";
- cin.getline(command,MAXL); ///< get your command
- inputLength = cin.gcount(); ///< how long was last input?
- cout << "\nYour command was: " << command << endl;
- // Parse the command into a noun or a verb.
- do {
- cstrverb[i]=command[i];
- i++;
- } while (command[i] != ' ');
- cstrverb[i] = '\0'; // place a delimiter character at the end of the noun
- i++;
- do {
- cstrnoun[n]=command[i];
- i++;
- n++;
- } while (i < inputLength);
- for (i=0;i<n;i++) {
- if (cstrnoun[i]==' ') {
- cstrnoun[i] = '\0';
- i=n;
- }
- }
- noun = nounptr;
- verb = verbptr;
- }
- // new code //
- ParseCmd::ParseCmd(string command) {
- regex ex("^\\s*(((?<verb>[a-z]*)\\s(?<noun>[a-z]*)|((?<verb>[a-z]*))))\\s*$");
- smatch what;
- if (regex_match(command,what,ex)) {
- noun = what["noun"];
- verb = what["verb"];
- valid = true;
- } else {
- valid = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement