if statement always evaluated to true

Hi, I was doing some work on my game, and rewriting it to avoid repetition of code, and have come across a problem I don't understand. On the main code, I have a function load where the user can load a game, within that function, I am reading from a file to determine how much the main character should level up. Anyway, it all works fine except for 1 thing. In the function for levelling up, I have an if statement to determine where or not to display a message, but it always appears to evaluate to true. What am I missing? The 2 relevant functions are below (all variables are declared, just not necessarily shown below):

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
void load(Hero& MainCharacter, Item Items[], Weapon weapons[])
{
    using namespace std;
    std::ifstream loadfile("gamesave.dat");
    if (loadfile.is_open())
    {
        std::string charname;
        getline(loadfile, charname);
        MainCharacter.setHeroName(charname);
        MainCharacter.SetStats();
        int Exp;
        loadfile >> Exp;
        MainCharacter.AddExperience(Exp);
        while (MainCharacter.getExperience()>=MainCharacter.getExpNextLevel())
        {
            MainCharacter.Levelup(true);
        }
        int charhp;
        loadfile >> charhp;
        MainCharacter.Heal(charhp-20);

        int Gold;
        loadfile >> Gold;
        MainCharacter.AddGold(Gold-50);

        int weaponID;
        loadfile >> weaponID;
        for (int i=0; i<WEAPONSLIST; i++)
        {
            weapons[i].setEquipped(false);
        }
        weapons[weaponID].setEquipped(true);
        int itemquantity;
        for (int i=0; i<ITEMSNUMBER; i++)
        {
            loadfile >> itemquantity;
            Items[i].SetItemQuantity(itemquantity);
        }

        int weaponquantity;
        for (int j=0; j<WEAPONSLIST; j++)
        {
            loadfile >> weaponquantity;
            weapons[j].setQuantity(weaponquantity);
        }
        cout << "Game loaded successfully";
    }
    else
    {
        cout << "No save file detected";
    }
}
void Levelup(bool gameload=false) //this function is in the Hero class
{
    using namespace std;
    Level++;
    MaxHP+=10;
    Strength+=2;
    Defense+=1;
    ExpNextLevel+=Level*Level*5;
    if (!gameload)
    {
        cout << "You levelled up to level " << Level << endl;
    }
}
Last edited on
first off line 55 should not be there, it should be at the top of your file by the includes above all other code. Also what is the point of the gameload parameter? I cannot see why it would always evaluate to true. Also, we cannot run the code as you have only posted a section of it, the best thing to do is make a suitable example that shows your problem or (please try to avoid this on these types of errors) post the full program source.

EDIT: Read next post
Last edited on
Script Coder wrote:
first off line 55 should not be there, it should be at the top of your file by the includes above all other code.

It is possible to put the using statement inside functions. It limits the "using" to that function.

Last edited on
It is possible to put the using statement inside functions. It limits the "using" to that function.


Wow, I did not know that. (You learn something new everyday) Thank you, but can you see any reason why the if would always evaluate to true, I can't.
I've uploaded a zipped version of the project. You can download it at http://www.mediafire.com/?kp243m0hwviiqzw. The purpose of the gameload parameter is I do not want the message to display if the player is just loading the game (i.e. when gameload is true), but if they were to level up while playing, the message should display.
You could just store their level and load it in. In which case all you store then is thier level and their current exp on that level.
I'm guessing the print in that if-statement always displays because of the default value you are giving it in the parameter list of void Levelup(bool gameload=false)

Unless you explicitly change gameload somewhere inside the function or alternatively have a value of TRUE for the function's argument when calling this function, it will always be FALSE inside the function, therefore
!gameload will evaluate to TRUE, satisfying the if-statement and thus printing that out everytime.

Hope that helps.
Last edited on
@ENIGMAx

please may I refer your attention to:
Line 16 wrote:
MainCharacter.Levelup(true);


@OP I am assuming that the LevelUp function you referred us to is inside your MainCharacter class. If not and you have a different one, please check that code.
Last edited on
@Script Coder

Yes, the Levelup function is in my Hero Class (MainCharacter being an instance of that class)
Last edited on
^^ Thanks Script Coder, much appreciated. Sorry manudude03 for the misguidance.
Last edited on
After about 4 hours of searching, I finally found what the problem was. I forgot the AddExperience function had the while loop for levelling up in it, so that would get called (and displaying the message), and when it comes to the condition for the while loop, it would always be false so the stuff inside it was never getting called. I added a second parameter to the AddExperience to deal with the Levelup boolean, and now it works as intended. Thanks for the help everyone.
Topic archived. No new replies allowed.