Why cant i load my stuff from a file 
  Oct 1, 2012 at 7:44am UTC  
 
Ok so i have this code and i cant load my stuff i outputed into my file for use in the other function,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 
#include <iostream> 
#include <string> 
#include <fstream> 
using  namespace  std;
struct  Vars
{
    void  Game();
    long  long  int  money;
    int  prisoners;
    string playerName;
    string prisonName;
};
int  main()
{
    int  choice;
    Vars v;
    cout << "1) New"  << endl;
    cout << "2) Load\n"  << endl;
    cin >> choice;
    if (choice == 1)
    {
        v.money = 50000;
        v.prisoners = 0;
        ofstream file;
        file.open("prison.txt" );
        cin.ignore(1000, '\n' );
        cout << "Hello please enter your name"  << endl;
        getline(cin, v.playerName);
        file << v.playerName << endl;
        cout << "\n" ;
        cout << "Thank you "  << v.playerName << " now please enter the name of your prison"  << endl;
        getline(cin, v.prisonName);
        file << v.prisonName << endl;
        cout << "\n" ;
        cout << "Ok thank you lets start the game"  << endl;
        cin.get();
        file << v.money << endl;
        file << v.prisoners << endl;
        file.close();
        v.Game();
    }
    else  if (choice == 2)
    {
        ifstream file;
        file.open("file.txt" );
        file >> v.playerName;
        file >> v.prisonName;
        file >> v.money;
        file >> v.prisoners;
        file.close();
        v.Game();
    }
}
void  Vars::Game()
{
    cout << playerName << endl;
    cout << prisonName << endl;
    cout << money << endl;
    cout << prisoners << endl;
}
 
 
Last edited on Oct 1, 2012 at 7:55am UTC  
 
 
 
 
  Oct 1, 2012 at 7:48am UTC  
 
Please post what file.txt contains!
 
 
 
 
  Oct 1, 2012 at 7:49am UTC  
 
Chay Hawk 
Prison 
50000 //Money 
0 //Number of prisoners
 
 
 
 
  Oct 1, 2012 at 9:06am UTC  
 
oh and just to clarify, it does it when i exit the program then start it back up and choose option 2. it just gives me weird numbers.
 
 
 
 
  Oct 1, 2012 at 9:57am UTC  
 
Look at the name of the file that you are saving to and the name of the file you are reading from. 
1 2 
        ofstream file;
        file.open("prison.txt" );
 
1 2 3 
        ifstream file;
        file.open("file.txt" );
 
... they need to be the same file...
change that and it runs nicely.
Just a suggestion, try making the part of the code that loads the file info into a function named "Load()" in your vars struct so that you can call it at any time you want. like I said, just a suggestion.
 
Last edited on Oct 1, 2012 at 10:05am UTC  
 
 
 
 
  Oct 1, 2012 at 4:32pm UTC  
 
Oh i cant believe i didnt see that -.-, i feel like an idiot :P. What do you mean make it into a load function? like this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 
struct  Vars
{
    void  Game();
    void  Load();
    long  long  int  money;
    int  prisoners;
    string playerName;
    string prisonName;
};
void  Load()
{
    ofstream file;
    file.open("prison.txt" );
       file.open("file.txt" );
        file >> v.playerName;
        file >> v.prisonName;
        file >> v.money;
        file >> v.prisoners;
        file.close(); 
}
 
like that?
 
 
 
 
Topic archived. No new replies allowed.