#Reading a data file into a vector

108 messages · Page 1 of 1 (latest)

keen mangoBOT
#

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.

grand topaz
#

I would use other ways to read the data from the file instead of using eof

//you forgot the () after eof too
while(!gameData.eof())
#

you can use getline for example

#

As for creating the vector for gamesClass that makes sense since you need 4 objects based off the content in your TestMaster.txt

brittle dune
#

would a getline read all 4 data types?

grand topaz
#
std::string line_of_data;
while (std::getline(Gamedata, line_ofdata)) {

}
grand topaz
brittle dune
#

okay thank u will try that now

#

or am i cout the wrong thing

grand topaz
#

!code

keen mangoBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
grand topaz
#

!f

keen mangoBOT
#
int main() {
  ifstream gameData;
  gameData.open("TestMaster.txt");

  vector<gamesClass> gameInfo;
  string temp;

  while (getline(gameData, temp)) {
  }

  cout << temp;

  return 0;
}

its reading now how do i store the data into the vector its only displaying the last line of the file im assuming im gonna need push_back?

gentlecannibal
grand topaz
#

well, you're not doing anything inside of the while loop

#

you have to find a way to iterate through your vector and push the data from the text file into the vector

keen mangoBOT
#

@brittle dune Has your question been resolved? If so, type !solved :)

brittle dune
grand topaz
#

oh my bad, you don't even need a loop inside the while

#

you can just push back to the vector

brittle dune
#

yea i thought to use pushback but i tried gameInfo.push_back(temp) and that isnt working do i need to use a diffrent varible

grand topaz
#

"isn't working" is very vague

#

it's hard to help with just that information

grand topaz
#

I think you should change this first

#

and then try to work on parsing the temp string inside the while loop and assigning it to the member variables

grand topaz
#

A good resource will help you understand all this code

brittle dune
#

oh i get an error on the getback line it wont run and im taking a data structures class so just the material i get from there

keen mangoBOT
#
How to Learn C++ Programming

We generally recommend a good book to learn the necessary fundamentals:

To actually write and run C++ code, you will need a compiler, editor, and debugger. We strongly recommend to start out using an IDE, which will provide all these tools for you:

