Save and Load text file
Jun 16, 2014 at 1:30am UTC
Trying to write a way to save and load a game. and i have done this to test it.
now the problem i have is that when i enter A as lets say 4 and enter B as say a 2 it gets written in the text file as 42 and when i try to load A and B A comes out as 42 and B comes out as 0
what can i do to fix 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 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
#include <iostream>
#include <fstream>
using namespace std;
fstream Save;
int A, B;
int choice;
int main()
{
cout << "Would you like to save or load? 1 = save 2 = load" << endl;
cin >> choice;
if (choice == 1)
{
cout << "Enter a number: " ;
cin >> A;
cout << "Enter another number: " ;
cin >> B;
Save.open("Save.txt" );
Save << A;
Save << B;
Save.close();
}
else if (choice == 2)
{
cout << "your number was: " ;
Save.open("Save.txt" );
Save >> A;
Save >> B;
Save.close();
cout << A << endl << B;
}
cout << endl;
system("pause" );
return 0;
}
Jun 16, 2014 at 1:35am UTC
oh never mind. haha.
just by random chance i tried using
Save << A << endl;
and that was all i needed!
Jun 16, 2014 at 7:47am UTC
You can also use
Save << A << " " ;
Topic archived. No new replies allowed.