Thread: Save function not working

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Save function not working

    Is this legal becuase it does not work, the file is created according to the argument passed into the function ok but after closing it is blank, if i do it without the if statements it works fine.

    Code:
    void SolBoard::SaveGame(int myfile)    
    {
        ofstream Gamefile;
    
        if(myfile == 0)                                 //which file to write over.
        ofstream Gamefile("mysave0.txt");
        if(myfile == 1)
        ofstream Gamefile("mysave1.txt");
        if(myfile == 2)
        ofstream Gamefile("mysave2.txt");
    
        for(countclick = 0; countclick < 49; countclick++)
        {
            Gamefile << moves_record[moves][countclick];
            Gamefile<< " ";
        }
        Gamefile.close();
    }

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should be using the open() member function instead of creating a new ofstream object in a scope local to the respective if statements. You should also consider what happens if myfile contains none of the values listed.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    ok thanks i will look up the function, i know i should put the error checking in too, file i/o does my swede in.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i got it, nice ta

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 05-13-2011, 08:28 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. Compiling sample DarkGDK Program
    By Phyxashun in forum Game Programming
    Replies: 6
    Last Post: 01-27-2009, 03:07 AM
  4. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  5. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 07:18 AM