Loading number from a file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JoeC

    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<fstrea m>

    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(){retu rn xlen;}
    int GetSizeY(){retu rn ylen;}
    };

    #endif

    The functions:

    #include "board.h"

    board::board(){

    create();
    }

    void board::create() {

    fill();
    }

    ifstream& board::cfill(if stream& 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<iostre am>
    #include<fstrea m>

    #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.Ge tSpace(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 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 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

    Each number eventually represent a kind of terrain. For starters I am
    just trying to Create the map.

  • JoeC

    #2
    Re: Loading number from a file

    Never mind, I had the file named wrong.

    Comment

    Working...