Jan 18, 2015 at 3:11am UTC
Save is an fstream. SAVE is a double float.
Jan 18, 2015 at 3:25am UTC
Are you certain the file is open? Line 2 will evaluate to false if SaveFile.mystery does not exist prior to you trying to create it with the open call. Flushing cout before calling getch might help you see that, although one wonders why you allow the program to continue past line 5 if the file is not opened.
Jan 18, 2015 at 3:31am UTC
Oh, I forgot to mention that the if evaluation was added as a test to see whether the file is open or not, it will not be in the final code.
Jan 18, 2015 at 3:34am UTC
Save.open needs its flags when using fstream. Add std::ios::out as a parameter to open.
Jan 18, 2015 at 3:43am UTC
Adding std::ios::out as a parameter did nothing...
All code for program (windows)
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 84 85 86 87
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <cstdio>
#include <conio.h>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int Clock = 0;
int ClockMax = 0;
int x = 0;
int y = 0;
int Action = 0;
int While = 0;
int key = 0;
int Ans = 0;
string line = "0" ;
double SAVE[27];
fstream Save("SaveFile.mystery" );
while (getline(Save, line))
{
SAVE[x] = atof(line.c_str());
x = x +1;
}
Save.close();
if (SAVE[26] != 1)
{
x = 0;
while (x != 26)
{
SAVE[x] = 0;
x = x +1;
}
SAVE[0] = SAVE[4] = SAVE[6] = SAVE[8] = SAVE[15] = 100;
SAVE[26] = 1;
}
While = 0;
x = 1;
//load
L00:
Save.open("SaveFile.mystery" , std::fstream::out);
if (!Save.is_open())
{
cout<< "Not open." ;
getch();
}
Ans = 0;
while (Ans != 27)
{
Save<< SAVE[Ans] << "\n" ;
Save<< "TestLine" ;
Ans = Ans +1;
}
Save.close();
system("cls" );
key = 0;
if (x == 1)
cout<< "\x10" ;
cout<< "Store\n" ;
if (x == 2)
cout<< "\x10" ;
cout<< "Common house\n" ;
key = getch();
if (key == 72)
x = x -1;
if (key == 80)
x = x +1;
if (x < 1)
x = 2;
if (x > 2)
x = 1;
if (key == 92)
{
system("cls" );
getch();
}
if (key == 122)
goto L01;
goto L00;
L01:
system("cls" );
}
Last edited on Jan 18, 2015 at 6:08pm UTC
Jan 24, 2015 at 5:08am UTC
I just found the issue, I have to use both ifstream and ofstream variables and make sure not to open a file twice.