'is it possible to output different values of different values from a class array into a file
We are doing an assignment with classes and objects and all that messy stuff.
I'm having a problem in which, when the user is done editing the array (adding or deleting data, blah blah blah), I need to update the .dat file with the values in the array for later use. I'm having a bit of trouble outputting the values into the file.
Any tips?
if this question has already been answered, just link me to the source and I'll delete this question.
ALSO, PLEASE NOTE THAT THE LIST ARRAY VARIABLE IN THE CODE IS FROM A CLASS.
Here is the test data (values are string, int, int, int, int, in that order):
MAN 5 9 2 89
LOL 4 3 8 23
SAP 7 2 2 34
And here is my code:
string name;
int gamesPlayed, goalsFor, goalsAgainst, points;
while (outfile << name << gamesPlayed << goalsFor << goalsAgainst << points)
{
for (int i = 0; i < numofclubs; i++)
{
outfile = list[i];
}
}
outfile.close();
Solution 1:[1]
You are not writing any delimiters between your values in the file. And, inside your for loop, you are trying to assign a value to the ofstream itself, rather than write the value to the file managed by the ofstream.
Also, you say the data is in an array, but you didn't show what that array actually looks like. Your while loop is not looping through any array. And your for loop is writing out club information that is not consistent with the desired file format you have described.
You probably need something more like this instead:
class Club
{
string name;
int gamesPlayed, goalsFor, goalsAgainst, points;
};
Club* list;
int numofclubs;
...
ofstream outfile("output.dat");
for (int i = 0; i < numofclubs; ++i)
{
outfile << list[i].name << ' '
<< list[i].gamesPlayed << ' '
<< list[i].goalsFor << ' '
<< list[i].goalsAgainst << ' '
<< list[i].points << '\n';
}
outfile.close();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
