I am trying to get my program to display multiple files for a user to choose from to load, this is the general code i tried but don't know how to actually put it as (the for loop is what i am having the trouble with)

ofstream save;
ifstream load;
int choice;
int savedplayers;
char *savedgame = new char [15];
load.open("savedplayers");
load >> savedplayers;
load.close();
cout << "1: Load game" << endl;
cout << "2: Save game" << endl;
cin >> choice;
    if (choice == 1){
        for (int i = 1; i <= savedplayers; i++){
            load.open(i);
            load >> savedgame;
            load.close();
            cout << i << ": " << savedgame;
            cin >> choice;
            cout << savedgame;
        }
    }
    else if (choice == 2){
        cout << "Save game as:";
        cin >> savedgame;
        save.open("savedplayers");
        savedplayers++;
        save >> savedplayers;
        save.close();
        save.open(savedplayers);
        save >> savedgame;
        save.close();
    }

}

>load.open(i);
Open requires a C-style string, so you'll need to do a conversion at some point:

char filename[20];

sprintf(filename, "%d", i);

load.open(filename);

To use sprintf you need to include <cstdio>, though the C-style solution isn't necessarily the best. Another alternative uses C++ strings and stringstreams:

ostringstream oss;

oss<< i;

load.open(oss.str().c_str());

What exactly do you want the code to do? As is, it's hard to be sure just by reading it. Maybe some comments are in order. :)

ok this is for a role playing game that i am making, i have a save and load player in it but i want to be able to save and load multiple people's files (eg. someone is playing and saves their game and a second player comes in and loads their game, thus they just select which game is theirs and play from there.) the file will contain things such as name, stats, and position in the game.

explaining the code i posted:

1) recall a file that says how many files are actually saved (savedplayers).
2) ask if they want to save or load.
3) if save then add 1 to savedplayers
4) if load then display all saved files (to be simple they are actually saved as numbers and just display what the player specified name which is stored in it, thus the for loop comes into play)
5) get user input as to what file to load.
6) put set the stats back to what they should be, and goto where they are in the game.

(steps 5 and 6 are not actually posted but will be put into play when i work this kink out first.)

ok so i tried modefieng it like so, but it doesn't want to load the savedgame variable from the file that i open,so it ends up just displaying the "1: " part and not the name part, can anyone think of a reason or solution to this?

#pragma hdrstop
#pragma argsused
#include <condefs.h>
#include <iostream.h>
#include <conio.h>
#include <stdlib.h>
#include <mmsystem.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 [15];
char filename[20];
cout << "1: Load game" << endl;
cout << "2: Save game" << endl;
cin >> choice;
    if (choice == 1){
       load.open("savedplayers.dat");
       load >> savedplayers;
       load.close();
        for (int i = 1; i <= savedplayers; i++){
            sprintf(filename, "%d", i);
            load.open(filename);
            load >> savedgame;
            load.close();
            cout << i << ": " << savedgame << endl;

        }
        cin >> choice;
        sprintf(filename, "%d", choice);
        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");
        sprintf(filename, "%d", savedplayers);
        save << savedplayers;
        save.close();
        save.open(filename);
        save << savedgame;
        save.close();
        loadsave();
    }

}

pry gonna sound really noobish but, does it need to? i didn't save it as any (in my expierience it seemed to work whenever i tried the savedplayers with or without the extension .dat so i didn't think ir really mattered)

>pry gonna sound really noobish but, does it need to?
It depends on how your file is saved, you need to match that name exactly or it will fail to open, and Windows is weird about file names. Turn off extension hiding and see what happens.

it just says that it is a file (not a .rtf, .dat or anything just file (saved as a plain text document)) even in the command prompt it recognizes that the files themselves (the 1, 2, 3 so one) have no file extensions, but the savedplayers is a .dat file. (again i know the savedplayers is a .dat file but still opens, loads ect... but when i give the path to it i am able to have the .dat part or not and it still works for me so i am not sure i truly need the file extension though) but yeah...

This article has been dead for over six months. Start a new discussion instead.