0

I'm making a game where I want to read from a .txt the attributes of the monsters. When I read the file and put the values into variables, those variables get garbage data instead of the .txt data. It's weird because it's only happening in my main project. I created a test project with the same class and literally copypasted the code, and there it works perfectly.

Here is my file reading method:

void Game::readMonsters() {

string monsterName, monsterFile = "";
int life, def, id = NULL;

ifstream monsters("assets/monsters.txt");

if (monsters) {

    monsters >> nMonsterTypes; // this variable exists in Game.h

    type = new MonsterType[nMonsterTypes](); // this object exists in Game.h


    for (int i = 0; i < nMonsterTypes; i++) {

        // all those variables are being set as memory garbage:

        monsters >> id;
        type[i].setID(id);

        monsters >> life;
        type[i].setHP(life);

        monsters >> def;
        type[i].setDefense(def);

        monsters >> monsterFile;
        type[i].setFile(monsterFile);

        monsters >> monsterName;
        type[i].setName(monsterName);

    }
}

monsters.close();

}

I appreciate any help!

  • 2
    I would try putting an else statement and output some message to see if you are even entering that if block. It's likely that you aren't opening the file, probably a pathing issue. Also, you probably don't want to be setting ints to NULL – ricco19 Apr 24 '18 at 18:22
  • If the file isn't opening, wouldn't the variables be null? – Anderson Pillar Apr 24 '18 at 18:27
  • Try initializing to something real like "1234" and see what happens. You should not be setting non-pointers to NULL anyways, use 0 or something like that, I can believe that even compiles – ricco19 Apr 24 '18 at 18:31
  • It still sets values like "-33686019" for the ints and "ؽ4\r(<–" for the strings – Anderson Pillar Apr 24 '18 at 18:33
  • [I'm afraid not.](http://en.cppreference.com/w/cpp/io/basic_istream/operator_gtgt2) You always need to test the stream state after any IO transaction to make sure the transaction succeeded. [You can also turn on exceptions for stream failures](http://en.cppreference.com/w/cpp/io/basic_ios/exceptions), but this is often more trouble than it is worth. – user4581301 Apr 24 '18 at 18:35
  • Also watch out for (incoming search term) working directory. Many development environments change the folder the program is being run from to make development of multiple builds simpler. This means that while the executable and assets folder are in project/debug, the program is run from and looks for the asset folder in project so that project/release and project/experimental can use the same assets – user4581301 Apr 24 '18 at 18:39
  • Helpful reading: [How do I get the directory that a program is running from?](https://stackoverflow.com/questions/143174/how-do-i-get-the-directory-that-a-program-is-running-from) – user4581301 Apr 24 '18 at 18:41
  • A warning about `monsters >> nMonsterTypes; // this variable exists in Game.h` You do not want variables defined in header files. If multiple t[ranslation units](https://stackoverflow.com/questions/1106149/what-is-a-translation-unit-in-c) include that header they all have the variable and the linker doesn't even try to resolve which is the real variable; [it just spits out an error](https://stackoverflow.com/questions/4192170/what-exactly-is-one-definition-rule-in-c) and refuses to build the program. – user4581301 Apr 24 '18 at 18:47
  • @user4581301 so what's the purpose of private attributes? – Anderson Pillar Apr 24 '18 at 18:52
  • If it's a member variable then that's completely fine. If you can confirm it's actually opening the file (try using an *exact* path to the file rather than relative) then it will be hard to help without more code/the actual text file – ricco19 Apr 24 '18 at 18:55
  • @ricco19 `if (monsters)` should be making sure the file opened. – user4581301 Apr 24 '18 at 18:56
  • 1
    But with no else statement theres no error handling or indication of success/fail here, the fact it works in another projects leads me to believe it's almost certainly a pathing issue – ricco19 Apr 24 '18 at 18:57
  • @AndersonPillar `nMonsterTypes` is a member of `Game`? That would be all right then. My apologies for misunderstanding. – user4581301 Apr 24 '18 at 18:57
  • @ricco19 You have me there sir. I should have taken a closer look at your opening comment. – user4581301 Apr 24 '18 at 18:59

1 Answers1

0

Using a breakpoint with Visual Studio I noticed the error was not in the reading, but in the get method of the "type" array. I was making a rand between 0 and nMonsterTypes when I should do between 0 and nMonsterTypes - 1 (to choose a random position in the array). Got good advices here though, thank you all!