Load from file will give random INT number

Pages: 123
Oh, so you do have file reading problem. It is not "not working", it is working as intended and shows that there is file operation failure which you have missed completely.

Make your main function like:
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
    int main()
    {
    try {
        ifstream infile;
        infile.exceptions(std::ifstream::failbit | std::ifstream::badbit);
        infile.open ("pokeballs.dat");
        infile >> x;
        infile.close();

        //
        infile.open ("pokemons.dat");
        infile >> y;
        infile.close();
        //
        infile.open ("cash.dat");
        infile >> z;
        infile.close();

        cout << "\nIf you wish to close the game, just close it directly from the close button.\n\n";
        game();
        return 1;
    }  catch (std::ios_base::failure &fail) {
        std::cout << fail.what();
        char c;
        std::cin >> c;
    }
    }



Alos your buy routine
1
2
3
cout << "You were given 5 pokeballs and got 200$ removed. You got " << z << "$ now.";
x = 5;
z = z-200;
should look like:
1
2
3
x += 5;
z = z-200;
cout << "You were given 5 pokeballs and got 200$ removed. You got " << z << "$ now.";
(As usual, you should change it in at least 2 places)

Last edited on
The more I run it the more baffled I get.
I tweaked the text files. Gave myself 500 balls, and then used 100. I had sold a few pokemon I had to get the amount of 400200$ sold 27 pokemon to get 5400$ for them. Checked my stats and I went from 400200$ to 400 balls, 0 pokemon, 5400$ in my bag. Looking through the code now.
I tried this in two different compilers, one accessed the files ok. The other didn't like the reuse of the same ifstream object for multiple files.

But of course I already explained how to check the file status and it was (claimed to be) all ok and so that can't be the issue here.
Last edited on
Chervil, your code (main) will return this instead

basic_ios::clear

And after pressing anything, it will close down.
Last edited on
Chervil, your code (main) will return this instead

I'm a bit lost. Could you please explain (post here or provide a link) exactly which code is referred to here?

I think possibly I may have referred to a feature which may not be available in some older compilers.

I don't remember whether it was already stated; which compiler and version number are you using please.
Last edited on
Sorry, I messed the names up, I mean MiiNiiPaa. Your main is returning me

basic_ios::clear
What exactly does it write? What compiler do you use?

Well, you did catch an exception. That means it got thrown. That means that there is problem with handling openng file and you did not do previous checks properly.

It can be that as Chervil said that your compiler does not like multiple opening of different files. Try to create own instance of ifstream for each file.
Last edited on
I removed the second loading. I'm using Dev-Cpp. Now, after removing the second load, it opens and wont create a random INT, but this time it is not going to load the files at all.

EDIT:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    int a, b, c;
    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 >> c;
    z = c;
    infile.close();


I edited back to the old one, and I'm getting random INT again, while with the code that MiiNiPaa gave, it is not going to load at all.
Last edited on
Which version of DevC++ ? It should be the Orwell version now at version 5.6.3
http://orwelldevcpp.blogspot.co.uk/
(version 4.9 was last updated in 2005 and is obsolete)

My suggestion for load and save:
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
const char fname1[] = "pokeballs.dat";
const char fname2[] = "pokemons.dat";
const char fname3[] = "cash.dat";

bool load(int & a, int & b, int & c)
{
    ifstream f1(fname1);
    f1 >> a;
    ifstream f2(fname2);
    f2 >> b;
    ifstream f3(fname3);
    f3 >> c;
    return (f1 && f2 && f3);
}

bool save(int a, int b, int c)
{
    ofstream f1(fname1);
    f1 << a;
    ofstream f2(fname2);
    f2 << b;
    ofstream f3(fname3);
    f3 << c;
    return (f1 && f2 && f3);
}

Then load in main() and/or game() like this:
1
2
3
4
5
    bool ok = load(x, y, z);
    if (!ok)
    {
        cout << "error loading from files 1" << endl;
    }
I'm using Dev-Cpp
I hope it is orwell, not bloodshed

while with the code that MiiNiPaa gave, it is not going to load at all.
Of course. Because there is an error here. Which my code unearth and forces program termination.

