Here's my question:
Below, find my code. I'm having trouble finding compact and efficient ways to compare strings stored by the user into an array. You'll see it plainly in the "DetermineVerb" and "DetermineAdj" functions.
Any suggestions will be greatly appreciated.
#include <iostream.h> #include <stdlib.h> #include <string.h> #include <iomanip.h> enum ActionType {WALK}; //enum type for lable enum DirectionType {EAST, NORTH, SOUTH, WEST}; //enum type for lable void PrintHeader(); void GetVerb(char[]); void DetermineVerb(char[], char[], int&, int&, ActionType&, DirectionType&, int&); void GetAdj(char[]); void DetermineAdj(char[], ActionType&, DirectionType&, int&, int&); void CheckWalk(int&, DirectionType&, int&); int main() { typedef int Boolean; //////////////////////////////////////////////// Boolean quit = false; // Boolean walkOK = false; // char verb[20]; // variable char adj[20]; // declarations int currPosition = 9; // ActionType action; // DirectionType direction; //////////////////////////////////////////////// PrintHeader(); while (!quit) { GetVerb(verb); DetermineVerb(verb, adj, quit, currPosition, action, direction, walkOK); } cout<<endl<<endl; system("PAUSE"); return 0; } //****************************************************************************** //This function is self-explanatory //****************************************************************************** void PrintHeader() { cout<<" ****************************************************************"<<endl; cout<<" This is a 3 x 3 tiled room."<<endl<<endl; cout<<" -------------------------------"<<endl; cout<<" | | | |"<<endl; cout<<" | 1 | 2 | 3 | Commands:"<<endl; cout<<" | | | | --------"<<endl; cout<<" ------------------------------ WALK NORTH"<<endl; cout<<" | | | | WALK EAST"<<endl; cout<<" | 4 | 5 | 6 | WALK SOUTH"<<endl; cout<<" | | | | WALK WEST"<<endl; cout<<" ------------------------------ QUIT"<<endl; cout<<" | | | |"<<endl; cout<<" | 7 | 8 | 9 |"<<endl; cout<<" | | | |"<<endl; cout<<" ------------------------------"<<endl<<endl; cout<<" ****************************************************************"<<endl<<endl; } //****************************************************************************** // Prompt user for verb string //****************************************************************************** void GetVerb(char verb[]) { cout<<"Enter command: "; cin>>verb; } //****************************************************************************** // Use "switch" to interpret verb string by first 2 letters (i need help here) //****************************************************************************** void DetermineVerb(char verb[], char adj[], int& quit, int& currPosition, ActionType& action, DirectionType& direction, int& walkOK) { switch (verb[0]) { case 'W': switch (verb[1]) { case 'A': action = WALK; GetAdj(adj); DetermineAdj(adj, action, direction, currPosition, walkOK); break; } break; case 'Q': quit = true; break; default : cout<<"not a verb"<<endl; break; } } //****************************************************************************** // Prompt user for adjective string //****************************************************************************** void GetAdj(char adj[]) { cin>>adj; } //****************************************************************************** // Use "switch" and "if" to interpret adjective string (need help here too) //****************************************************************************** void DetermineAdj(char adj[], ActionType& action, DirectionType& direction, int& currPosition, int& walkOK) { switch (adj[0]) { case 'E': switch (adj[1]) { case 'A': if (action == WALK) { direction = EAST; CheckWalk(currPosition, direction, walkOK); if (walkOK) currPosition++; //adjust position in matrix else { cout<<setw(45)<<"--------------------"<<endl; cout<<setw(45)<<"***You hit a wall***"<<endl; cout<<setw(45)<<"--------------------"<<endl; } } break; } break; case 'N': switch (adj[1]) { case 'O': if (action == WALK) { direction = NORTH; CheckWalk(currPosition, direction, walkOK); if (walkOK) currPosition -= 3; //adjust position in matrix else { cout<<setw(45)<<"--------------------"<<endl; cout<<setw(45)<<"***You hit a wall***"<<endl; cout<<setw(45)<<"--------------------"<<endl; } } break; } break; case 'S': switch (adj[1]) { case 'O': if (action == WALK) { direction = SOUTH; CheckWalk(currPosition, direction, walkOK); if (walkOK) currPosition += 3; //adjust position in matrix else { cout<<setw(45)<<"--------------------"<<endl; cout<<setw(45)<<"***You hit a wall***"<<endl; cout<<setw(45)<<"--------------------"<<endl; } } break; } break; case 'W': switch (adj[1]) { case 'E': if (action == WALK) { direction = WEST; CheckWalk(currPosition, direction, walkOK); if (walkOK) currPosition--; //adjust position in matrix else { cout<<setw(45)<<"--------------------"<<endl; cout<<setw(45)<<"***You hit a wall***"<<endl; cout<<setw(45)<<"--------------------"<<endl; } } break; } break; } } //****************************************************************************** // Checks for collision //****************************************************************************** void CheckWalk(int& currPosition, DirectionType& direction, int& walkOK) { switch (currPosition) { case 1: if (direction == EAST || direction == SOUTH) walkOK = true; else walkOK = false; break; case 2: if (direction == EAST || direction == SOUTH || direction == WEST) walkOK = true; else walkOK = false; break; case 3: if (direction == SOUTH || direction == WEST) walkOK = true; else walkOK = false; break; case 4: if (direction == EAST || direction == NORTH || direction == SOUTH) walkOK = true; else walkOK = false; break; case 5: walkOK = true; break; case 6: if (direction == NORTH || direction == SOUTH || direction == WEST) walkOK = true; else walkOK = false; break; case 7: if (direction == EAST || direction == NORTH) walkOK = true; else walkOK = false; break; case 8: if (direction == EAST || direction == NORTH || direction == WEST) walkOK = true; else walkOK = false; break; case 9: if (direction == NORTH || direction == WEST) walkOK = true; else walkOK = false; break; } } //******************************************************************************