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.