I don't know what's wrong, but it will basically give random number. Don't know what is creating this. I've searched the whole document, nothing found.
int main()
{
int a, b, k;
ifstream infile;
infile.open ("pokeballs.dat");
infile >> a;
x = a;
infile.close();
//
infile.open ("pokemons.dat");
infile >> b;
y = b;
infile.close();
//
infile.open ("cash.dat");
infile >> k;
z = k;
infile.close();
cout << "\nIf you wish to close the game, just close it directly from the close button.\n\n";
game();
return 1;
}
// something like this
if(infile.is_open()){ // check to see if the file required is even open to get data from
infile >> x;
}else{
cout << "File couldn't be opened\n";
}
You should check the status of the file after each operation. For example:
1 2 3 4 5 6
infile.open ("pokeballs.dat");
if (!infile)
cout << "could not open pokeballs.dat" << endl;
infile >> a;
if (!infile)
cout << "failed to read a" << endl;
You could add similar checks for the output file too, and I highly recommend that the output file is not called "infile", it looks like an input and is confusing to read.
Ok, that may have been time consuming. But it has eliminated one possible cause. Now you need to check for other issues, such as uninitialised variables, or corruption of some area of memory.
If you know how to use a debugger, you could trace through the code and watch the values of important variables as it executes. (If not, maybe now is a good time to learn). Or you could add additional cout statements to keep track of values. Often it is better to open an output file to generate a log, rather than using cout which will clutter up the screen.
I'm really surprised from the fact that it saves correctly, so I don't think that there's any problem with the file. There might be problem with the loading system, so, can someone make me a loading system using integers so I try it out? Maybe my code is wrong.
Without getting into it (the code), I am guessing that you are apparently resetting your variables somewhere just from seeing it run. I press 1a few times and still only get 5 pokeballs, but when I catch a pokemon and check stats it is 0 pokeballs.
For example, if I press 1 10 times I would expect 50 pokeballs, but only have 5. Yet money is still out $2k for it.
Works for me.
Just to make sure, do this after each declaration of istream infile; (there is 2 of them): infile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
Strange about that. Also, did you put the cash yourself? I am still getting infinite things. I don't know what's wrong with this.. @MiiNiiPaa your code is not working. It says:
1 2
This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
I'm asking my self what the error might be. If you made any fix, can you please show some code as evidence so I find my way out of this?