Saving Data

in the following code, it runs but does not save data for some reason, can someone help me fix the problem?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
int main(int argc, char *argv[])
{
ofstream saveData;
ifstream takeData;
takeData.open("data.dat");
string PlayAgain;
     Player1();
     replay:
     YourType();
     OponentType();
     TypeAdvantages();
     Rules();
     Battlephase();
     Earned();
     Level();
     cout << "Would you like to play again? y o n"<< endl;
     cin >> PlayAgain;
     if(PlayAgain == "y")
     {
     goto replay;
     }
     takeData.close();
     saveData.open("data.dat");
     saveData.close();
     system("PAUSE"); 
	 return 0;
}

code for the test, which took save data...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <ctime>
#include <string>
#include <sstream>
#include <cstdlib>
#include <vector>
# include <fstream>
using namespace std;

int main(int argc, char *argv[])
{
string name;
ofstream saveData;
ifstream takeData;
takeData.open("data.dat");
takeData >> name;
takeData.close();
cout << "The last person who used this program was named: " << name;
cout << "\nWhat is your name?";
cin >> name;
saveData.open("data.dat");
saveData << name;
saveData.close();
return 0;
}
In your first example, notice how you are opening and closing saveData and takeData without streaming anything into them, or even passing pointers to functions/methods.
how would i put a pointer to a function in it? (for example, both the data from function level and function earned)
Last edited on
And don't use goto's they are really bad and evil. Forget they even exist. Use a loop instead.
Note in the second example.
saveData << name;
That is streaming the 'name' variable into the saveData file stream (writing it to the file).

You are not saving or loading ANY data in the first example.
i tried doing that and its not working with the pointers to my function

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
int main(int argc, char *argv[])
{
string PlayAgain;
ofstream saveData;
ifstream takeData;
takeData.open("data.dat");
takeData >> Level;
takeData.close();
     Player1();
     replay:
     YourType();
     OponentType();
     TypeAdvantages();//yet to return a value
     Rules();
     Battlephase();
     Earned();
     Level();
     cout << "Would you like to play again? y o n"<< endl;
     cin >> PlayAgain;
     if(PlayAgain == "y")
     {
     goto replay;
     }
     system("PAUSE");
     saveData.open("data.dat");
     saveData >> Level;
     saveData.close(); 
	 return 0;
}


ERROR MESSAGES

484 C:\Documents and Settings\User\My Documents\myFightingGame.cpp ambiguous overload for 'operator>>' in 'takeData >> Level'

503 C:\Documents and Settings\User\My Documents\myFightingGame.cpp no match for 'operator>>' in 'saveData >> Level'
Last edited on
I think you should read over some of the tutorials again.

http://cplusplus.com/doc/tutorial/

- Basic Input/Output
- Pointers

http://www.java2s.com/Tutorial/C/0160__Function/Usepointerasfunctionparameter.htm
does not tell me how to save a file of defined functions in it ( level(), and earned() ,i want stored after the program ends)
You don't want to save a function pointer in a data file. If you recompile your program, the address of the function will change and the address in the file will no longer be valid and will cause undefined behavior (probably a crash) when you run.

Also, your error messages are a result of your giving the same name to a variable and a function. I cautioned you about doing this in an earlier thread.
Line 7: You're trying to read into a variable named Level.
Line 17: You're calling a function named Level.
Line 26: You're trying to output Level. The variable or the address of the function? The compiler doesn't know which.
Stylistic suggestion: Make your variable names lower case. Use name case (initial or mixed caps) for functions.

thanks for the advice, i just newly learned saving because i wanted to start learning how to save my program for my game, ill definately use your stylistic suggestion, but do you how i would get around doing that so when i run my program again, the level and earned function will update?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//-----------Globals in the process of being removed----------
long Earnings = 10;
long Money = 0;
long AmountExp = 10;
long TotalExp = 0;
long NewLevel = 1;
long LevelUp = 30;
long SkillPoints = 3;
long TotalSkillPoints = 0;
//------------------------------------------------------------------------
void Earned()
{
     Money += Earnings;
     cout << "You earned $" << Earnings << " and " << AmountExp << " exp.\n";
     TotalExp += AmountExp;
     pause(3);
     cout << "You now have $" << Money << " and " << TotalExp << " exp.\n";
     pause(5);
     system("CLS");
}
//------------------------------------------------------------------------------
void Level()
{
            if(TotalExp >= LevelUp)
            {
LevelUp: 
            NewLevel += + 1;
            LevelUp = LevelUp * 2 + 10;
            TotalSkillPoints += + SkillPoints;
            cout << "You leveled up to level " << NewLevel << " and gained " << SkillPoints << " skill points.\n";
            pause(5);
            cout << "You now have " << TotalSkillPoints << " unused skill points.\n";

            pause(5);
            }
            if(TotalExp >= LevelUp)
            {
            cout << "...\n";
            pause(1);
            goto LevelUp;
            }
            pause(5);
            system("CLS");
}
Last edited on
You need to decide the key variables you want to save across games. Once you decided that, then you can write those variables to a file. When you restart a game, simply read the data from that file back into the same variables. One easy way to do that is to put the key variables in a struct, then simply write the struct out and read the struct back in. That also applies to classes. You can have a method in each of your classes that is responsible for archiving the instance of the class to disk, and another method that restores the state of the class from disk.

A couple of problems in the code you posted:
1) You're using LevelUp ambiguously. At line 7, it's a variable. At line 26, it's a label.
2) Gotos are a bad practice and should be avoided. Use a loop instead.
can you show me an example of using a struct to achieve my purpose? if i just wanted to save money, totalexp, levelup, newlevel and skillpoints?
Try something like this
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
struct SaveData
{
  int money, totalexp, levelup, newlevel, skillpoints;
};

// Overload global operators << and >> to use them with SaveData
ofstream& operator <<(ofstream& out, SaveData& save)
{
  out << save.money << ' ' << save.totalexp << ' ' << save.levelup << ' ' << save.newlevel << ' ' << save.skillpoints;
  return out;
}

ifstream& operator >>(ifstream& in, SaveData& save)
{
  in >> save.money >> save.totalexp >> save.levelup >> save.newlevel >> save.skillpoints;
  return in;

It may need adjustments and can be improved
Last edited on
thanks
Topic archived. No new replies allowed.