why

hey guys, ive got my code down and it seems right to me, but of course it isn't. it compiles without any errors and runs. but the problem is 'i think' that it is not acceptign the 6th case statement as its not drawing up the bloody grid, its spinning me out. can you help me plz.?

//Header Files
#include <iostream.h>
#include <stdlib.h>

//Global Declarations
const int MAXCOMMANDS = 100;
const int SIZE = 20;
char ans;
char ignore=',';

int j;
int x;
int y;
int direction =0;
int commandarray[MAXCOMMANDS][2];// ={0};
int floor[SIZE][SIZE];// ={0};

//Function Prototypes
int turnright(int);
int turnleft(int) ;
void getcommands(int [][2]);
int movepen(bool&, int [][SIZE], int, int, int&, int&);
void printarray(int [][SIZE]);

//Main program
void main()
{

int command =0;


bool pendown = false;

do
{
//Initialize variables each time
for(int j =0; j<SIZE; ++j)
{
for(int k=0; k<SIZE; ++k)
floor[j][j]=0;
}
for(int k=0; k<100; ++k)
{
for(j=0; j>2; ++j)
commandarray[k][j] =0;
}
system("cls");
int counter =0;

command =0;
int distance =0;
direction =0;
counter =0;
pendown =false;
int a=0;
int b=0;
system("cls");

getcommands(commandarray);

command = commandarray[counter][0];
while (command !=9)
{
switch (command)
{
case 1: pendown = false;
cout << "The pen is up";
break;

case 2: pendown = true;
cout << "The pen is down";
break;

case 3: direction = turnright(direction);
cout << x<<","<<y;
break;

case 4: direction = turnleft(direction);
cout << x<<","<< y;
break;
case 5: distance = commandarray[counter][1];
movepen(pendown, floor, direction, distance, a, b);
break;
case 6:
cout<< "The drawing is:\n\n" ;
printarray(floor);
break;

}
++counter;
command =commandarray[counter][0];
}

cout<<endl<<"Would you like to redraw?";
cin>>ans;
}
while ((ans=='y')||(ans=='Y'));
return;
}

//Defintion of right turn on grid
int turnright(int)
{
if(direction==3)
direction=0;
else;
++direction;
return (direction);
}

//Defintion of turn on grid
int turnleft(int)
{
if(direction=0)
direction = 3;
else;
--direction;
return(direction);
}

//Reads input given
void getcommands( int commandarray[][2] )
{
// int y;
// int entry;
int distance;
int i=0;
int command=0;
cout<<"Please Enter command: "<<endl;


cout <<"\t <<Command List\n\n";
cout <<"\t <<0 : Read from external file\n";
cout <<"\t <<1 : Pen Up\n";
cout <<"\t <<2 : Pen Down\n";
cout <<"\t <<3 : Turn Right\n";
cout <<"\t <<4 : Turn Left\n";
cout <<"\t <<5,x : Move forward x spaces\n";
cout <<"\t <<6 : Print Drawing\n";
cout <<"\t <<9 : End Commands (sentinel)\n\n\n";

do
{
cin>>command;
//cout<<endl;
commandarray[j][0]=command;
if (command==5)
{
cin>>ignore;
cin>>distance;
commandarray[j][1]=distance;
}
++j;

}
while((command !=9) && (i < MAXCOMMANDS));

}
//Moves the pen according to commands given
int movepen( bool &pendown, int floor [][SIZE], int direction, int distance, int &a, int &b)
{

int counter = 0;

switch(direction)

{
case 0: //Right movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
++a;
}
break;


case 1: //Down movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
++a;
}
break;

case 2: //Left movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
--a;
}
break;

case 3: //Up movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
--a;
}
break;
}
return(pendown, a, b);
}
//End of movePen

// Print out the array with desired commands
void printarray(int floor[][SIZE])
{
for (int i=0; i<SIZE; ++i)
{
for (int j=1; j<SIZE; ++j)
{
if (floor[i][j]==0)
cout<<" ";
else
cout<<"*";
}
cout<< endl;
}
return;
}
[5498 byte] By [wargis] at [2007-11-18 20:33:55]
# 1 Re: why
Is the text "The drawing is:" being printed to the screen?

