/** roomba2.cpp 31-jan-2007/sklar this program expands on roomba.cpp by allowing the user to enter commands telling the robot to go forward, backward, left or right. */ // section 1: include C++ library definitions #include using namespace std; // section 2: declare variables int x; // robot's x position int y; // robot's y position char c; // user's input bool q; // does user want to quit? // section 3: declare methods int display() { cout << "the roomba is at location ("; cout << x; cout << ","; cout << y; cout << ")\n"; return 0; } // end of display() int moveForward() { cout << "moving forward...\n"; y = y + 1; return 0; } // end of moveForward() int moveBackward() { cout << "moving backward...\n"; y = y - 1; return 0; } // end of moveBackward() int moveLeft() { cout << "moving left...\n"; x = x - 1; return 0; } // end of moveLeft() int moveRight() { cout << "moving right...\n"; x = x + 1; return 0; } // end of moveRight() // section 4: define main method int main() { x = 0; y = 0; q = false; while ( q==false ) { cout << "which way should roomba move (enter F,B,L,R or Q)? "; cin >> c; cout << "you entered: "; cout << c; cout << "\n"; if ( c=='F' ) { moveForward(); display(); } else if ( c=='B' ) { moveBackward(); display(); } else if ( c=='L' ) { moveLeft(); display(); } else if ( c=='R' ) { moveRight(); display(); } else if ( c=='Q' ) { q = true; } else { cout << "oops! you entered something invalid. please try again :-)\n"; } } // end while q==false } // end of main()