473,322 Members | 1,496 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

Loading number from a file

I am trying to create a simple map game and my map is based on a grid
of intigers. My map for some reason is not working well. I am not
sure what is wrong. I wrote a simple check program but outut dosn't
match my map of numbers.

Here is the object that holds the grid:

#include<fstream>

using namespace std;

#ifndef BOARD_H
#define BOARD_H

class board{

static const int xlen = 30;
static const int ylen = 25;
int b[ylen][xlen];
void create();
ifstream& cfill(ifstream& in, int& code); /*Reads map from file */
void fill();
public:
board();
int GetSpace(const int x, const int y){return b[y][x];}
int GetSizeX(){return xlen;}
int GetSizeY(){return ylen;}
};

#endif

The functions:

#include "board.h"

board::board(){

create();
}

void board::create(){

fill();
}

ifstream& board::cfill(ifstream& in, int& code){

in>>code;
return in;
}

void board::fill(){
int terrain;
ifstream f ("map.txt");

for(int l2 = 0; l2 != ylen; l2++){
for(int l1 = 0; l1 != xlen; l1++){
if(cfill(f, terrain)){
b[l2][l1] = terrain;
}
}
}
}

Finally the test program:

#include<iostream>
#include<fstream>

#include"board.h"

board b;

int main(){

for(int y = 0; y != b.GetSizeY(); y++){
for(int x = 0; x != b.GetSizeX(); x++){
std::cout<<b.GetSpace(y,x);

}
std::cout<<endl;
}
system("pause");
return 0;
}

My Data file:

1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 0 0 1 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 1 1 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 1 1 1
1 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 1 0 1 1
1 0 1 0 0 0 1 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 0 0 1
1 0 0 4 0 0 1 0 0 1 0 1 0 2 2 0 0 0 0 0 0 0 0 0 0 1
1 0 0 0 0 0 1 0 0 0 0 0 0 2 2 2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. For starters I am
just trying to Create the map.

Aug 12 '06 #1
1 1152
Never mind, I had the file named wrong.

Aug 12 '06 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: MFA | last post by:
Hi All I have one ASP page. During opening this page its taking some time to open as its has to process some asp code. I want to display some animated gif or text showing that page is loading...
3
by: Roy Wang | last post by:
hi, My problem is how to determining when the XML file has loaded using javascript. I loaded an xml file using javascript in a web page. The code below is loading the xml file for IE:...
5
by: Olaf Gschweng | last post by:
We're new into DB2 and have some problem with DB2 8.1 (?) on a Linux system. We load some big tables of a DB2 database from files every day. We do a "DELETE FROM table" for each table and then we...
1
by: Steven Blair | last post by:
Hi, Got a general query regarding XML and Databases. To be honest, I really don't kno that much about XML. Here is my problem: I have a XMl file containing a number of transactions in a...
4
by: Matt Sawyer | last post by:
I am attempting to use an API (CxApiOem.dll) that has a large number of defines and complicated structs. It's just too much hassle to attempt to use DLLImport to make the desired API calls. ...
10
by: eholz1 | last post by:
Hello Members, I am setting up a photo website. I have decided to use PHP and MySQL. I can load jpeg files into the table (medium blob, or even longtext) and get the image(s) to display without...
5
by: yeoj13 | last post by:
Hello, I have a db2load script I'm using to populate a large table. Ideally, my target table is required to have "Not Null" constraints on a number of different columns. I've noticed a ...
20
by: Nickolai Leschov | last post by:
Hello all, I am programming an embedded controller that has a 'C' library for using its system functions (I/O, timers, all the specific devices). The supplied library has .LIB and .H files. ...
5
by: s0suk3 | last post by:
Hi, I wanted to know how cautious it is to do something like: f = file("filename", "rb") f.read() for a possibly huge file. When calling f.read(), and not doing anything with the return...
3
by: Leighya | last post by:
Im currently working on this xml file but when i load it to Mozilla, i got an error "Error Loading Stylesheet: Xpath parse failure: invalid variable name" It loads on IE properly. Only with the...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.