Is your commandarray correct? Check this in the debugger.
Marc G at 2007-11-11 1:12:16 >
# 2 Re: why
no its not showing that text.
it seems correct, same data type, declaration. dunno whats wrong. im not fimiliar with the debuigger, i get all these symbols i can't understand.:blush:

[edit]; typo
wargis at 2007-11-11 1:13:16 >
# 3 Re: why
Originally posted by wargis
no its not showing that text.
it seems correct, same data type, declaration. dunno whats wrong. im not fimiliar with the debuigger, i get all these symbols i can't understand.:blush:

[edit]; typo

You can't possibly learn to program without being familiar with your debugger. That should be your highest priority. It will let you see line by line what your program is doing, how it got to where it is, current variable values, etc. Absolutely indispensible.
GCDEF at 2007-11-11 1:14:20 >
# 4 Re: why
If you are getting "symbols that you dont understand" with the debugger, maybe you are building a release version and not a debug one... Check it out.
Caronte at 2007-11-11 1:15:20 >
# 5 Re: why
im only a beginner, not familiar with debug.
do you mean (in c++) you go build > start debug > go?
because thats all i know, then what?
wargis at 2007-11-11 1:16:19 >
# 6 Re: why
after i do the above (debug), and run the program, when i select 'draw grid' number 9, i get an error, then i see this??

77F60B6F push esi
77F60B70 push dword ptr [ebp+0Ch]
77F60B73 mov esi,dword ptr [ebp+8]
77F60B76 push esi
77F60B77 call 77F60C26
77F60B7C test al,al
77F60B7E jne 77F87B6F
77F60B84 push ebx
77F60B85 push edi
77F60B86 lea eax,[ebp-8]
77F60B89 push eax
77F60B8A lea eax,[ebp-4]
77F60B8D push eax
77F60B8E call 77F79CCF
77F60B93 call 77F79CEA
77F60B98 and dword ptr [ebp+8],0
77F60B9C mov ebx,eax
77F60B9E cmp ebx,0FFh
77F60BA1 je 77F727F1
77F60BA7 cmp ebx,dword ptr [ebp-4]
77F60BAA jb 77F727F8
77F60BB0 lea eax,[ebx+8]
77F60BB3 cmp eax,dword ptr [ebp-8]
77F60BB6 ja 77F727F8
77F60BBC test bl,3
77F60BBF jne 77F727F8
77F60BC5 mov eax,dword ptr [ebx+4]
77F60BC8 cmp eax,dword ptr [ebp-4]
77F60BCB jb 77F60BD6
77F60BCD cmp eax,dword ptr [ebp-8]
77F60BD0 jb 77F727F8
77F60BD6 test byte ptr ds:[77FC324Ah],80h
77F60BDD jne 77F87B76
wargis at 2007-11-11 1:17:24 >
# 7 Re: why
anyone please? its stumped me, can anyone tell me why/where the problem is?
wargis at 2007-11-11 1:18:25 >
# 8 Re: why
Goto the message handler for your "Draw Grid 9" command. (What code gets called when you click this). Find the first line that actually executes a statement (not a variable declaration) and right-click on that line. Select "Insert/Remove Breakpoint" from the context menu.

Now, press F5 to start your program in Debug mode. When you click "Draw Grid 9", it should stop at this breakpoint and then you can step through your code using the Debug menu at the top (or the F10, F11 keys).
Bond at 2007-11-11 1:19:28 >
# 9 Re: why
hmm ok, i done what you suggested. but it comes with this error after ive inserted the breakpoint and ran the program through the debugger.
"Unhandled exception in comp.exe (NTDLL.DLL): 0xc0000005: Access Violation"

??
wargis at 2007-11-11 1:20:28 >
# 10 Re: why
anyone? please??:cry:
wargis at 2007-11-11 1:21:29 >
# 11 Re: why
Originally posted by wargis
im only a beginner, not familiar with debug.
do you mean (in c++) you go build > start debug > go?
because thats all i know, then what?

No. Go to menu->Build->Set Active Configuration. Select the Debug one here and build the project and once the exe is built, hit F5
kirants at 2007-11-11 1:22:32 >
# 12 Re: why
The problem is with your movepen method.
All the for loops in that method are infinite loops!
Instead of ++distance in all those loops, you want ++counter.
Marc G at 2007-11-11 1:23:29 >
# 13 Re: why
And indeed, if you don't know how to use the debugger, then you should definitely take the time to learn it.
You can't program without a debugger!
Marc G at 2007-11-11 1:24:35 >
# 14 Re: why
for(int j =0; j<SIZE; ++j)
{
for(int k=0; k<SIZE; ++k)
floor[j][j]=0;
}

