Whenever I run this code the while(b<63) only seems to run once then does nothing. The (a<63) loop runs fine but doesn't go again because the first loop just stops. Any idea what's going on?

#include <iostream>
#include <time.h>
using namespace std;

int main()
{
    int a,b,c,sand;
    int land[64][64];
    srand((unsigned)time(NULL));
    a=0;
	b=0;
	c=0;
    while(b<63)
    {
		while(a<63)
		{
               sand=rand()%100;
				if(b==0)
				{
					if(sand<90)
					   {
								  land[a][b]=1;
								  a++;
					   }
					if(sand>90)
					{
							  while(c<8&&a<63)
							  {
                                    
                                    
										land[a+c][b]=2;
										c++;
							  }
							  a+=8;
							  c=0;
							  while(a<63)
							  {
										 land[a][b]=1;
										 a++;
							  }

					}
				}
		
				if(b>=1)
				{
					if(land[a][b-1]==1)
						land[a][b]=1;
					if(land[a][b-1]==2)
					{
						if(sand>66)
							a-=1;
						if(sand<33)
							a+=1;
						while(c<8&&a<63)
							  {                                  
								land[a+c][b]=2;
								c++;
							  }
						a+=8;
					}
				}

			}
					cout<<b<<endl;
					b++;
					a=0;
	}
    //output
    a=0;
    b=0;
    while(b<63)
    {
               while(a<63)
               {

                   if(land[a][b]==1)
                   {
                                   cout<<"G";
                                   a++;
                                   }
                   if(land[a][b]==2)
                   {
                                   cout<<"S";
                                   a++;
                                   }
              }
              cout<<endl;
              b++;
              a=0;
    }
                                    
    
    
    
    
    


    cin.get();
    return 0;
}

Thanks.

Recommended Answers

All 4 Replies

When you say "only seems to run once then does nothing." do you mean the program hangs or crashes or it only goes through one iteration and then exits?

Your loop design is slightly off. b will be incremented on the first iteration of the outer loop. But then b = 1. So the condition b>=1 is true. The program then executes the body of that if statement starting with the

if(land[a][b-1]==1)
land[a][b]=1;

statement. This statement will continue to execute over and over because it does not alter a or b. and the loop is infinite now.
This is only what i noticed from a first look at the code. I'm sure if you post what your trying to accomplish with you code and other details. More people will take a look and help you polish it up. hope that helps some :)

A little word of advice:
When posting code, replace your indentation tabs with spaces, or change your IDE's settings to do it for you. Otherwise, your code winds up looking really strange. Poorly formatted code is prone to errors and is hard to read.

It seems that you are missing a closing brace in your output section.

//output
  a=0;
  b=0;
  while(b<63)
  {
    while(a<63)
    {
      if(land[a][b]==1)
      {
        cout<<"G";
        a++;
        if(land[a][b]==2)
        {
          cout<<"S";
          a++;
        }
      }
      cout<<endl;
      b++;
      a=0;
    }
    cin.get();
    return 0;
  }  //<--- this closes outer while loop, not main()

You'll have to add another brace above the cin.get(). This would be much easier to see if your formatting were better.

Formatting guidelines.

Yea sorry it hangs.

Ok I fixed it so it'll increment now so it doesn't stick there and it now goes around 3 times before hanging again instead of twice.

What I'm trying to do is make it generate a river in ascii. It should generate the first line then after that generate more lines based on the one above it. If it worked it would make a curvy line of S's.

#include <iostream>
#include <time.h>
using namespace std;

int main()
{
 int a,b,c,sand;
 int land[64][64];
 srand((unsigned)time(NULL));
 a=0;
 b=0;
 c=0;
 while(b<63)
 {
  while(a<63)
  {
   sand=rand()%100;
   if(b==0)
    {
     if(sand<90)
     {
      land[a][b]=1;
      a++;
      cout<<a<<endl;
     }
     if(sand>90)
     {
      while(c<8&&a<63)
      {                                  
       land[a+c][b]=2;
       c++;
      }
      a+=8;
      c=0;
      while(a<63)
      {
       land[a][b]=1;
       a++;
       cout<<a<<endl;
      }

     }
    }
		
	 if(b>=1)
	 {
	  if(land[a][b-1]==1)
	  {
	   land[a][b]=1;
	   a++;
	  }
	  if(land[a][b-1]==2)
	  {
	   if(sand>66)
		a-=1;
	   if(sand<33)
		a+=1;
	   while(c<8&&a<63)
	   {                                  
		land[a+c][b]=2;
		c++;
	   }
	   a+=8;
	   while(a<63)
	   {
		land[a][b]=1;
		a++;
		cout<<a<<endl;
	   }
	  }
	 }

   }
   cout<<b<<endl;
   b++;
   a=0;
 }
//output
 a=0;
 b=0;
 while(b<63)
 {
  while(a<63)
  {

   if(land[a][b]==1)
   {
    cout<<"G";
    a++;
   }
   if(land[a][b]==2)
   {
    cout<<"S";
    a++;
   }
  }
  cout<<endl;
  b++;
  a=0;
 }
cin.get();
return 0;
}

Here's the newer code if needed.
Thanks again

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.