GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 20-Feb-2006, 14:57
chr1zis chr1zis is offline
New Member
 
Join Date: Feb 2006
Posts: 6
chr1zis is on a distinguished road

Array of Objects


Hello all. I am pretty new to C++ so I hope I can explain my problem. I am trying to create an array of objects. I want to read in a file that contains 1's and 0's and create an object for each 1 or 0. As I create each object I want to place them into an array. I think I have figured out how to read in the file and create the objects but I am having trouble putting them into an array. Please help! Thanks in advance.

Here is my code so far....

CPP / C++ / C Code:
class Cell {
public:
	Cell(int a) {
		value = a;
	}
private:
	int value;
};

int main()
{
    const int size = 10;
    int lifearray[size][size]; //constructing the 10x10 board
    int current;
        
    ifstream inLife("input.dat", ios::in); 
    ofstream outLife("output.dat", ios::out);
    
    if(!inLife)
    {
        cerr << "File could not be opened" << endl;
        exit(1);
    }    

    //for loop that reads in the file and constructs the array
    for(int i=0; i<10; i++)
    {
            for(int j=0; j<10; j++)
            {
                      inLife >> current;
					  Cell life(current);
                      lifearray[i][j] = current;
            }
    }
Last edited by LuciWiz : 21-Feb-2006 at 01:04. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #2  
Old 20-Feb-2006, 15:07
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: Array of Objects


You need to convert each byte to an int before it can be stored as an integer.

Firest declare current as a char array because 1 in ascii is 0x31 and 0 is 0x30.

Next you want to convert those input bytes respectively to 0x01 and 0x00 and store them in 4 bytes (an integer).

to do this i would use the atoi function declared in stdlib.h

so it would be:
CPP / C++ / C Code:
char current[size];
cin >> current;
lifearray[i][j] = atoi(current);

see a synopsis here: http://man.he.net/man3/atoi
Last edited by LuciWiz : 21-Feb-2006 at 01:05. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #3  
Old 20-Feb-2006, 15:11
chr1zis chr1zis is offline
New Member
 
Join Date: Feb 2006
Posts: 6
chr1zis is on a distinguished road

Re: Array of Objects


ignore this.. lifearray[i][j] = current;

i need to know how to create an array to where i can store the Cell objects...
  #4  
Old 20-Feb-2006, 15:17
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: Array of Objects


You can declare an array of classes just like any other type...
CPP / C++ / C Code:
#define MAX_CELLS 1000

class Cell *CELL[MAX_CELLS];

CELL[i] = new Cell(current);
Last edited by LuciWiz : 21-Feb-2006 at 01:34. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #5  
Old 20-Feb-2006, 15:32
chr1zis chr1zis is offline
New Member
 
Join Date: Feb 2006
Posts: 6
chr1zis is on a distinguished road

Re: Array of Objects


does this look right??
CPP / C++ / C Code:
const int size = 10;
    int lifearray[size][size]; //constructing the 10x10 board
    int current;
	class Cell *array[10][10];
        
    ifstream inLife("input.dat", ios::in); 
    ofstream outLife("output.dat", ios::out);
    
    if(!inLife)
    {
        cerr << "File could not be opened" << endl;
        exit(1);
    }    

    //for loop that reads in the file and constructs the array
    for(int i=0; i<10; i++)
    {
            for(int j=0; j<10; j++)
            {
                      inLife >> current;
                      array[i][j] = new Cell(current);
            }
    }

are you familiar with the game of life? http://www.bitstorm.org/gameoflife/ this is what i am trying to accomplish. i have already made the game using just the integer values and now i am trying to do it using objects.. how would i be able to use my 'play' function (which plays the game) if i am using objects.. here is the code.. thanks

CPP / C++ / C Code:
int play(int c[10][10], int size)
{
    if(!c)
    {
          cerr << "Board contains no live cells." << endl;
          exit(1);
    }
    
    int temp[10][10]; //temp array so the final values will not be altered
    
    for(int i=0; i<10; i++)
     {
        for(int j=0; j<10; j++)
        {
                temp[i][j] = c[i][j];
        }
     }
    
    int legalTurn = 0;
    int fitness = 0;
    int lastTurn = 0;
    bool anyLife = false;
  
    //for loop that goes through the array and sends the values to the
    //check function to determine if the cell should be alive or dead
    for(int x=0; x<100; x++)
    {
            for(int y=0; y<10; y++)
            {
                    for(int z=0; z<10; z++)
                    {
                            int living = check(c, y, z);
                            if(living > 0)
                            {
                                      temp[y][z] = 1;
                                      anyLife = true;
                                      if(x==100-1)
                                      {
                                                  lastTurn++;
                                      }
                            }
                            else 
                                 temp[y][z] = 0;
                    }
            }
            
            if(anyLife)
            {
                       legalTurn++;
            }
            
            anyLife = false;
            
            for(int i=0; i<10; i++)
            {
                    for(int j=0; j<10; j++)
                    {      
                           c[i][j] = temp[i][j];
                    }
            }
    }
    
    fitness = legalTurn * lastTurn;
    return fitness;
}
Last edited by LuciWiz : 21-Feb-2006 at 01:39. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #6  
Old 20-Feb-2006, 15:39
davekw7x davekw7x is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Left Coast, USA
Posts: 4,720
davekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to beholddavekw7x is a splendid one to behold

Re: Array of Objects


Quote:
Originally Posted by chr1zis
i need to know how to create an array to where i can store the Cell objects...

CPP / C++ / C Code:
    const int size = 10;
    Cell cellArray[size][size]; //constructing the 10x10 array of Cells

(But you will need another constructor --- with no parameters)

CPP / C++ / C Code:
public:
  Cell(){};

Regards,

Dave
  #7  
Old 20-Feb-2006, 15:54
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: Array of Objects


Your first code group looks alright. I'm not quite sure what you mean be 'how can i use my play function' though. If you mean how can you pass the Cell object to the function there are several ways to do this. You could declare Cell as a global variable so all functions could access it or you could pass it as a variable to the function. The way I would do it though is to pass it by refrence.

Code:
int play(class Cell &CurrentCell);
  #8  
Old 20-Feb-2006, 16:01
chr1zis chr1zis is offline
New Member
 
Join Date: Feb 2006
Posts: 6
chr1zis is on a distinguished road

Re: Array of Objects


well my play function is set up to where i send it an array. is there any way i can still send it an array of class objects ??
  #9  
Old 20-Feb-2006, 16:13
Chris.Dev's Avatar
Chris.Dev Chris.Dev is offline
Junior Member
 
Join Date: Jan 2005
Posts: 48
Chris.Dev will become famous soon enough

Re: Array of Objects


of course

CPP / C++ / C Code:

class Cell {
public:
	Cell(int a) {
		value = a;
	}
             int GetValue() { // since you declared value as private you
                                   // need a function to get its value
                 return value;
             }
private:
	int value;
};

int play(class Cell *cells) {
    int i;
    for(i=0;i<MAX_CELLS;++i) {
        //access the array just like any other
        cells[i].GetValue();
    }
}
Last edited by LuciWiz : 21-Feb-2006 at 01:42. Reason: Please insert your C++ code between [c++] & [/c++] tags
  #10  
Old 20-Feb-2006, 16:30
chr1zis chr1zis is offline
New Member
 
Join Date: Feb 2006
Posts: 6
chr1zis is on a distinguished road

Re: Array of Objects


i have to copy the values into a temp array so the final values will not be altered.. when i do this..

CPP / C++ / C Code:
    for(int i=0; i<10; i++)
     {
        for(int j=0; j<10; j++)
        {
            temp[i][j] = cellArray[i][j].getValue();    
        }
     }

it gives me 2 errors on the line where i try to set the value that is in cellArray to the same spot in the temp array.. it says: 'class Cell' does not define this operator or a conversion to a type acceptable to the predefined operator. and it also says: '.getValue' must have class/struct/union type..
Last edited by LuciWiz : 21-Feb-2006 at 01:45. Reason: Please insert your C++ code between [c++] & [/c++] tags
 
 

Recent GIDBlogPython ebook by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer Usage in C++: Beginner to Advanced varunhome C++ Forum 0 19-Aug-2005 09:25
Merge and Heap...which is really faster silicon C++ Forum 0 10-May-2005 13:46
Help with an array using pointers glulu76 C++ Forum 1 01-May-2005 11:21
template comiling problems - need expert debugger! crq C++ Forum 1 01-Feb-2005 21:26

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:47.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.