Saving a text game
I'm having some problems using the fstream.h header file to save a person's game in my text casino game. We've used examples from various books, but they never seem to work in the main source for the game. It's giving me 2 errors. Here they are.
error C2065: 'fout' : undeclared identifier
warning C4552: '<<' : operator has no effect; expected operator with side-effect
I'm also using the iostream header file. I think I've heard about some conflicts caused by it, but I'm not sure if they are real are just made up.
Anyways, thanks to anybody for any help.
you will need to include either fstream.h (or fstream depending on your compiler) in order to use an ofstream (which is usually what fout is) in your program.
The first error says the compiler can't use fout because it hasn't been declared as a valid object.
The second error says you haven't successfully overloaded the << operator for the class you are using. It may "magically" go away if you correct the first problem.
disclaimer: code untested.
const int MAX = 10;
//overload the << operator for the class sample using friend
friend ostream & ostream<<(ostream & os, sample & s);
//definition of overloaded << operator for class sample
ostream & ostream<< (ostream & os, sample & s)
os << s.data << endl << s.name << endl;
sample1.data = 1;
fout << sample1;
I have included the fstream header file. I've been tinkering with it for quite a while, and nothing seems to be working yet. Anymore suggestions?
post an example of the code you have been using. If you are following the example I have posted it should work.
I thought you just had to do it this way:
ofstream fout("filename.txt"); // declare fout
fout << "SOME_INTERESTING_TEXT" << somevariable << "\n";// use it