I'm doing this exercise where I'm supposed to emulate logowriter. Basically it's a turtle that starts in the center and moves in different directions based on the commands you input. My problem is all the commands but one take only one value as an input. The other command takes two command takes two variables, one variable teling the program the turtle needs to be moved forward and the second variable telling how far it should move. Well this seems to be throwing of my whole program. For one entering command 6 which is supposed to print all the movements made doesn't work(although it works when run on its own). And when I try to exit by pressing 9 I have to enter it like 3 times.
My book didn't do a very good job explaining the ins and outs of scanf, I was hoping maybe someone over here can help me out.
Code:
#include <stdio.h>
void moveFor(int, int, int[][ 50 ], int);
void printArray( int[][ 50 ] );
int positionV = 25, positionH = 25;
int main()
{
int input = 0, spaceInput = 0, direction = 0, pen = 0;
int floor[ 50 ][ 50 ] = { { 0 }, { 0 } };
printArray( floor );
while (input != 9) {
scanf("%d %d", &input, &spaceInput);
if (input == 9)
continue;
/*
if (input == 3)
direction -= 90;
if (input == 4)
direction += 90;
direction = direction % 360;
*/
if (input == 1)
pen = 0;
if (input == 2)
pen = 1;
if (input == 5 && spaceInput != 0) {
moveFor(direction, spaceInput, floor, pen);
spaceInput = 0;
if (input == 6)
printArray( floor );
}
}
return 0;
}
void moveFor(int d, int spaces, int table[][ 50 ], int pStatus)
{
int counter;
for (counter = 1; counter <= spaces; ++counter) {
if (d == 0 && positionH != 49) {
if (pStatus == 1)
table[++positionH][positionV] = 1;
if (pStatus == 0)
++positionH;
}
if (d == 90 && positionV != 49) {
if (pStatus == 1)
table[positionH][--positionV] = 1;
if (pStatus == 0)
--positionV;
}
if (d == 180 && positionH != 0) {
if (pStatus == 1)
table[--positionH][positionV] = 1;
if (pStatus == 0)
--positionH;
}
if (d == 270 && positionV != 0) {
if (pStatus == 1)
table[positionH][++positionV] = 1;
if (pStatus == 0)
++positionV;
}
}
return;
}
void printArray( int array[][ 50 ] )
{
int i, j;
for (i = 0; i <= 49; ++i) {
for (j = 0; j <= 49; ++j) {
if (array[ i ][ j ] == 0)
printf(" ");
else if (array[ i ][ j ] == 1)
printf("*");
}
printf("\n");
}
return;
}