Hi everyone,

I have a question regarding reading commands from an array for a turtle graphics program.

5, x - Move forward x number of spaces (x being the next number in the array, ie. 5, 5)
4 - Turn right
9 - End commands (sentinel value)

If you have this array:

int commands[] = { 5, 5, 4, 5, 9, 9 }

These commands cause the turtle to move 5 spaces in whatever direction he's facing (the first 5 invokes the move command, while the next 5 in the array tells the turtle how many spaces to move). Then the next command is 4 which tells the turtle to turn right. Then the following 5, 9 tells the turtle to move 9 spaces forward.

Now, my problem is I have a for loop that reads these commands:

void TurtleGraphics::processTurtleMoves( const int commands[] )
{
	for ( int cmd = 0; commands[ cmd ] != 9; cmd++ )
	{
		switch ( commands[ cmd ] )
                {
			case 4: // turn right
				tDirection += 3;
				if ( tDirection > 12 )
				{
					tDirection -= 12;
				}
				break;
			case 5: //move forward x number of spaces
				int steps = commands[ cmd++ ];

				for ( int stepsTaken = 0; stepsTaken < steps; stepsTaken++ )
				{
					switch ( tDirection )
					{
						case 3: // turtle is facing right
							if ( penPos == 1 )
							{
								m_Floor[ tRow ][ tCol ] = 1;
							}
							tCol++
							break;
						case 6: // turtle is facing down
							if ( penPos == 1 )
							{
								m_Floor[ tRow ][ tCol ] = 1;
							}
                                                        tRow++;
					                break;
						case 9: // turtle is facing left
							if ( penPos == 1 )
							{
								m_Floor[ tRow ][ tCol ] = 1;
							}
							if ( tRow-- >= 0 )
							{
								cout << "Out of bounds" << endl;
							}
							tRow--;
							break;
						case 12: // turtle is facing up
							if ( penPos == 1 )
							{
								m_Floor[ tRow ][ tCol ] = 1;
							}
                                                        tCol++;
							break;
					}
				}
				break;
			case 9: // end of data (sentinel)
				break;
		}
	}

This code is simplifiied to just the area I'm confused about. I don't get how to read in the "second" command when case 5 (move forward) is chosen. I'm thinking I have to increment cmd so the read in command is the digit after the current one, and then read the value in cmd + 1 and use this value to process the number of steps the turtle should take.

So the question is:

How do you read in the second value in the commands 5, 5 and then process the next command, 4, and then 5, 9, and then 9 to end? Help please! ^^

You need to change your increment:

case 5: //move forward x number of spaces
        int steps = commands[ ++cmd ];

This will increment before you load steps, getting the number of steps.

commented: solver of pre-incrementing instead of post-incrementing +1

I'm actually still getting a compile error with that code.

error C2360: initialization of 'steps' is skipped by 'case' label
see declaration of 'steps'

I don't think I'm going with the right approach to this problem.

*I also tried putting steps in its own braces to give it local scope, ie.

{
     int steps = commands[ ++cmd ];
}

The error for this is now:

error C2065: 'steps' : undeclared identifier

Anyone else with a different approach to this problem maybe?

I think you can declare and intilialize steps after the function "processTurtleMoves" declaration.

void TurtleGraphics::processTurtleMoves( const int commands[] )
{
  int steps=0;
  .... .. .. 
  .... .. ..

}

This should solve the problem. And as WaltP said, use a prefix form rather than a postfix one.

It compiles now that I put the steps inside the for loop, but not BEFORE the for loop:

for ( int cmd = 0; commands[ cmd ] != 9; cmd++ )
{
    int steps = command[ ++cmd ];
    
    switch ( commands[ cmd ]
    {
...
}

But won't this pre-increment cmd before it even enters the switch statement? I thought I want to increment cmd AFTER the switch statement determines that I want scenario 5, then use the incremented cmd to determine how many spots to move?

Do not initialize steps with command[++cmd], this will surely increment cmd before it even enters the switch statement. Just initialize the steps variable with any legal value. It will be better if you initialize steps before the loop as I said in previous post.

commented: solver of declaring variables inside switch cases +1

Apparently, its not good to declare variables inside "case x" like the others have said. Everything worked when I declared it as a local variable outside the switch statement and used the variable "steps" inside the "case x" when needed.

Thanks to everyone's help, I managed to solve this problem. ++points for all of you. ^^

Thanks ohnomis for the prefix version "++points" . ;)
I think you should mark the thread as solved.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.