in

Function not loading data into C++ vector (XCODE)


I am trying to load vector data from a file into vector Enemies but its not working. Enemies.empty() shows true. I tried to check within the functions where the issues are occuring using [Debug] and I think the csv file is not being read.

This is the output:

Default Attributes Given To Alger Waterlow
Alger Waterlow
  Strength: 2
   Dexterity: 2
  Intelligence: 2
  Defense: 2

[Debug]: 
[Debug]: File Data Printed!
[Debug]: Enemies is Null? 1
Program ended with exit code: 0

Here is Main.cpp:

#include "Characters.hpp"
#include "Enemies.hpp"
#include <fstream>

vector<Enemy> LoadEnemies(string filename){
    ifstream file(filename);
    string filedata;
    //Checking if file is being accessed
    file >> filedata;
    cout << "[Debug]: "<< filedata << endl;
    cout << "[Debug]: File Data Printed!" << endl;
    
    vector<Enemy> Enemies;
    
    int i = 0;
    for(string line; getline(file, line); ){
        
        if (i == 0){
            i++;
            continue;
        }
        Enemy temp = Enemy();
        temp.loadfromCSV(line);
        Enemies.push_back(temp);
    }
    file.close();
    return Enemies;
}

int main() {
    Character c = Character("Alger Waterlow");
    cout << c << endl;
    
    vector<Enemy> Enemies = LoadEnemies("Enemies.csv");

    cout << "[Debug]: Enemies is empty? " << Enemies.empty() <<endl;
    
    for(auto E : Enemies){
        cout << E << endl;
    }
    
    

    return 0;
}

Here is Enemies.cpp:

#include "Enemies.hpp"
#include "Utils.hpp"

void Enemy::loadfromCSV(string line){
    vector<string> tokens = tokenizecsvline(line);
    
    setname(tokens[0]);
    //cout << tokens[0];
    sethealth(stoi(tokens[1]));
    setstrength(stoi(tokens[2]));
    setdexterity(stoi(tokens[3]));
    setintelligence(stoi(tokens[4]));
    setdefense(stoi(tokens[5]));
    setlevel(stoi(tokens[6]));
    setexp(stoi(tokens[7]));
    cout << "[Debug]: LoadfromCSV function executed!" << endl;
}

Here is the Enemies Header file:

#ifndef Enemies_hpp
#define Enemies_hpp

#include "Characters.hpp"

class Enemy : public Character {
private:
    int exp_;
public:
    Enemy() : Character(), exp_{10}{}
    auto & exp() {return exp_;}
    void setexp(int exp) {exp_ = exp;}
    
    friend ostream& operator<<(ostream& os, Enemy& c){
        return os << c.name() << endl << "  "
        << "Strength: " << c.strength() << endl << "   "
        << "Dexterity: " << c.dexterity() << endl << "  "
        << "Intelligence: " << c.intelligence() << endl << "  "
        << "Defense: " << c.defense() << endl << "  "
        << "Exp: " << c.exp() << endl;
    }
    void loadfromCSV(string line);
};

#endif

Here is the Utils.hpp where I tokenize the csv data from Enemies.csv:

#ifndef Utils_hpp
#define Utils_hpp

#include <iostream>
#include <chrono>
#include <random>
#include <vector>
#include <sstream>
#include <string>

using namespace std;

vector<string> tokenizecsvline(string line){
    vector<string> token;
    stringstream linestream(line);
    string temp;
    
    while(getline(linestream, temp, ',')){
        token.push_back(temp);
    }
    cout << "[Debug]: tokenizecsvline function executed!" << endl;
    return token;
}


#endif

Let me know if you require any other pieces of my code. Not providing all of it because I think the issue is occuring in the provided code files. Thank you in advance!



Source: https://stackoverflow.com/questions/70552450/function-not-loading-data-into-c-vector-xcode

Simple Python script that dumps a Wikidot wiki forum into JSON structures

When ever I press certain button the app crashes and gives me this Error "Unexpectedly found nil while unwrapping an Optional value" how do i fix it?