-
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;
}
-
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.