2 Replies - 278 Views - Last Post: 03 September 2013 - 04:58 PM Rate Topic: -----

#1 defjamvan  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 29
  • Joined: 02-September 13

Multidimensional arrays contain garbage?

Posted 03 September 2013 - 09:02 AM

I have two arrays. My first array(3D) contains data(strings) read from file. My second array(2D) is copied from the 3D.
My program is that when I output my 3D array to screen, the last two values contain garbage. When I output my 2D array, there's alot more garbage in it. My datafile contains 208 values(strings). How do I get rid of the garbage.
Read into a 3D:
void readFile( char Udeck[5][42][13] )
{
  ifstream fin;
  int i, j;
  fin.open( "datafile.txt");
  
  for( i = 0; i < 5; i++ )
	 {
	   for (j = 0; j < 42; j++ )
	    {
	      fin >> Udeck[i][j];
	  
	    
	    }
	 }
	
}




Display 3D
void displayUnshufedDeck(  char Udeck[5][42][13] )
{
	int i = 0;
	int j = 0;
	
	for( i = 0; i < 5; i++ )
	 {
	   for (j = 0; j < 42; j++ )
	    {
	    
	      //cout << "Udeck[" << i << "][" << j << "] = " << Udeck[i][j] << endl;
	    cout << Udeck[i][j] << " ";
	    
	    }
	 }
	 
}



Copying from 3D to 2D
void StrCpy ( char dest[210][13], char source[5][42][13] )
 {
   
   for(int i = 0; i < 5; i++)
     { 
      for (int j = 0; j < 42; j++)
       {
         for ( int k = 0; k < 13; k++ )
    	  {
		  
            dest[i*42+j][k] = source[i][j][k];
            cout << dest[i*42+j] << " ";
    	  }
		  
         
		 
       }
     }
}



Is This A Good Question/Topic? 0
  • +

Replies To: Multidimensional arrays contain garbage?

#2 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1362
  • View blog
  • Posts: 4,726
  • Joined: 19-February 09

Re: Multidimensional arrays contain garbage?

Posted 03 September 2013 - 10:08 AM

Hi, 5 * 42 is 210.

Do you want 4 * 52 (208)?
Was This Post Helpful? 0
  • +
  • -

#3 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 3616
  • View blog
  • Posts: 11,263
  • Joined: 05-May 12

Re: Multidimensional arrays contain garbage?

Posted 03 September 2013 - 04:58 PM

Don't you already have a thread about this here? http://www.dreaminco...ontain-garbage/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1