ok so i am a hs student learning some c++ and recently learned the fstream function and tried to make a load save type thing for a programm i am courently makeing. as far as i can tell the coding is correct but it isn't saveing or loading the file right, any help would be greatly appreaciated as i and my teacher cannot find the error. the code is as follows.

#pragma hdrstop
#pragma argsused
#include <condefs.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <fstream.h>
#include <stdio.h>


//---------------------------------------------------------------------------

void loadsave ();

int main(int argc, char **argv)
{
loadsave();

        return 0;
}

void loadsave()
{
ofstream save;
ifstream load;
int choice;
int savedplayers;
char *savedgame = new char [100];
string convert;
char *filename = new char [50];
       load.open("savedplayers.dat");
       load >> savedplayers;
       load.close();
if (savedplayers >=1)
cout << "1: Load game" << endl;
cout << "2: Save game" << endl;
cin >> choice;
    if (choice == 1){
        for (int i = 1; i <= savedplayers; i++){
            convert += i;
            convert += ".dat";
            for (int j=0; j<=4; j++){
                filename[j] = convert[j];
            }
            load.open(filename);
            load >> savedgame;
            load.close();
            cout << i << ": " << savedgame << endl;

        }
        cin >> choice;
        convert += choice;
        convert += ".dat";
        for (int j=0; j<=4; j++){
                filename[j] = convert[j];
        }
        cout << filename;
        load.open(filename);
        load >> savedgame;
        load.close();
        cout << endl << endl << endl << endl << savedgame << endl;
        getch();
        loadsave();
    }
    else if (choice == 2){
        cout << "Save game as:";
        cin >> savedgame;
        load.open("savedplayers.dat");
        load >> savedplayers;
        load.close();
        savedplayers++;
        save.open("savedplayers.dat");
        save << savedplayers;
        convert[0] = savedplayers;
        convert += ".dat";
        for (int j=0; j<=4; j++){
            filename[j] = convert[j];
        }
        save.close();
        save.open(filename);
        save << savedgame;
        save.close();
        loadsave();
    }

}

o and i am running in windows, my compiler is borland c++ builder 3. and a run down of the attempt of code i used is to ask the user to load or save a file, just call that file a number according to how many there are and store inside of that file the name that they want to call it. when they try to open the file it will display the corisponding number with that file and display the name inside of that file so that the user can determine which file they want.

>void loadsave ();
It's best to avoid using one function for two wildly different operations.

>string convert;
I'll assume that string.h defines a string class on your old ass compiler. Otherwise this is a ghastly error.

>if (savedplayers >=1)
I'd bet my next paycheck that you want to enclose the body of this if statement in braces.

>convert += i;
>convert += ".dat";
You're going to get some wacky filenames if you don't clear the string after each iteration of the loop.

Jumping forward because I don't really want to discourage you by saying that every line is an error. ;)

>save.open("savedplayers.dat");
Do you really want to open the file for writing, or do you want to open it for appending? Opening for writing typically truncates the file to zero length while appending preserves existing contents.

>loadsave();
Repeat after me: Loop good, tail recursion bad.

I think I'll stop there. Go over your code and document what it does, not what you think it does. Give your teacher the same instructions since he should be capable of catching every problem in your code if he knows anything about C++. :rolleyes:

>void loadsave ();
It's best to avoid using one function for two wildly different operations.

>string convert;
I'll assume that string.h defines a string class on your old ass compiler. Otherwise this is a ghastly error.

>if (savedplayers >=1)
I'd bet my next paycheck that you want to enclose the body of this if statement in braces.

>convert += i;
>convert += ".dat";
You're going to get some wacky filenames if you don't clear the string after each iteration of the loop.

Jumping forward because I don't really want to discourage you by saying that every line is an error. ;)

>save.open("savedplayers.dat");
Do you really want to open the file for writing, or do you want to open it for appending? Opening for writing typically truncates the file to zero length while appending preserves existing contents.

>loadsave();
Repeat after me: Loop good, tail recursion bad.

I think I'll stop there. Go over your code and document what it does, not what you think it does. Give your teacher the same instructions since he should be capable of catching every problem in your code if he knows anything about C++. :rolleyes:

conserning the paycheck thing, no i don't want the brackets, with out them it only consernes the next line after the if statement. if there are no saved players, then don't display the "load player" option.

as for the void loadsave it was just an example, i have a big role playing game that i am trying to create, and instead of only letting the player to play the game and never be able to stop until they end the game, so it was kinda simulating that i will be calling to a function called loadsave from the main function of that game.

string.h, yes it is for a string function that i had tried for the conversersion part but forgot to take out after i discarded that method because it was even further from where i wanted to be

as for teh convert += i; convert += .dat, what do you mean, i want to say as long as i is less then the number of saved players, i am going to open that file and display it's name, each file is supposedto actually be named a number. so to load player 1 i want to open the file 1.dat read the contents and display the first word in the file. so the only way i could figure out how to do this is to first set convert equal to i (the file name) and add ".dat" on the end. if you can think of a better way to do this, please by all means share it, cus this was the only other way than using sprintf to do this.

save.open("savedplayers.dat"); yse i do want to open it for writing, because they chose to save a new player i want to add how many saved players there are, so i open savedplayers.dat (file containing only the number of saved files there are), add the number in it so as to keep an up to date account.

lol and as for the think it does and what it does i have worked out whith my teacher is that the load part itself is not working properly. when tested with the load.fail it says that it did in fact fail. and as for the teacher not being real good (lol) he did take programmin in college but mostly in java, and also isn't an actual c++ teacher, this class is actually an independent study, so i am actually learning it on my own and the teacher is just there to help me either understand or to just give me assignments. and i complete them on my own time.

(just to clear things up cus some people in the past when i reply think that i am ranting back at them, i am by no means trying to do that, just simply trying to convey my oppinions/fact back to you)

>no i don't want the brackets
Okay, I see what you're doing now. Good thing I didn't actually bet my paycheck. ;)

>as for the void loadsave it was just an example
k

>as for teh convert += i; convert += .dat, what do you mean
You keep adding to convert, so on the first loop it would be (for example) 0.dat, then on the second loop it would be 0.dat1.dat and filename would still be 0.dat.

I'm not in a position to spell out all that's wrong with your code right now, but at a glance it has problems of both design and implementation.

alright, i will try and fix that string problem, i did get it semi working but spitting out junk at me (such as 1/4pqr lol) thank you for all your help.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.