should be floor[j][k] = 0; // to init properly?
SBarber at 2007-11-11 1:25:33 >
# 15 Re: why
for(j=0; j>2; ++j)
commandarray[k][j] =0;

does not seem correct. Maybe for(j=0;j < 2; j++)??
SBarber at 2007-11-11 1:26:36 >
# 16 Re: why
Originally posted by wargis

//Defintion of right turn on grid
int turnright(int)
{
if(direction==3)
direction=0;
else;
++direction;
return (direction);
}

//Defintion of turn on grid
int turnleft(int)
{
if(direction=0)
direction = 3;
else;
--direction;
return(direction);
}

}
//Moves the pen according to commands given
int movepen( bool &pendown, int floor [][SIZE], int direction, int distance, int &a, int &b)
{

int counter = 0;

switch(direction)

{
case 0: //Right movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
++a;
}
break;


case 1: //Down movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
++a;
}
break;

case 2: //Left movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
--a;
}
break;

case 3: //Up movement
for(counter=0; counter <distance; ++distance)
{
if (pendown==true)
floor[b][a]=1;
else
floor[b][a];
--a;
}
break;
}
return(pendown, a, b);
}
//End of movePen



Hi,
As already pointed by Marc, the mistake are in movepen function (ofcourse there are many other problems).

Try this version of movepen(), (and check your other functions as well) :

//Defintion of right turn on grid
int turnright(int direction)
{
if(direction == 3)
direction = 0 ;
else
++direction ;

return (direction);
}

//Defintion of turn on grid
int turnleft(int direction)
{
if(direction == 0) // You have used = instead of ==
direction = 3;
else
--direction;

return(direction) ;
}

//Moves the pen according to commands given
int movepen( bool pendown, int floor [][SIZE], int direction, int distance, int &a, int &b)
{
int counter = 0;
switch(direction)
{
case 0: //Right movement
for(counter = 0 ; counter < distance ; ++counter)
{
if (pendown == true)
floor[b][a]=1;
//else
// floor[b][a];
++a;
}
break;

case 1: //Down movement
for(counter = 0 ; counter < distance ; ++counter)
{
if (pendown == true)
floor[b][a]=1;
++a;
}
break;

case 2: //Left movement
for(counter = 0 ; counter < distance ; ++counter)
{
if (pendown == true)
floor[b][a]=1;
--a;
}
break;

case 3: //Up movement
for(counter = 0 ; counter < distance ; ++counter)
{
if (pendown == true)
floor[b][a] = 1 ;
--a;
}
break;
}

return 0 ;
}
//End of movePen

Your movepen() function has 4 infinite loops, when runs every time Increase or decrese the variable 'a'. When 'a' cross the range between 0 to 20 and goes out of bound, then at some place "Access Violation" occurs.

Hope you'll learn using the Debugger soon ... :-)
Regards.

-Vipul Pathak ;
VipulPathak at 2007-11-11 1:27:34 >
# 17 Re: why
my god, thanks fellas, silly errors that were overlooked.
now theres 2 things wrong, it won't turn left, right or go down. and say, i put pen down, and after that i input 5,5 (first 5 refers to move forward and the 2nd digit (which is a 5 in this case refers to how many spaces to move) it will move 1 short of that, in my case 4 spaces.??
heres the code agian, wheres the problem?

//Header Files
#include <iostream.h>
#include <stdlib.h>

//Global Declarations
const int MAXCOMMANDS = 100;
const int SIZE = 20;
char ans;
char ignore=',';

int j;
int x;
int y;
int direction =0;
int commandarray[MAXCOMMANDS][2];// ={0};
int floor[SIZE][SIZE];// ={0};

//Function Prototypes
int turnright(int);
int turnleft(int) ;
void getcommands(int [][2]);
int movepen(bool&, int [][SIZE], int, int, int&, int&);
void printarray(int [][SIZE]);

