Load from file will give random INT number

Pages: 123
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.

This is the loading code:

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
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;
}


And this is the game(); code:

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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
int game()
{
    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();
    
    c = rand()%6;
    cout << "\nHello. Welcome to the most simple pokemon game.\n";
    cout << "\nYou got " << x << " pokeballs, " << y << " pokemons and " << z << "$ in your bag.\n";
    cout << "\nWhat do you want to do? Catch pokemon, buy pokeballs, sell pokemons or check your stats?\n\n"; 
    cout << "\nPress 1 (and than ENTER) for catching a pokemon. \nPress 2 (and than ENTER) to buy pokeballs. \nPress 3 (and than ENTER) to sell pokemons. \nPress 4 (and than ENTER) to check your stats.";
    cout << "\nYour option: ";
    cin >> n;
    if(n == 1)
    {
         if(x > 0)
         {
              if(c == 0)
              {
                    cout << "______";
                    cout << "\nFailure! You didn't caugh any pokemon and lost a pokeball instead!\n\n";
                    x = x-1;
                    again();
              }
              else if(c ==1)
              {   
                    cout << "______";
                    cout << "\nSuceed! You caught a pokemon!\n\n";
                    y = y+1;
                    x = x-1;
                    again();
              }
              else if(c == 2)
              {
                    cout << "______";
                    cout << "\nFailure! You didn't caugh any pokemon and lost a pokeball instead!\n\n";
                    x = x-1;
                    again();
              }
              else if(c == 3)
              {
                   cout << "______";
                   cout << "\nFailure! You didn't caugh any pokemon and lost a pokeball instead!\n\n";
                   x = x-1;
                   again();
              }
              else if(c == 4)
              {
                   cout << "______";
                   cout << "\nFailure! You didn't caugh any pokemon and lost a pokeball instead!\n\n";
                   x = x-1;
                   again();

              }
              else if(c == 5)
              {
                   cout << "______";
                   cout << "\nSuceed! You caught a pokemon!\n\n";
                   y = y+1;
                   x = x-1;
                   again();

              }
         }              
         else 
         {
              again();
              cout << "______";
              cout << "\nYou don't have any pokeball.\n\n"; 
         }
    }
    else if(n == 2)
    {
         if(x < 6)
         {
              if(z > 199)
              {
                   cout << "______";
                   cout << "You were given 5 pokeballs and got 200$ removed. You got " << z << "$ now.";
                   x = 5;
                   z = z-200;
                   again();
              }
              else 
              { 
                   cout << "______";
                   cout << "\nYou need at least 200$ to buy pokeballs!\n\n"; 
              }
         }
         else 
         { 
              cout << "______";
              cout << "\nYou got 5 pokeballs already\n\n";
              again(); 
         }
    }
    else if(n == 3)
    {
         if(y > 0)
         {
              cout << "______";
              cout << "\nYou gave " << y << " pokemons and got " << y*200 << "$ in return\n\n";
              z = y*200;
              y = 0;
              again();
         }
         else 
         { 
              cout << "______";
              cout << "\nYou don't have any pokemon in your bag.\n\n";
              again(); 
         }
    }
    else if(n == 4)
    {
         cout << "______";
         cout << "\nYou got " << x << " pokeballs, " << y << " pokemons and " << z << "$ in your bag.\n\n";
         again();
    }
    else if(n > 4 || n < 0)
    {
         again();
    }
    return 0;
}


While, this is the saving code on top of "other()"

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
    ofstream infile;
    
    // pokeballs
    infile.open ("pokeballs.dat");
    infile << x;
    infile.close();
    
    // pokemons
    infile.open ("pokemons.dat");
    infile << y;
    infile.close();
    
    // cash
    infile.open("cash.dat");
    infile << z;
    infile.close();
    
Do checks on if files are opened correctly using .is_open() member functions.
main() loads the values into a,b,k and assigns them to x,y,z. Then game() does the very same thing, are you sure you want to load them twice?

I presume that x,y,z are globals because they aren't declared in your functions.

what is the contents of your 3 .dat files before the program runs? something needs to be there to initialise the game when it first runs.



Yes, x,y,z are global. The three .dat files contain 0, I don't know why it will show that random number.

And yes, I'm sure that I want to load them twice, because I was trying where was the INT changing but it still keeps doing so (random number).

@MiiNiPaa what do you mean by that? Can you please direct me on what to check?

Thanks!
1
2
3
4
5
infile.open("pokeballs.dat");
if(!infile.is_open()) {
    std::cout << "error opening file pokeballs.dat";
    exit(-1);
}
He means:
1
2
3
4
5
6
// 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";
}
Last edited on
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.
No error showing up at all. Its all fine.

[IMG]http://i.imgur.com/or1VJFQ.png[/IMG]

This is how it shows up when the dat file contain 0 instead.
Did you check all three files?

As Chervil pointed out check results of each input operation too.
Did you add checks for all of the file accesses (open, read, close x 3) * 2 , that's eighteen checks when getting the input.
Last edited on
There's no error, tried them on all.
What do the files look like then? If the problem isn't the code, according to you, then it could be a format issue in the files.
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.
Last edited on
Empty file which contains the numbers.

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.
Post your code and files so we can see it. This method will take forever to help you fix if we aren't sure what is wrong with your code to begin with.
http://pastebin.com/9g45T4Dm

That's the code.

All of the files have 0, except pokeballs which has 5 in it.
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.
Last edited on
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);
I think your logic may be off on a few things after running it a few times.
Last edited on
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?
Pages: 123