You are here:

C++/Function not working.


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;
     width = longFile.size();
     height = stringVect.size();
  in.seekg (0);
  terrainArray = new Array2D<char>(width, height);

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

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().


All Answers

Answers by Expert:

Ask Experts


Amit Kumar


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


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

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

©2017 All rights reserved.