#include using namespace std; /** * Process an R (right) or L (left) command. * /param direction If this is R, then interprets as right turn, * else if it is any other value, process a left turn. * /param text The entire command string. * /param pos The position in the command string. This should be at * the character immediately after the R or L character. */ bool processRorL(char direction, string text, int& pos) { // Get distance string distance = ""; while (isdigit(text[pos])) // THERE'S A BUG HERE! { distance += text[pos]; pos++; } if (distance == "") // there were no digits return false; // Write full instruction cout << "Turn "; if (direction == 'R') cout << "right"; else cout << "left"; cout << " and proceed for " << distance << " blocks." << endl; return true; } /** * Process a stop command. It is expected that there is a single upper case character * immediately following the S character at test[pos] * * /param direction This is ignored. Perhaps it should not be here? Or maybe * this function could be combined with processRorL? * /param text The entire command string. * /param pos The position in the command string. This should be at * the character immediately after the S character. */ bool processS(char direction, string text, int& pos) { // Check to see if we are at the end of the string. // Hint: is anything like this done in processRorL? if (pos == text.size()) { return false; } // is this character lower case? // For your purposes, you may want to use toupper or tolower if (!isupper(text[pos])) { return false; } cout << "Stop at point of interest " << text[pos] << "." << endl; pos++; return true; } /** * This loops through the command string and calls the necessary functions */ bool translateInstructionString(string text) { int pos = 0; while (pos != text.size()) { // Get instruction code char direction = text[pos]; pos++; switch (direction) { case 'R': case 'L': if (!processRorL(direction, text, pos)) return false; break; case 'S': if (!processS(direction, text, pos)) return false; break; default: return false; } } } /* * Instructions for following sightseeing directions by foot: * R12 Turn right and proceed for 12 blocks * L3 Turn left and proceed for 3 blocks * SC Stop at point of interest C [upper case letter] * Input is full instruction string: * R12L3SAL10R5SB * Output is sequence of actions spelled out: * Turn right and proceed for 12 blocks. * Turn left and proceed for 3 blocks. * Stop at point of interest A. * Turn left and proceed for 10 blocks. * Turn right and proceed for 5 blocks. * Stop at point of interest B. */ int main() { string command("R12L3SAL10R5SB"); cout << endl << "Command: " << command << endl << endl; translateInstructionString(string(command)); return 0; }