Simple turtle problem

This is a discussion on Simple turtle problem within the C++ Programming forums, part of the General Programming Boards category; Hi Guys I have decided to make my own version of the turtle logo graphics program but and I have ...

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475

    Simple turtle problem

    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;
       }
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Don't worry I solved the problem. I changed "if" to "while" so it looped untill the input was inside the array bounds.
    I'm just trying to be a better person - My Name Is Earl

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    It's not being ignored, as far as I can tell; if you enter a number out of bounds you get one chance to fix it (since it's an if, not a while).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A simple file I/O problem
    By eecoder in forum C Programming
    Replies: 10
    Last Post: 10-16-2010, 11:00 PM
  2. Problem in simple code.
    By richdb in forum C Programming
    Replies: 6
    Last Post: 03-20-2006, 01:45 AM
  3. Problem in very simple code
    By richdb in forum C Programming
    Replies: 22
    Last Post: 01-14-2006, 08:10 PM
  4. Simple File I/O problem
    By Ignited in forum C++ Programming
    Replies: 3
    Last Post: 01-07-2006, 09:49 AM
  5. turtle graphics problem??
    By atif in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2002, 12:48 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21