//Main program
void main()
{

int command =0;


bool pendown = false;

do
{
//Initialize variables each time
for(int j =0; j<SIZE; ++j)
{
for(int k=0; k<SIZE; ++k)
floor[j][k]=0;
}
for(int k=0; k<100; ++k)
{
for(int j=0; j>2; ++j)
commandarray[k][j] =0;
}
system("cls");
int counter =0;

command =0;
int distance =0;
direction =0;
counter =0;
pendown =false;
int a=0;
int b=0;
system("cls");

getcommands(commandarray);

command = commandarray[counter][0];
while (command !=9)
{
switch (command)
{
case 1: pendown = false;
// cout << "The pen is up";
break;

case 2: pendown = true;
// cout << "The pen is down";
break;

case 3: direction = turnright(direction);
// cout << x<<","<<y;
break;

case 4: direction = turnleft(direction);
// cout << x<<","<< y;
break;
case 5: distance = commandarray[counter][1];
movepen(pendown, floor, direction, distance, a, b);
break;
case 6:
// cout<< "The drawing is:\n\n" ;
printarray(floor);
break;

}
++counter;
command =commandarray[counter][0];
}

cout<<endl<<"Would you like to redraw?";
cin>>ans;
}
while ((ans=='y')||(ans=='Y'));
return;
}

//Defintion of right turn on grid
int turnright(int direction)
{
if(direction==3)
direction =0;
else;
++direction;
return (direction);
}

//Defintion of turn on grid
int turnleft(int direction)
{
if(direction==0)
direction =3;
else;
--direction;
return(direction);
}

//Reads input given
void getcommands( int commandarray[][2] )
{
// int y;
// int entry;
int distance;
int i=0;
int command=0;
cout<<"Please Enter command: "<<endl;


cout <<"\t <<Command List\n\n";
cout <<"\t <<0 : Read from external file\n";
cout <<"\t <<1 : Pen Up\n";
cout <<"\t <<2 : Pen Down\n";
cout <<"\t <<3 : Turn Right\n";
cout <<"\t <<4 : Turn Left\n";
cout <<"\t <<5,x : Move forward x spaces\n";
cout <<"\t <<6 : Print Drawing\n";
cout <<"\t <<9 : End Commands (sentinel)\n\n\n";

do
{
cin>>command;
//cout<<endl;
commandarray[j][0]=command;
if (command==5)
{
cin>>ignore;
cin>>distance;
commandarray[j][1]=distance;
}
++j;

}
while((command !=9) && (i < MAXCOMMANDS));

}
//Moves the pen according to commands given
int movepen( bool &pendown, int floor [][SIZE], int direction, int distance, int &a, int &b)
{

int counter = 0;

switch(direction)

{
case 0: //Right movement
for(counter=0; counter <distance; ++counter)
{
if (pendown==true)
floor[a][b]=1;
else
floor[a][b]=0;
++b;
}
break;


case 1: //Down movement
for(counter=0; counter <distance; ++counter)
{
if (pendown==true)
floor[a][b]=1;
else
floor[a][b]=0;
++b;
}
break;

case 2: //Left movement
for(counter=0; counter <distance; ++counter)
{
if (pendown==true)
floor[a][b]=1;
else
floor[a][b]=0;
--b;
}
break;

case 3: //Up movement
for(counter=0; counter <distance; ++counter)
{
if (pendown==true)
floor[a][b]=1;
else
floor[a][b]=0;
--b;
}
break;
}
return(pendown, a, b);
}
//End of movePen

// Print out the array with desired commands
void printarray(int floor[][SIZE])
{
for (int i=0; i<SIZE; ++i)
{
for (int j=1; j<SIZE; ++j)
{
if (floor[i][j]==0)
cout<<" ";
else
cout<<"*";
}
cout<< endl;
}
return;
}
wargis at 2007-11-11 1:28:37 >
# 18 Re: why
1) It's <iostream>, not <iostream.h>. The iostream.h header is non-standard, and doesn't even exist in the later versions of Visual C++.

#include <iostream>
using namespace std;

2) It's int main(), not void main(). The main() function returns an int, not void.

3) You made the mistake that a lot of beginners make, and that is to code the entire program at once without unit testing, and hoping that the entire program works on the first try. In other words, you should have written just the pen down function, then test the program. If it works, then you write the pen left funtion, then test. If that works then you write the pen right function, then test, etc. You should have written seperate functions to do the move down, left, etc. instead of cramming all of that code into a "movepen" function. Then testing and debugging would have been much easier since you won't be changing perfectly good, working code to fix the bad code. Right now, if there is something wrong with one of the pen functions, you have to change the code that affects all of the pen functions.

