Tutle graphic pen problem

This is a discussion on Tutle graphic pen problem within the C++ Programming forums, part of the General Programming Boards category; Hey guys I am in the very early stages of attempting the infimous Logo program in C++ using the imaginary ...

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

    Tutle graphic pen problem

    Hey guys

    I am in the very early stages of attempting the infimous Logo program in C++ using the imaginary turtle.

    What I have compiles ok, but when I am testing if the pen is rasied up or down I am getting the wrong messages returned by the function. Oh and is it safe to use references on boolean variables like I have done? My compiler didn't complain. I only did this to ensure I got the right string returned.

    Basically, if the pen is down, and i ask it to raise it up it only returns "The pen is alresdy up!" when i want it to return "The pen is raised up"

    Here is all I have so far. I just want to get this part working ok first before I get on with the more complex part using the 2d array.

    Code:
    #include <iostream>
    #include <string>
    
    // function prototypes
    int displayMenu ( const int[][ 20 ] );
    std::string checkPenUp ( bool& );
    std::string checkPenDown ( bool& );
    
    // main - begins program execution /////////////////////////////////////////////
    //
    int main(int argc, char *argv[])
    {
       const int MAX_ROW = 20;
       const int MAX_COL = 20;
    
       const int board[ MAX_ROW ][ MAX_COL ] = { 0 };
    
       displayMenu ( board );
    
       std::cin.get(); // freeze console output window
    
       return 0; // return value from main to OS
    }
    
    // function to display main options menu
    int displayMenu ( const int brd[][ 20 ] )
    {
       int choice;
       bool penPos = false; // pen is raised up to start with
    
       do
       {
          std::cout << "\tSelect an option:\n\n"
                    << "1 Raise Pen Up\n"
                    << "2 Lower Pen Down\n"
                    << "3 Turn Right\n"
                    << "4 Turn Left\n"
                    << "5 Move By Entered Values\n"
                    << "6 Print Image\n"
                    << "9 Quit Program\n";
          std::cin >> choice;
    
          switch ( choice )
          {
          case 1:
             std::cout << checkPenUp ( penPos );
             break;
    
          case 2:
             std::cout << checkPenDown ( penPos );
             break;
    
          case 3:
             break;
    
          case 4:
             break;
    
          case 5:
             break;
    
          case 6:
             break;
    
          case 9:
             return 0;
             break;
    
          default:
             break;
          }
    
       } while ( choice );
    }
    
    // function to check if pen is raised up
    std::string checkPenUp ( bool &rPen )
    {
       std::string str1 = "The pen has been raised up\n";
       std::string str2 = "The pen is already raised up!\n";
    
       if ( rPen == false )
       {
          return str2;
       }
    
       else
       {
          rPen = true;
          return str1;
       }
    }
    
    // function to check if pen is raised down
    std::string checkPenDown ( bool &rPen )
    {
       std::string str1 = "The pen has been placed down\n";
       std::string str2 = "The pen is already placed down!\n";
    
       if ( rPen == true )
       {
          return str2;
       }
    
       else
       {
          rPen = false;
          return str1;
       }
    }
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,892
    Code:
     if ( rPen == false )
       {
          return str2;
    rPen is false if the pen is down. Or at least that's what the setter logic of checkPenUp/Down says. The comment in the menu function and your test logic says otherwise.

    Sort that stuff out. You will want to rename the variable to something that clearly indicates the meaning of true.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,475
    Thanks CornedBee that helped! I changed the main vaible to:

    Code:
    penIsUp = true;
    Then I made these modifications and its all working now

    Code:
    // function to check if pen is raised up
    std::string checkPenUp ( bool &rPen )
    {
       std::string str1 = "The pen has been raised up\n";
       std::string str2 = "The pen is already raised up!\n";
    
       // if pen is already up
       if ( rPen == true )
       {
          return str2;
       }
    
       // if pen is down raise up
       else if ( rPen == false )
       {
          rPen = true;
          return str1;
       }
    }
    
    // function to check if pen is raised down
    std::string checkPenDown ( bool &rPen )
    {
       std::string str1 = "The pen has been placed down\n";
       std::string str2 = "The pen is already placed down!\n";
    
       // if pen is up place it down
       if ( rPen == true )
       {
          rPen = false;
          return str1;
       }
    
       // if pen is already down
       else if ( rPen == false )
       {
          return str2;
       }
    }
    I'm just trying to be a better person - My Name Is Earl

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 02:51 PM
  2. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 06:54 PM
  3. Graphic problem
    By kara in forum Linux Programming
    Replies: 2
    Last Post: 05-28-2006, 07:49 AM
  4. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 05:24 PM
  5. Graphic Problem...
    By vasanth in forum Game Programming
    Replies: 2
    Last Post: 12-02-2002, 03:26 PM

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