<:microsoft:1165512917047853127> Windows
  • [Visual Studio](#1165492293810257920 message)
  • CLion
<:tux:1165505626894520361> Linux
<:apple:1165508607798943754> Mac
grand topaz
#

The two books at the top are really good if you can afford them but if you can't use learncpp.com website to learn C++ since it's free

grand topaz
brittle dune
#

yea

grand topaz
#

!code

keen mangoBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
grand topaz
#

!f

keen mangoBOT
#
#include <fstream>
#include <iostream>
#include <vector>
using namespace std;

class gamesClass {
 public:
  int gameId;
  string name;
  string rDate;
  double rating;

  gamesClass(int _gameId, string _name, string _rDate, double _rating) {
    gameId = _gameId;
    name = _name;
    rDate = _rDate;
    rating = _rating;
  }

  // Getters
  int GetgameId() { return this->gameId; }
  string Getname() { return this->name; }
  string GetrDate() { return this->rDate; }
  double Geteating() { return this->rating; }

  // Setters
  void SetgameId(int gameId) { this->gameId = gameId; }
  void SetName(string name) { this->name = name; }
  void SetrDate(string rDate) { this->rDate = rDate; }
  void Setrating(double rating) { this->rating = rating; }
};

int main() {
  ifstream gameData;
  gameData.open("TestMaster.txt");

  int gameId;
  string name;
  string rDate;
  double rating;

  vector<gamesClass> gameInfo;
  string temp;

  while (getline(gameData, temp)) {
    gameInfo.push_back(temp);
  }

  cout << gameInfo;

  return 0;
}
gentlecannibal
grand topaz
#

I think stepping through the code with a debugger and by putting a breakline after the while loop will help you understand what is going on.

#

It's giving you an error on that line right?

#
gameInfo.push_back(temp);
brittle dune
#

yes that line

grand topaz
#

You have a vector of objects and you're trying to add a string to it

#

If you have a vector or array of ints how do you add to it?

brittle dune
#

i would just do a pushback with the int i need to add to it so temp would bve the wrong thing to pushback or do i need to somehow make the temp also a vector?

grand topaz
#

for the first part of what you said, you can either do

std::vector<int> nums;
nums.push_back(5);

or

std::vector<int> nums;
int num = 5;
nums.push_back(num);
grand topaz
#

so changing it would be a bad idea

grand topaz
#

kind of like this

 while (getline(gameData, temp)) {
    gamesClass object;
    //add more code
  }
#

does this make sense, or did I lose you?

#

I can try to explain it better

brittle dune
#

would adding object read them into the vector?

grand topaz
#

what do you mean by read them into the vector

brittle dune
#

like could i use a pushback with the object to populate the vector

grand topaz
#

yes

#

it is a vector of objects, after all

#

After you add the object, you need to parse the string temp

#

then assign values to the object

#

and finally push back to the vector of objects

brittle dune
#

i originnaly tried gaamesClass temp and that didnt work is object diffrent than just a varible

grand topaz
#

maybe you misspelled something?

brittle dune
#

yea haha my a key is kinda broke it was spelled right

grand topaz
#

you can't have the same name as string temp

#

the compiler won't know whether to use the string temp or gamesClass temp

brittle dune
#

string temp wasnt in at the time

#

do i still need too keep temp and then also make a vector to store

grand topaz
#

yea, the string temp is important

#

I think this proved that you need better naming habits for your variables

#

try to be more specific

#

like you can convert the string temp to string line

brittle dune
#

yea haha i for sure could name better

#

it makes sense at first to me till theres a bunch of varibles

grand topaz
#

lol

#

When you start working on large projects, you'll have 100s if not thousands of variables. So, specific names for variables will help a lot

brittle dune
#

yea i never have close to that many i also get confused with the constructors and like name, _name is there a better way for that?

grand topaz
#

Someone on this server got angry for me at recommending this and I know there's a good reason for it but just try to imagine that every variable in your class has m_ as a prefix

#

this makes it a member variable

#

meaning it belongs to the class

brittle dune
#

trying to declare gamesClass object; is giving me an error do i need the whole vector<gamesClass> object;

grand topaz
#

what's the error?

#

you can copy paste errors, if you didn't know btw

brittle dune
#

59 14 project 1 cs.cpp [Error] no matching function for call to 'gamesClass::gamesClass()'

#

int main()
{

 ifstream gameData;
 gameData.open("TestMaster.txt");
 
 
int gameId;
string name;
string rDate; 
double rating;

vector <gamesClass> gameInfo; 

string temp; 

while(getline(gameData,temp))
{
    gamesClass object; 
 gameInfo.push_back(object); 
}

cout << gameInfo; 



return 0; 

}

grand topaz
#

please follow this to make your code look good

#

!code

keen mangoBOT
#
How to Format Code on Discord
Markup

```cpp
int main() {}
```

Result
int main() {}
grand topaz
#

back ticks are a bit hard to find but you might find them on top left part of keyboard

grand topaz
#

What do you think string temp looks like during the first time/iteration of the while loop?

#

hint: look at TestMaster.txt

brittle dune
#

temp would just be the first line in the first iteration wouldnt it?

#

and the gamesClass object; still gives an error do i need to declare it as a vector again but the how would it be diffrent than the gameInfo vector?

grand topaz
#

the first iteration string temp would look like this:

53171    Bubble Bobble    1/1/1986    4.03

the second iteration

418467    Call of Duty: Warzone    3/10/2020    3.71

keeps going until the fourth row

brittle dune
#

would i be able to set my vector equal too temp as it goes through?

grand topaz
#

It's basically an empty object.

#

You need to use the string temp to add values to the member variables of gameClass Object and after it has values in all of it's member variables you can finally push back

#

I gotta go eat but I'll give you a link to something that can be useful

#

It's a bit long but it's worth it because you'll understand what you're doing

#

This is helpful too

brittle dune
#

thank you

grand topaz
#

You welcome!