Dec 19, 2009 at 3:15pm UTC
Hiya everyone,I have a problem with my source code and I just dont know how to fix it ... any suggestions ?
Thanks
#include <iostream>
using namespace std;
const int MAXCOMMANDS = 100, SIZE = 20;
int turnRight( int );
int turnLeft( int );
void getCommands( int [][ 2 ] );
void movePen( int, int [][ SIZE ], int, int );
void printArray( const int [][ SIZE ] );
int main()
{
int floor[ SIZE ][ SIZE ] = { 0 }, command, direction = 0,
commandArray[ MAXCOMMANDS ][ 2 ] = { 0 }, distance, count = 0;
int penDown= 0;
getCommands( commandArray );
command = commandArray[ count ][ 0 ];
while ( command != 9 ) {
switch ( command ) {
case 1:
penDown = 0;
break;
case 2:
penDown = 1;
break;
case 3:
direction = turnRight( direction );
break;
case 4:
direction = turnLeft( direction );
break;
case 5:
distance = commandArray[ count ][ 1 ];
movePen( penDown, floor, direction, distance );
break;
case 6:
cout << "\nThe drawing is:\n\n";
printArray( floor );
break;
}
command = commandArray[ ++count ][ 0 ];
}
return 0;
}
void getCommands( int commands[][ 2 ] )
{
int tempCommand;
cout << "Enter command (9 to end input): ";
cin >> tempCommand;
for ( int i = 0; tempCommand != 9 && i < MAXCOMMANDS; ++i ) {
commands[ i ][ 0 ] = tempCommand;
if ( tempCommand == 5 ) {
cin.ignore(); // skip comma
cin >> commands[ i ][ 1 ];
}
cout << "Enter command (9 to end input): ";
cin >> tempCommand;
}
commands[ i ][ 0 ] = 9; // last command
}
int turnRight( int d )
{
return ++d > 3 ? 0 : d;
}
int turnLeft( int d )
{
return --d < 0 ? 3 : d;
}
void movePen( int down, int a[][ SIZE ], int dir, int dist )
{
static int xPos = 0, yPos = 0;
int j; // looping variable
switch ( dir ) {
case 0: // move to the right
for ( j = 1; j <= dist && yPos + j < SIZE; ++j )
if ( down )
a[ xPos ][ yPos + j ] = 1;
yPos += j - 1;
break;
case 1: // move down
for ( j = 1; j <= dist && xPos + j < SIZE; ++j )
if ( down )
a[ xPos + j ][ yPos ] = 1;
xPos += j - 1;
break;
case 2: // move to the left
for ( j = 1; j <= dist && yPos - j >= 0; ++j )
if ( down )
a[ xPos ][ yPos - j ] = 1;
break;
case 3: // move up
for ( j = 1; j <= dist && xPos - j >= 0; ++j )
if ( down )
a[ xPos - j ][ yPos ] = 1;
xPos -= j - 1;
break;
}
}
void printArray( const int a[][ SIZE ] )
{
for ( int i = 0; i < SIZE; ++i ) {
for ( int j = 0; j < SIZE; ++j )
cout << ( a[ i ][ j ] ? '*' : ' ' );
cout << endl;
}
system ("PAUSE");
}
Dec 19, 2009 at 4:27pm UTC
Can you be more specific about your problem? Does it compile? If not, exactly what error messages do you get? If it does compile, does it not run correctly? If so, what do you expect it to do, and what does it actually do?
Can you put your code in code tags next time?
Dec 19, 2009 at 4:47pm UTC
Sorry,the hotlink is not workin so i have to do it like this ... Heres what it says when i compile it :
http://img14.imageshack.us/i/54169634.jpg/
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#include <iostream>
using namespace std;
const int MAXCOMMANDS = 100, SIZE = 20;
int turnRight( int );
int turnLeft( int );
void getCommands( int [][ 2 ] );
void movePen( int , int [][ SIZE ], int , int );
void printArray( const int [][ SIZE ] );
int main()
{
int floor[ SIZE ][ SIZE ] = { 0 }, command, direction = 0,
commandArray[ MAXCOMMANDS ][ 2 ] = { 0 }, distance, count = 0;
int penDown= 0;
getCommands( commandArray );
command = commandArray[ count ][ 0 ];
while ( command != 9 ) {
switch ( command ) {
case 1:
penDown = 0;
break ;
case 2:
penDown = 1;
break ;
case 3:
direction = turnRight( direction );
break ;
case 4:
direction = turnLeft( direction );
break ;
case 5:
distance = commandArray[ count ][ 1 ];
movePen( penDown, floor, direction, distance );
break ;
case 6:
cout << "\nThe drawing is:\n\n" ;
printArray( floor );
break ;
}
command = commandArray[ ++count ][ 0 ];
}
return 0;
}
void getCommands( int commands[][ 2 ] )
{
int tempCommand;
cout << "Enter command (9 to end input): " ;
cin >> tempCommand;
for ( int i = 0; tempCommand != 9 && i < MAXCOMMANDS; ++i ) {
commands[ i ][ 0 ] = tempCommand;
if ( tempCommand == 5 ) {
cin.ignore(); // skip comma
cin >> commands[ i ][ 1 ];
}
cout << "Enter command (9 to end input): " ;
cin >> tempCommand;
}
commands[ i ][ 0 ] = 9; // last command
}
int turnRight( int d )
{
return ++d > 3 ? 0 : d;
}
int turnLeft( int d )
{
return --d < 0 ? 3 : d;
}
void movePen( int down, int a[][ SIZE ], int dir, int dist )
{
static int xPos = 0, yPos = 0;
int j; // looping variable
switch ( dir ) {
case 0: // move to the right
for ( j = 1; j <= dist && yPos + j < SIZE; ++j )
if ( down )
a[ xPos ][ yPos + j ] = 1;
yPos += j - 1;
break ;
case 1: // move down
for ( j = 1; j <= dist && xPos + j < SIZE; ++j )
if ( down )
a[ xPos + j ][ yPos ] = 1;
xPos += j - 1;
break ;
case 2: // move to the left
for ( j = 1; j <= dist && yPos - j >= 0; ++j )
if ( down )
a[ xPos ][ yPos - j ] = 1;
break ;
case 3: // move up
for ( j = 1; j <= dist && xPos - j >= 0; ++j )
if ( down )
a[ xPos - j ][ yPos ] = 1;
xPos -= j - 1;
break ;
}
}
void printArray( const int a[][ SIZE ] )
{
for ( int i = 0; i < SIZE; ++i ) {
for ( int j = 0; j < SIZE; ++j )
cout << ( a[ i ][ j ] ? '*' : ' ' );
cout << endl;
}
system ("PAUSE" );
}
this should be a turtle graph
Last edited on Dec 19, 2009 at 4:49pm UTC
Dec 19, 2009 at 6:18pm UTC
I don't understand what the errors mean exactly, but the problem is on Line 69:
commands[ i ][ 0 ] = 9; // last command
It appears that "i" doesn't exist in this scope, because you declared it within the parameters of the "for" loop. In other words, you can't do this:
1 2 3 4
for (int i = 0; i < 10; i++){
cout << "\n" << "current value of i:" << i;
}
cout << "\n" << "final value of i: " << i;
... If you want "i" to continue to exist, declare it outside of the loop.
1 2 3 4 5
int i;
for (i = 0; i < 10; i++){
cout << "\n" << "current value of i: " << i;
}
cout << "\n" << "final value of i: " << i;
Last edited on Dec 19, 2009 at 6:18pm UTC