Thread: Spacing out 2d Array

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    16

    Spacing out 2d Array

    Hello all. I am currently working on a project in which i use a 2d array to hold a representation of a map for the game snake. I draw the map in a file and then read it into the 2d array. I read it in fine, because i can cout it but then heres where my problem occurs. It just cout's it in a line, instead of how i had it written in the file. Is there anyway to make it so it looks exactly like whats in the file? Heres my code for reading it in and outputting it. Thanks for any help!

    COL and ROW are const int's with the max size of the 2d array.

    Code:
    for(row=0;row<ROW;row++){
    	for(col=0;col<COL;col++){
    		in >> array[row][col];
    		cout << array[row][col] << endl;
    		}
    	}
    Last edited by trev456; 04-26-2007 at 09:14 PM. Reason: misspelling

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,005
    Quote Originally Posted by trev456 View Post
    I am currently working on a project in which i use a 2d array to hold a representation of a map for the game snake. I draw the map in a file and then read it into the 2d array. I read it in fine, because i can cout it but then heres where my problem occurs. It just cout's it in a line, instead of how i had it written in the file. Is there anyway to make it so it looks exactly like whats in the file?
    Better explanations of the problem are better.. And in the future, remember to post data declarations -- quite often that is the key to the problem.
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User
    Join Date
    Dec 2005
    Location
    Canada
    Posts
    267
    take out endl

    OS: Windows 7, XUbuntu 11.10, Arch Linux
    IDE: CodeBlocks
    Compiler: GCC

  4. #4
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    You might have to put the endl outside the inner for loop, if each row is on a separate line.

  5. #5
    Registered User
    Join Date
    Apr 2007
    Posts
    16
    Ahh yes that would be it =) Thank you so much all and ill try to post my data declarations next time!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. from 2D array to 1D array
    By cfdprogrammer in forum C Programming
    Replies: 17
    Last Post: 03-24-2009, 10:33 AM
  2. Help with mallocing a 2d array please?
    By Gatt9 in forum C Programming
    Replies: 5
    Last Post: 10-10-2008, 03:45 AM
  3. 2D array pointer?
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 04-23-2006, 08:16 AM
  4. Read file in 2D array
    By Chook in forum C Programming
    Replies: 1
    Last Post: 05-08-2005, 12:39 PM
  5. Class Template Trouble
    By pliang in forum C++ Programming
    Replies: 4
    Last Post: 04-21-2005, 04:15 AM