Did you try that:
MiiNiPaa wrote:
It can be that as Chervil said that your compiler does not like multiple opening of different files. Try to create own instance of ifstream for each file.


Now try that

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
{
    ifstream infile("pokeballs.dat");
    infile >> x;
    if(!infile)
        std::cout << "Error reading file pokeballs.dat";
}
{
    ifstream infile("pokemons.dat");
    infile >> y;
    if(!infile)
        std::cout << "Error reading file pokemons.dat";
}
{
    ifstream infile("cash.dat");
    infile >> z;
    if(!infile)
        std::cout << "Error reading file cash.dat";
}
        cout << "\nIf you wish to close the game, just close it directly from the close button.\n\n";
        game();

DO the same for opening files in game() function Or, berrter use Chervil approach.
Last edited on
Thank you all for your solutions guys! Chervil, I'm now using yours for an easier way!

In the meanwhile, your answer works also MiiNiPaa!

I'm so excited that it finally started working!

Also, I'm using bloodshed 4.9.9.2.
I'm using bloodshed 4.9.9.2.
Upgrade to Orwell one ASAP. Bloodshed version is outdated, buggy (one of the bugs you encountered today) and does not support C++11
I got another question.

I want to run again() from a function which is before it. How can I?

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
int shop()
{
    int r;
    string yn = "";
    cout << "\nHow many pokeballs you want to buy?: ";
    cin >> r;
    cout << "\n\nAre you sure you want to buy " << r << " pokeballs for " << r*50 << "$? (y-n): "
    cin >> yn;
    if(yn == "y" || yn == "Y")
    {
          if(z >= r*50)
          {
               cout << "\nYou have bought "<<r<<" pokeballs for " << r*50 << "$\n\n";
               x = r;
               z = z-(r*50);
               again();
          }
          else
          {
              cout << "\nNot enough money! Choose another quantity";
              shop();
          }
    }
    else if(yn == "n" || yn == "N")
    {
         shop();
         }
         return 0;
}

int again()
{


I can't move from shop to the other function since I'll get errors.

56 C:\Dev-Cpp\Applications - Console\Numbers\main.cpp `again' undeclared (first use this function)

Thanks again and cheers!
You should not call function recursively (like in again() or here: indirect recursion) because it is going to crash your program after some time.

It looks like you just should let it return to continue execution from where it was interrupted.
How can I do that? I want to execute the shop() too in the meanwhile.
I want to execute the shop() too in the meanwhile.
Execute it. Let it return. Continue from where you been.
1
2
3
4
5
6
7
void game()
{
  //some game stuff
    if(selection == SHOP)
        shop(); //Will call shop()
  //Execution will be resumed here after shop() finishes
}
Here is the code I came up with earlier. Basically you could change the case that runs the buy pokeballs to add the shop function to turn it into a shop.

http://codeviewer.org/view/code:40c5
@MiiNiPaa I get errors by trying to do that.

143 C:\Dev-Cpp\Applications - Console\Numbers\main.cpp `selection' undeclared (first use this function)
143 C:\Dev-Cpp\Applications - Console\Numbers\main.cpp `SHOP' undeclared (first use this function)
177 C:\Dev-Cpp\Applications - Console\Numbers\main.cpp return-statement with a value, in function returning 'void'
143 error 'selection' undeclared means you have no selection variable defined.
143 error 'SHOP' undeclared again means you have no SHOP variable or such defined.
177 error means you are using a return statement in a void function.

Look at my code, I updated it to have an empty shop function and put comments in my switch statement where you would place it instead of purchasing 5 pokeballs.

My code is ugly, but I was trying to stay close to your code. I was attempting to simplify it by removing the extra calls and things you simply didn't need.

http://codeviewer.org/view/code:40c9

line 52: case 2
Last edited on
Bro, thanks but I don't want to get pre-made as I wont learn anything off it. It is better when I learn the things step by step. Also, I think you haven't got my point.

I want that from the shop() function, go to the again();, which is after the shop().

There, like MiiNiPaa code, but just that his code will give me errors..

Execute it. Let it return. Continue from where you been.


Pages: 123