cvs.gedasymbols.org/archives/browse.cgi   search  
Mail Archives: djgpp/1997/12/29/14:30:23

From: Spiritseeker <spiritseeker AT geocities DOT com>
Newsgroups: comp.os.msdos.djgpp
Subject: How come there are five "4" in the array, when i only set four!?
Date: Tue, 30 Dec 1997 03:17:00 +0100
Organization: A customer of Tele2
Lines: 95
Message-ID: <34A8599C.45D@geocities.com>
NNTP-Posting-Host: mn8.swip.net
Mime-Version: 1.0
Cache-Post-Path: mn8!s-23470 AT dialup160-3-54 DOT swipnet DOT se
To: djgpp AT delorie DOT com
DJ-Gateway: from newsgroup comp.os.msdos.djgpp

Hello dear list, could anyone have a look at this and tell why 
Map.Print(); gives me five '4' instead of just four?

--- layer.h ---

#ifndef LAYER_H
#define LAYER_H

class LayerObj {
 private:
  int X_SIZE;
  int Y_SIZE;
  int *tile_data;
 public:
  LayerObj(int xsize, int ysize);
  ~LayerObj(void);
  void Assign(int x_tile, int y_tile, int newval);
  void Print(void);
};

#endif

--- layer.cpp ---

#ifndef __dj_include_stdio_h_
#include <stdio.h>
#endif

#include "layer.h"

LayerObj::LayerObj(int xsize, int ysize)
{
 int x,y;

 /* allocate memory */
 tile_data = (int *) new int[ysize][xsize];

 /* init variables */
 X_SIZE = xsize;
 Y_SIZE = ysize;

 /* clear the array */
 for(y=0;y<Y_SIZE;y++)
  for(x=0;x<X_SIZE;x++) 
    *(tile_data + (y * Y_SIZE) + x) = 0;
};

LayerObj::~LayerObj(void)
{
 /* delete tile_data */
 delete tile_data;
};


/* Assign:
 *  Assigns a value to the tile_data array.
 */
void LayerObj::Assign(int x_tile, int y_tile, int newval)
{
 *(tile_data + (y_tile * Y_SIZE) + x_tile) = newval;
}
 
void LayerObj::Print(void)
{
 /* prints the array to the screen */
 int x,y;

 for(y=0;y<Y_SIZE;y++)
  for(x=0;x<X_SIZE;x++)
   if (x == X_SIZE - 1)
     printf("%d\n",*(tile_data + (y * Y_SIZE) + x));
   else
     printf("%d",*(tile_data + (y * Y_SIZE) + x));
};

--- test.cpp ---


#include <stdio.h>
#include "layer.h"

int main(void)
{
 LayerObj Map = LayerObj(21,20);

 Map.Assign(0,0,4);
 Map.Assign(0,16,4);
 Map.Assign(16,0,4);
 Map.Assign(16,16,4);

 Map.Print();
};

--- done! ---
/Jonas

- Raw text -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019