#Need help reading a data file into a vector of a class

16 messages · Page 1 of 1 (latest)

fervent wadi
#

im not sure how to read the data from the file and store it into my vector i can only get the last line to print out and dont know know to save them to the individual class variables

zenith capeBOT
#

When your question is answered use !solved to mark the question as resolved.

Remember to ask specific questions, provide necessary details, and reduce your question to its simplest form. For tips on how to ask a good question use !howto ask.

timid thicket
#

1: this format is awful

#

2: you can use: binary, json, own format

#

most easier way is use json, easy to use lib here is Json for Modern c++

#

this lib is header only and you can just include file

#

there exist to json and from json, but not care about it now

just do proper serialize de-serialize of variables

#
std::string yourStringThatEntireFile = readAllFromFile(fileWithData)
auto parsetJson = json::parse(yourStringThatEntireFile);
for (auto& json : parsed file)
{
gamesClass object; 
  object.set... = json["variablename"];
    object.push_back(gamesClass); 
}
#

find guides about json format

#

idk, seems like basics for me, rly check some guides

fervent wadi
# timid thicket 1: this format is awful

while (gameData >> gameId >> name >> rDate >> rating) {
gamesClass game();
game.gameId = gameId;
game.name = name;
game.rDate = rDate;
game.rating = rating;
gameInfo.push_back(game); ive got this for reading the data into a vector but im getting an error that says 56 14 D:\Dtobe\Documents\project 1 cs.cpp [Error] request for member 'gameId' in 'game', which is of non-class type 'gamesClass()'

timid thicket
#
  1. your syntax incorrect
  2. use of stream unformatted incorrect
  3. dont use unformatted streams at all
  4. USE JSON IT EASY
#

;compile

class cl{};
int main(){
   cl obj(); // error
}```
past nebulaBOT
#
Compiler Output
<source>: In function 'int main()':
<source>:3:10: warning: empty parentheses were disambiguated as a function declaration [-Wvexing-parse]
    3 |    cl obj(); // error
      |          ^~
<source>:3:10: note: remove parentheses to default-initialize a variable
    3 |    cl obj(); // error
      |          ^~
      |          --
<source>:3:10: note: or replace parentheses with braces to aggregate-initialize a variable
acoustic spire
#

You are redefining gamesClass object(); everytime in the loop. This should be outside of the loop. As a result, lastime you enter the loop you just have a final line in your object.