Thread: Need help with functions

  1. #1
    Unregistered
    Guest

    Need help with functions

    Hello I am trying to learn C++ on my own and I have a program that I am trying to convert to using all functions instead of objects. I know your asking why I would want to do that. Well I kind of understand objects and classes. I just don't fully understand how to pass values to functions. Anyway here is the program and if you can help me out in anyway please let me know.


    //Program: Draws square based on turtle graphis logo. This particular program draws a square
    #include<iostream>
    using std::cin;
    using std::cout;
    using std::endl;


    int main()
    {
    //Declare variables
    char SetTile;
    char TurtleTile;
    char Display;
    char TurtleLine;
    int xpos;
    int ypos;
    int direction; //1 up,2 is right, 3 is down, 4 is left
    bool PenState; //false is pen up and true is pen down

    int input=99,spaces=0; //null values
    char world[20][20];
    turtle my_turtle;

    int commands[15]={2,5,12,3,5,12,3,5,12,3,5,12,1,6,9};
    my_turtle.initialize(world);

    for(int k=0;k<15;k++)
    {

    input=commands[k];

    switch (input) //draws square
    {
    case 1: PenUp();break;
    case 2: PenDown();break;
    case 3: turn(input);break;
    case 4: k++; spaces=commands[k];
    case 5: move(spaces,world);break;
    case 6: print(world);break;
    case 9: return 0;break;
    }
    }
    return 0;
    }
    //************************************************** *******************************************
    //
    Function definitions
    void turtle()
    {
    SetTile=' ';
    TurtleLine='0';
    Display='#';
    TurtleTile=' ';
    PenState=true;
    ypos=1;
    xpos=1;
    direction=2;
    };

    void initialize(char world[20][20]);
    void MoveOne(char world[20][20]);
    void move(int spaces, char world[20][20]);
    void turn(int direction); //4 is left and 3 is right
    void PenUp();
    void PenDown();
    void print(char world[20][20]);
    };
    void print(char world[20][20])
    {
    for(int y=0;y<20;y++)
    {
    for(int x=0;x<20;x++)
    {
    cout<<world[y][x]<<' ';
    }
    cout<<endl;
    }
    }

    void PenUp()
    {
    PenState=false;
    }
    void PenDown()
    {
    PenState=true;
    }

    void move(int spaces, char world[20][20])
    {
    for(int x=0;x<spaces;x++)
    {
    MoveOne(world);
    }
    }

    void turn(int direction) //4 is left and 3 is right
    {
    if(direction==3)
    {
    if(direction==4)direction=1;
    else direction+=1;
    }
    if(direction==4)
    {
    if(direction==1)direction=4;
    else direction-=1;
    }
    }

    void initialize(char world[20][20])
    {
    for(int x=0;x<20;x++)
    {
    for(int y=0;y<20;y++)
    {
    world[y][x]=' ';
    if(x==0 || x==20-1 || y==20-1 || y==0)
    world[y][x]=Display;
    }
    }
    }

    void MoveOne(char world[20][20])
    {
    if(PenState==true)
    world[ypos][xpos]=TurtleLine;
    else
    world[ypos][xpos]=SetTile;

    switch (direction)
    {
    case 2: if(xpos+1!=20-1) //checks for edge of wall and moves right
    xpos+=1;
    break;
    case 4: if(xpos-1!=0) //moves left
    xpos-=1;
    break;

    case 3: if(ypos+1!=20-1) //moves down
    ypos+=1;
    break;

    case 1: if(ypos-1!=0) //moves up
    ypos-=1;
    break;
    }

    world[ypos][xpos]=TurtleTile;
    }

  2. #2
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Code:
    int commands[15]={2,5,12,3,5,12,3,5,12,3,5,12,1,6,9}; 
    my_turtle.initialize(world); 
    
    for(int k=0;k<15;k++) 
    { 
    
    input=commands[k]; 
    
    switch (input) //draws square 
    { 
    case 1: PenUp();break; 
    case 2: PenDown();break; 
    case 3: turn(input);break; 
    case 4: k++; spaces=commands[k]; 
    case 5: move(spaces,world);break; 
    case 6: print(world);break; 
    case 9: return 0;break; 
    } 
    }

    What is your switch supposed to do when you assign 12 as a value in the switch?

    Also, when you attempt to modify values in your functions, the variables go out of scope. So, you are going to need to do a little changing.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Void Functions Help
    By bethanne41 in forum C++ Programming
    Replies: 1
    Last Post: 05-09-2005, 05:30 PM
  2. Functions and Classes - What did I do wrong?
    By redmage in forum C++ Programming
    Replies: 5
    Last Post: 04-11-2005, 11:50 AM
  3. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  4. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  5. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM