Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Loading number from a file

Reply
Thread Tools

Loading number from a file

 
 
JoeC
Guest
Posts: n/a
 
      08-12-2006
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 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number eventually represent a kind of terrain. For starters I am
just trying to Create the map.

 
Reply With Quote
 
 
 
 
JoeC
Guest
Posts: n/a
 
      08-12-2006
Never mind, I had the file named wrong.

 
Reply With Quote
 
 
 
Reply

Thread Tools

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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
order of iframe loading with document loading ofir Javascript 0 12-03-2007 12:06 PM
loading image -> detect when image is done loading edfialk Javascript 0 05-10-2007 07:28 PM
[OT] Is loading the second Java application faster than loading the first? David Segall Java 2 01-02-2007 04:41 PM
OT: Number Nine, Number Nine, Number Nine FrisbeeŽ MCSE 37 09-26-2005 04:06 PM
Image loading using javascript. Handling timeouts and parrallel loading under IE zborisau@gmail.com Javascript 4 08-28-2005 02:02 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57