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.
108 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.
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
how would you use a getline in this situation
would a getline read all 4 data types?
std::string line_of_data;
while (std::getline(Gamedata, line_ofdata)) {
}
yea
It would be one line of string, so I think you would have to find a way to parse it and change the types
!code
```cpp
int main() {}
```
int main() {}
!f
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?
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
@brittle dune Has your question been resolved? If so, type !solved :)
yea im having trouble figuring out how to push the data into the vector
oh my bad, you don't even need a loop inside the while
you can just push back to the vector
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
btw from here, you have to change
cout << temp;
to iterating through the vector and using the getters to print out the values. You don't have to do this but this is just to make sure your vector of objects have the right values.
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
Btw, what resource/resources are you using to learn C++
A good resource will help you understand all this code
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
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:
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
Can you copy paste your code?
yea
!code
```cpp
int main() {}
```
int main() {}
!f
#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;
}
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);
yes that line
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?
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?
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);
the temp is doing it's job well, which is to take an entire line from the TestMaster.txt file and add it to it's value
so changing it would be a bad idea
GameClass has 4 different variables and a couple of getters and setters. The only way to push back to a vector of GameClass objects would be to follow this example
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
would adding object read them into the vector?
what do you mean by read them into the vector
like could i use a pushback with the object to populate the vector
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
i originnaly tried gaamesClass temp and that didnt work is object diffrent than just a varible
it doesn't matter what you call it
maybe you misspelled something?
there's 2 a's here
oh wait
yea haha my a key is kinda broke it was spelled right
you can't have the same name as string temp
the compiler won't know whether to use the string temp or gamesClass temp
string temp wasnt in at the time
do i still need too keep temp and then also make a vector to store
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
yea haha i for sure could name better
it makes sense at first to me till theres a bunch of varibles
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
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?
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
trying to declare gamesClass object; is giving me an error do i need the whole vector<gamesClass> object;
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;
}
```cpp
int main() {}
```
int main() {}
back ticks are a bit hard to find but you might find them on top left part of keyboard
you're not using the string temp anywhere in your while loop. That's an important part of your code
What do you think string temp looks like during the first time/iteration of the while loop?
hint: look at TestMaster.txt
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?
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
would i be able to set my vector equal too temp as it goes through?
You didn't add anything to the member variables of
gamesClass Object
and yet you try to add it to the vector <gamesClass> gameInfo;
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
thank you
You welcome!