Loading This Map Array

This is a discussion on Loading This Map Array within the C++ Programming forums, part of the General Programming Boards category; Basically, I'm loading up a 12 X 16 array of 0's, 1's and 2's from a text file. I want ...

  1. #1
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Question Loading This Map Array

    Basically, I'm loading up a 12 X 16 array of 0's, 1's and 2's from a text file. I want to assign each of those to a spot in the array.

    This is what the text file looks like:

    Code:
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 1 1 0 0 0 0 0 0 1
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1


    This is my code so far to cycle through the array:

    Code:
    #include <fstream.h>
    
    char map[12][16];
    
    void main()
    {
    	ofstream file("test.txt");
    	for(int y=0,y<13,y++)
    	{
    		for(int x=0,x<17,x++)
    		{
    			map[y][x]=
    		}
    	}
    	file.close();
    }

  2. #2
    Toaster Zach L.'s Avatar
    Join Date
    Aug 2001
    Posts
    2,686
    Your loops go one too far, for one thing.
    The word rap as it applies to music is the result of a peculiar phonological rule which has stripped the word of its initial voiceless velar stop.

  3. #3
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826

    Re: Loading This Map Array

    Originally posted by drdroid
    This is my code so far to cycle through the array:

    #include <fstream.h>

    char map[12][16];

    void main()
    1) 'void main' - read the FAQ. This is wrong.
    2) read you C++ book on functions to read characters from a file.

    Seriously, take two seconds and look it up. You obviously found out how to open a file, so turn the page and learn how to read.

    Quzah.
    Hope is the first step on the road to disappointment.

  4. #4
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669
    hmmm... Now that I look back at it. I realise that it says nothing like that. I wonder if that's why I asked it on the board? Could it be? I.. Have.. No.. Idea?

    I'd apologize for being rude, but you had it coming.

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by drdroid
    hmmm... Now that I look back at it. I realise that it says nothing like that. I wonder if that's why I asked it on the board? Could it be? I.. Have.. No.. Idea?

    I'd apologize for being rude, but you had it coming.
    You're telling me your source of information that covers opening files says absolutely nothing about reading from them? Bull$$$$.

    Even something as basic as the man pages for things like 'fopen' reference other file related functions.

    I mean really, god damn, use two seconds and a search engine and the first link gave me the answer to your question. And my keywords had absolutely nothing to do with putting the information into an array.

    Quzah.
    Hope is the first step on the road to disappointment.

  6. #6
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    Your syntax for the for loops is incorrect. It should be:
    Code:
    for(int y=0;y<size;++y)
    {
            rest of your code
    }
    You put commas instead of semicolons, and you also go one farther than you should. To read in from a file, you treat it exactly like you were going to read in from the console.
    Code:
    file>>data;//to read to a \n (new line)
    file.get(map[x][y]);//to get one character at a time and put it in map[x][y]
    You should also be using the new standard headers, which include everything in the standard namespace.
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    For more on namespaces, there should be something in the tutorial.

  7. #7
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    Error:

    I get the error:

    Code:
    D:\Programs\PROJECTS\Map Loader\Main.cpp(15) : error C2039: 'get' : is not a member of 'basic_ofstream<char,struct std::char_traits<char> >'

  8. #8
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77
    I'm guessing you didn't #include <iostream>. I believe that is where it is declared.

  9. #9
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...?

    Here's my code:

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    char map[12][16];
    char info;
    
    void main()
    {
    	ofstream data("test.txt");
    	for(int y=0;y<13;y++)
    	{
    		for(int x=0;x<17;x++)
    		{
    			data.get(map[y][x]);
    			cout << map[y][x];
    		}
    		data << info;
    		cout << endl;
    	}
    	data.close();
    }

  10. #10
    Registered User HaLCy0n's Avatar
    Join Date
    Mar 2003
    Posts
    77

    Re: ...?

    Code:
    int main() //should be int
    {
    	ifstream data("test.txt"); //should be ifstream
    	for(int y=0;y<12;y++)//should be the size
    	{
    		for(int x=0;x<16;x++)//same here
    A few errors.

  11. #11
    Student drdroid's Avatar
    Join Date
    Feb 2002
    Location
    Montreal, Quebec
    Posts
    669

    ...

    It not loading right. I get all 0's.

    Code:
    int loadmap()
    {
    	ifstream data("test.txt");
    	for(int scany=0;scany<18;scany++)
    	{
    		for(int scanx=0;scanx<25;scanx++)
    		{
    			data.get(map[scany][scanx]);
    		}
    	}
    	data.close();
    	return 0;
    }
    Btw, I also changed the map.

    Code:
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1
    1 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 2 1

  12. #12
    Registered User
    Join Date
    Jan 2003
    Posts
    648
    Use this instead...

    data >> map[scany][scanx];

    And why isn't it (x, y)? You're doing the opposite now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Polynomials and ADT's
    By Emeighty in forum C++ Programming
    Replies: 20
    Last Post: 08-19-2008, 09:32 AM
  2. Unknown Memory Leak in Init() Function
    By CodeHacker in forum Windows Programming
    Replies: 3
    Last Post: 07-09-2004, 10:54 AM
  3. Merge sort please
    By vasanth in forum C Programming
    Replies: 2
    Last Post: 11-09-2003, 12:09 PM
  4. loading map and rendering to the screen???
    By werdy666 in forum Game Programming
    Replies: 4
    Last Post: 10-26-2002, 04:09 AM
  5. Hi, could someone help me with arrays?
    By goodn in forum C Programming
    Replies: 20
    Last Post: 10-18-2001, 10:48 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21