Hi Guys
I have decided to make my own version of the turtle logo graphics program but and I have simplified it by removing the turn left and right commands. Instead the user enters the amount of movements they wish to make and then enter the x and y coordinates for each movement.
It compiles ok but when I attempt to go outside of the boundarys of the array I am getting a seg-fault ( which i should do ) but it is not reading the if statement.
Here is my code
Code:#include <iostream> #include <string> // function prototypes int mainMenu ( char[][ 20 ] ); std::string raisePenUp ( bool& ); std::string lowerPenDown ( bool& ); void moveTurtle ( char[][ 20 ], bool& ); void printArray ( char[][ 20 ] ); // main function - driver ////////////////////////////////////////////////////// // int main ( void ) { const int MAX_ROW = 20; const int MAX_COL = 20; char floor[ MAX_ROW ][ MAX_COL ] = {{ 0, 0 }}; mainMenu ( floor ); std::cin.get(); // freeze console output window return 0; // return value from int main } // function to display the main menu int mainMenu ( char flr[][ 20 ] ) { // pen position. True is raised up False is lowered down bool penPosition = true; int choice = true; do { std::cout << "\tMain Menu\n\n" << "Enter 1 to raise the pen up\n" << "Enter 2 to lower the pen down\n" << "Enter 5 to move the turtle\n" << "Enter 6 to print the array\n" << "Enter 9 to quit the program\n\n" << "> "; std::cin >> choice; switch ( choice ) { case 1: std::cout << raisePenUp ( penPosition ); break; case 2: std::cout << lowerPenDown ( penPosition ); break; case 5: moveTurtle ( flr, penPosition ); break; case 6: printArray ( flr ); break; case 9: return 0; break; default: break; } } while ( choice ); } // function to raise the pen upwards so the turtle can // move freely along the board std::string raisePenUp ( bool &rPp ) { std::string str1 = "The pen has been raised up...\n\n"; std::string str2 = "The pen is already rasied up!\n\n"; if ( rPp == true ) { return str2; } else if ( rPp == false ) { rPp = true; return str1; } } // function to lower the pen down so the turtle can // draw the shapes onto the board std::string lowerPenDown ( bool &rPp ) { std::string str1 = "The pen has been lowered down\n\n"; std::string str2 = "The pen is already lowered down!\n\n"; if ( rPp == false ) { return str2; } else if ( rPp == true ) { rPp = false; return str1; } } // function to move turtle void moveTurtle ( char flr[][ 20 ], bool &rPp ) { int xCoord = 0; int yCoord = 0; short totalMoves = 0; if ( rPp == true ) { std::cout << "\nYou cannot draw while the pen is raised up\n\n"; } else if ( rPp == false ) { std::cout << "\n\nHow many moves will the turtle make?: "; std::cin >> totalMoves; for ( int i = 0; i < totalMoves; i++ ) { std::cout << "Enter x coordinate for movement " << i << ": "; std::cin >> xCoord; if (( xCoord < 0 ) || ( xCoord > 20 )) { std::cerr << "\nPlease keep within 0 - 20\n"; std::cout << "Enter x coordinate for movement " << i << ": "; std::cin >> xCoord; } std::cout << "Enter y coordinate for movement " << i << ": "; std::cin >> yCoord; // this is being ignored by the program if (( yCoord < 0 ) || ( yCoord > 20 )) { std::cerr << "\nPlease keep within 0 - 20\n"; std::cout << "Enter y coordinate for movement " << i << ": "; std::cin >> yCoord; } flr[ xCoord ][yCoord ] = '#'; } } } // function to print the array showing the patterns made by // the turtle controlled by the commands from the user void printArray ( char flr[][ 20 ] ) { for ( int i = 0; i < 20; i++ ) { for ( int j = 0; j < 20; j++ ) std::cout << flr[ i ][ j ]; std::cout << std::endl; } }



LinkBack URL
About LinkBacks


