I/O problem cont

Nonetheless, I rewrote the source without any type of input or written output
(attached to this message). It compiles fine but it doesn’t run… anyone
have an idea?

Thank you again in advance,

Daniel

/////////// THE NEW CODE \\\\\\\\

#include
#include <iostream.h>
#include <stdio.h>
#include <math.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#include <fstream.h>

#include <SDL/SDL.h>

using namespace std;

class pixel_state {
public:
unsigned int R : 8, G : 8, B : 8;
pixel_state () {};
pixel_state operator + (pixel_state);
pixel_state operator += (pixel_state);
pixel_state operator = (pixel_state);
pixel_state operator / (double);
pixel_state operator * (double);
};

pixel_state pixel_state :: operator + (pixel_state param) {
pixel_state temp;
temp.R = R + param.R;
temp.G = G + param.G;
temp.B = B + param.B;
return (temp);
}

pixel_state pixel_state :: operator += (pixel_state param1) {
pixel_state temp1;
temp1.R = temp1.R + param1.R;
temp1.G = temp1.G + param1.G;
temp1.B = temp1.B + param1.B;
return (temp1);
}

pixel_state pixel_state :: operator = (pixel_state param4) {
pixel_state temp4;
temp4.R = param4.R;
temp4.G = param4.G;
temp4.B = param4.B;
return (temp4);
}

pixel_state pixel_state :: operator / (double param2) {
pixel_state temp2;
temp2.R = (unsigned int)(R / param2);
temp2.G = (unsigned int)(G / param2);
temp2.B = (unsigned int)(B / param2);
return (temp2);
}

pixel_state pixel_state :: operator * (double param3) {
pixel_state temp3;
temp3.R = (unsigned int)(R * param3);
temp3.G = (unsigned int)(G * param3);
temp3.B = (unsigned int)(B * param3);
return (temp3);
}

int random_seed () {

int rand_seed;
srand((unsigned int)time((time_t *)NULL));
rand_seed = rand();
return rand_seed;

}

int random_range(int lowest_number, int highest_number)
{
srand((unsigned int)time((time_t *)NULL));
if(lowest_number > highest_number){
int q = highest_number;
lowest_number = highest_number;
highest_number = q;
}

int range = highest_number - lowest_number + 1;
return lowest_number + int(range * rand()/(RAND_MAX + 1.0));

}

pixel_state** initialize_species (long k, int seed) {

pixel_state **array;

array = new pixel_state* [160];
for (int i=0; i<160; i++) array[i] =new pixel_state [120];

for (int n=0; n<160; n++) {
      for (int l; l<120; l++) {
          srand(seed+random_range(0,k));
          float R_temp = (rand()/RAND_MAX)*255;
          float G_temp = (rand()/RAND_MAX)*255;
          float B_temp = (rand()/RAND_MAX)*255;
          array[n][l].R = (int)R_temp;
          array[n][l].G = (int)G_temp;
          array[n][l].B = (int)B_temp;
          }
	}

return array;

}

pixel_state** evolve_species (pixel_state **array, int L) {

pixel_state **array_to_evolve, counter;

array_to_evolve = new pixel_state* [160];
for (int i=0; i<160; i++) array_to_evolve[i] =new pixel_state [120];

for (int n=0; n<160; n++) {
for (int l=0; l<120; l++) {

    int k;
	counter.R = 0;
    counter.G = 0;
    counter.B = 0;

 for (k=1; k<=L; k++) {

  if ((n-k)<0 && (n+k)<L) counter += (array[n-k+160][l]+array[n+k][l]);
  if ((n-k)<0 && (n+k)>L) counter += (array[n-k+160][l]+array[n+k-160][l]);
  if ((n-k)>0 && (n+k)>L) counter += (array[n-k][l]+array[n+k-160][l]);
  if ((n-k)>0 && (n+k)<L) counter += (array[n-k][l]+array[n+k][l]);

  if ((l-k)<0 && (l+k)<L) counter += (array[n][l-k+120]+array[n][l+k]);
  if ((l-k)<0 && (l+k)>L) counter += (array[n][l-k+120]+array[n][l+k-120]);
  if ((l-k)>0 && (l+k)>L) counter += (array[n][l-k]+array[n][l+k-120]);
  if ((l-k)>0 && (l+k)<L) counter += (array[n][l-k]+array[n][l+k]);
			}
        array_to_evolve[n][l] = ((counter + (array[n][l]*4)) / (4*k*k+3));
                          }
     }

return array_to_evolve;

}

