I have only started learning borland c++ programming a few months ago, I'm actually a math student, so my knowledge of c++ is pretty limited. I have an assignment where X represents an ant that moves around in a 7x7 grid box. It can move up, left, down or right by 1 step at a time. I have already written the code, but sometimes the 1st 7x7 grid displayed is sometimes distorted. It only displayed a 7x3 grid. Please let me know what is the mistake I made. I know my course is not so efficient, I just want to get it working first. Please help me! I'm dying! =(

#include<iostream.h>
#include<conio.h>
#define size 20

void main()
{
srand(time(0));
int box;
int i,j,y=0, count, random, key;

for (i=0;i<7;i++)
for (j=0;j<7;j++){
box[j]=y+1;
y= box[j];
}
i=3;
j=3;
for (count=1;;count++)
{
random = rand()%4;

if (count==1)
key= box[3][3];
else if (random==0)
key= box[j=j+1];
else if (random==1)
key= box[j=j-1];
else if (random==2)
key= box[j];
else if (random==3)
key= box[j];

if (i<0||j<0)
goto out;
if (i>=7||j>=7)
goto out;

{
cout<<" ";
for(int line=0;line<7;line++)

cout<<"--- ";
}
cout<<endl;
for(int k=0;k<7;k++)
{
for(int l=0;l<7;l++)
{
if(key!=box[k][l])
cout<<"| ";

else if(key==box[k][l])
cout<<"| X ";

}
cout<<"|";
cout<<endl;
{
cout<<" ";
for(int line=0;line<7;line++)

cout<<"--- ";
cout<<endl;
}
}
cout<<endl<<endl;
}
out: cout<<"\nExit from program.";
getch();
}

i didnot found your problem can you tell us the senario of the error

Your code does what you said you wanted it to do for me, no problem displaying any of the grids. There are a number of improvements that you could make though.

The number one thing that you should probably do is use a more modern compiler/development environment. I hear that Code::Blocks is good on Windows. Personally, I like to use Qt Creator for small projects like this.

As far as your code goes:

  1. main should return int , so your program should start int main() . Once you do this, you will have to end your program with return 0; (although you can return any int you like, it is conventional to return zero if everything went to plan). If you use a more modern compiler, it will complain if you try and do void main() (as it should).
  2. Don't use #define like you have. You might as well declare size like this:
    #include <iostream>
    
    int main(){
       const unsigned size = 20;
       
       /* The rest of your program goes here as before */
    
    }
  3. Don't use goto in this context. goto is usually only used when trying to make some kind of run-time optimisation in code that has to run as fast as possible. It's always going to cost you readability, which is more important than the optimisation that you get from goto . In your case, you're in a loop when you call goto , so you can replace it with a break; and the loop will exit. In general, if you want to just skip to the end of your program at some point, you can return instead.
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.