You are here:

C++/Function not working.

Advertisement


Question
Okay, I'm writing an ascii 'maze' game. The maze isn't getting output properly. There's a bunch of weird characters and all wrong. I've narrowed the problem (i think) down to the following function:

#include <fstream>
#include <string>
#include <vector>
#include <iostream>
#include "terrain.h"


using namespace Game;

terrain::terrain (std::string fileName) {
  
  std::ifstream in(fileName.c_str());
  
  if (!in) {
  
     throw GameException("Cannot open terrain file.");
  }
  
  else {
  
     std::string entry;
     std::string longFile;
     std::vector<string> stringVect;
     
     while (getline(in, entry)) {
        
        if (entry.size() > longFile.size()) {
         longFile = entry;
        }
        
        stringVect.push_back(entry);
     }
     
     width = longFile.size();
     height = stringVect.size();
  }
  
  in.seekg (0);
  terrainArray = new Array2D<char>(width, height);

  char passTo;
  while (in.get(passTo)) {
     
     for(
        unsigned y = 0;
        y < height;
        y++
     ) {
        for(
         unsigned x = 0;
         x < width;
         x++
        ) {
         terrainArray->Put(x, y, passTo);
         
        }
     }
  }
  in.close();
}

Answer
I dont understand why you're doing in.seekg(0)? Probably that is the problem. If not, please get back to me with more details as to what is the format of the input file and what is the program trying to do. As it is, I dont have much idea and my answer is just a guess.

A note about your style: it is not good to have such a large construct, also dependent on external resources like file reading. Maybe you can move it to a separate function init().

C++

All Answers


Answers by Expert:


Ask Experts

Volunteer


Amit Kumar

Expertise

I can answer Cplusplus language and library questions, including STL, ACE, Boost. I have a good background in Mathematics.

Experience

Programming in CPP for about 8 years. Industry experience of 4 years.

Education/Credentials
Bachelors and Masters in Computer Science from Indian Institute of Technology Delhi.

©2016 About.com. All rights reserved.