#Need help reading a data file into a vector of a class
16 messages · Page 1 of 1 (latest)
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.
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
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()'
- your syntax incorrect
- use of stream unformatted incorrect
- dont use unformatted streams at all
- USE JSON IT EASY
;compile
class cl{};
int main(){
cl obj(); // error
}```
<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
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.