Thread: Saving a text game

  1. #1
    Unregistered
    Guest

    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.

    -Chris

  2. #2
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    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.

    Code:
    #include <fstream.h>
    
    const int MAX = 10;
    
    class sample
    {
      public:
        int data;
        char name[MAX];
        //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;
      return os;
    }
    
    int main()
    {
      sample sample1;
      sample1.data = 1;
      strcpy(sample1.name, "Spot");
      ofstream fout("myFile.txt");
      fout << sample1;
      return 0;
    }
    disclaimer: code untested.

  3. #3
    Unregistered
    Guest
    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?

  4. #4
    Registered User
    Join Date
    Mar 2002
    Posts
    1,595
    post an example of the code you have been using. If you are following the example I have posted it should work.

  5. #5
    Pygmy Monkey ErionD's Avatar
    Join Date
    Feb 2002
    Posts
    408
    I thought you just had to do it this way:

    Code:
    ofstream fout("filename.txt"); // declare fout
    fout << "SOME_INTERESTING_TEXT" << somevariable << "\n";// use it

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Text based game
    By wipeout4wh in forum C Programming
    Replies: 12
    Last Post: 03-26-2009, 04:39 PM
  2. Text adventure game - desgin
    By beene in forum Game Programming
    Replies: 9
    Last Post: 05-12-2008, 07:51 PM
  3. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM
  4. My Pre-Alpha Version Rpg Text Game
    By knight543 in forum C++ Programming
    Replies: 1
    Last Post: 04-06-2002, 06:02 AM
  5. Text Based Game
    By drdroid in forum C++ Programming
    Replies: 2
    Last Post: 02-18-2002, 06:21 PM