This is how professional programmers write code -- they don't write an entire app without testing the various parts first, and when the parts have been tested, then the integration of those parts into the entire program is done.

4) Using the debugger is as simple as pressing F10. You will then see the arrow next to the line that will be executed. F10 executes that line and goes to the next line, F11 steps into any function call (if the source code to the function exists and has been built with debug info).

If you know just two keys (F10 and F11), you can use the debugger. This is probably even easier than typing in the program. In other words, there's practically no excuse to not use the debugger. The Visual C++ debugger is one of the easiest to use, even someone who knows very little C++ programming can understand how to use it within 5 or 10 minutes, if not sooner. Once you use the debugger, you will be able to answer all of the questions that you've asked so far.

If you're not using Visual C++, then this entire thread should have been posted in the non-Visual C++ forum.

5) With the iostream and main() changes made, what exactly are we to look for when we run the program? I ran the program, entered the input going by your description, but then what? Is there something that we are to be observing? There is no output. Please specify exactly what the input is to the program, and what we are to be looking for to duplicate the problem that you're seeing.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 1:29:36 >
# 19 Re: why
hi paul, yes im using microsoft visual c++ 6.0, what do you mean there is no output, as in nothing is being plotted by the turnleft/right functions? but it's incrementing/decrementing and returning the final direction, or do i have to cout ++--
wargis at 2007-11-11 1:30:32 >
# 20 Re: why
The following code you posted is 1-based. Arrays start at 0.

for (int j=1; j<SIZE; ++j)
{
if (floor[i][j]==0)
cout<<" ";
else
cout<<"*";
}

You never print floor[i][0], since j is never 0 in this loop. This is the reason why one of the "*" is missing.

for (int j=0; j<SIZE; ++j)
{
//...
}

This illustrates my point that I mentioned. You should have tested this function way before writing all of that other code. If the print code doesn't work, how can you use it to determine if any of the other code is working correctly?

Also, the reason why there wasn't any output when I ran your program is that you did not specify exactly what to input. You speicfied entering a "2", and then entering "5,5", and that is exactly what I did. Since the program didn't do anything, I pressed "Enter". It is very important that you specify the steps needed, since we don't know if the "Enter" is supposed to draw the grid.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 1:31:33 >
# 21 Re: why
hmmm, still not turning left/right. ive tried adding some outputs, modified a bit, but nothing.
wargis at 2007-11-11 1:32:43 >
# 22 Re: why
Have you tried using the debugger?
Marc G at 2007-11-11 1:33:41 >
# 23 Re: why
yes, iv tried, this yellow arrow runs through the program step-by step.
still cant find it...
:ehh:

the problemm that is
wargis at 2007-11-11 1:34:46 >
# 24 Re: why
i got it, it was in my case statements, i had to change them to:

case 0 = ++b --> right
case 1 = ++a --> down
case 2 =--b --> left
case 3 = --a --> up
but now, after you finish drawing, and you get the prompt 'would you like to redraw', if you select yes(y then enter) and input commands, now matter what you do, it will redraw the first graphics you inputted, what the?
wargis at 2007-11-11 1:35:46 >
# 25 Re: why
You never clear out the floor array before you input new data. All you're doing now is checking if the answer is 'y' or 'Y' -- you are not doing anything else. You need to reset the floor matrix to spaces.

With proper unit testing, you would not have encountered this problem, but as I mentioned, you made the mistake that a lot of beginners make, and that is to write the whole program first. The trouble that I see is that you will encounter more and more problems ("when I now do this, this doesn't work", etc.), all stemming from not testing the various components first and building the program up this way.

Regards,

Paul McKenzie
Paul McKenzie at 2007-11-11 1:36:38 >
# 26 Re: why
ahh yeh, thx paul.
now for my next section, im writing it by itself just as you suggested, its for my code in this post. its so i can read external files, i wrote this off the top of my head, but doesnt seem right.

char text[50];

if (command ==0)
{
cout<<"Enter the file name including extension:\n";
cin>>text;
ifstream openfile(text);

char text;
while(!openfile.eof());
{
openfile.get(text);
cout << text;
}
openfile.close();
while(!openfile.eof() && command!=9);
}
wargis at 2007-11-11 1:37:41 >