pixel_state** array_COPY (pixel_state **array) {

 pixel_state **array_COPY;

 array_COPY = new pixel_state* [160];
 for (int i=0; i<160; i++) array_COPY[i] =new pixel_state [120];


 for (int n=0; n<160; n++) 
   for (int l=0; l<120; l++) 
 array_COPY[n][l] = array[n][l];
 
 return array_COPY;

}

char * SDL_Input (char* buffer, int X) {

 SDL_EnableUNICODE(1);
 SDL_Event e;
 string s;
 int MAXLENGTH = X;
 while(SDL_PollEvent(&e)) {
     switch( e.type ) {
             case SDLK_RETURN:
                  break;
             case SDL_KEYDOWN:
                  if(s.length() < MAXLENGTH)
                  {s = s + string(1,(char)(e.key.keysym.unicode&0xFF));
                  continue;}
                  else break;
                  }}
 for (int i=0; i<X; i++) {buffer[i]=s[i];}
 
 return buffer;

}

bool stability_control(pixel_state **array, pixel_state **array_comp) {

int n, l;
for (n=0; n<160; n++) {
for (l=0; l<120; l++) {
if ((array[n][l].R != array_comp[n][l].R) ||
(array[n][l].G != array_comp[n][l].G) ||
(array[n][l].B != array_comp[n][l].B)) break;
}
}
if (n<(159) || l<(119)) return false;
else return true;
}

void Slock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
if ( SDL_LockSurface(screen) < 0 )
{
return;
}
}
}

void Sulock(SDL_Surface *screen)
{
if ( SDL_MUSTLOCK(screen) )
{
SDL_UnlockSurface(screen);
}
}

void DrawPixel(SDL_Surface *screen, int x, int y,
Uint8 R, Uint8 G, Uint8 B)
{
Uint32 color = SDL_MapRGB(screen->format, R, G, B);
switch (screen->format->BytesPerPixel)
{
case 1: // Assuming 8-bpp
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x;
*bufp = color;
}
break;
case 2: // Probably 15-bpp or 16-bpp
{
Uint16 *bufp;
bufp = (Uint16 )screen->pixels + yscreen->pitch/2 + x;
*bufp = color;
}
break;
case 3: // Slow 24-bpp mode, usually not used
{
Uint8 *bufp;
bufp = (Uint8 )screen->pixels + yscreen->pitch + x * 3;
if(SDL_BYTEORDER == SDL_LIL_ENDIAN)
{
bufp[0] = color;
bufp[1] = color >> 8;
bufp[2] = color >> 16;
} else {
bufp[2] = color;
bufp[1] = color >> 8;
bufp[0] = color >> 16;
}
}
break;
case 4: // Probably 32-bpp
{
Uint32 *bufp;
bufp = (Uint32 )screen->pixels + yscreen->pitch/4 + x;
*bufp = color;
}
break;
}
}

void DrawScene(SDL_Surface *screen, pixel_state **array)
{
Slock(screen);

float x_pixel_per_cell = floor(640/159.9);
float y_pixel_per_cell = floor(480/119.9);

for(float x=0; x<640; x++)
{
for(float y=0; y<480; y++)
{
int Red = array[(int)(x/(x_pixel_per_cell + 0.7))][(int)(y/
(y_pixel_per_cell + 0.7))].R;
int Green = array[(int)(x/(x_pixel_per_cell + 0.7))][(int)(y/
(y_pixel_per_cell + 0.7))].G;
int Blue = array[(int)(x/(x_pixel_per_cell + 0.7))][(int)(y/
(y_pixel_per_cell + 0.7))].B;
DrawPixel(screen,(int)x,(int)y,Red,Green,Blue);
}
}
Sulock(screen);
SDL_Flip(screen);
}

int main(int argc, char *argv[]) {

pixel_state **evo_array, **evo_array_check;
int Population, Years, iter, strati, seed, check_counter=0;
long stati;

if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 )
{
printf(“Unable to init SDL: %s\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

SDL_Surface *screen;
screen=SDL_SetVideoMode
(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF|SDL_FULLSCREEN);
if ( screen == NULL )
{
printf(“Unable to set 640x480 video: %s\n”, SDL_GetError());
exit(1);
}
int done=0;

while(done == 0)
{
SDL_Event event;

while ( SDL_PollEvent(&event) )
{
  if ( event.type == SDL_QUIT )  {  done = 1;  }

  if ( event.type == SDL_KEYDOWN )
  {
    if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; }
  }
}

seed = random_seed();
stati = 2000;
Years = 300;
strati = 13;

evo_array = initialize_species(stati, seed);

for (iter=0; iter<=Years; iter++) {

evo_array = evolve_species(evo_array, strati);
DrawScene(screen, evo_array);
    }

return 0;
}