problem with my function

i have a problem in my code
http://codeviewer.org/view/code:440f
the exp is not adding to 20 when win
Your code:
1
2
3
4
5
6
7
8
9
int Menu::expAndLevelObtainWhenWin(int exp = 0,int lvl = 1) //// experience and lvl computer
{
    exp+=20;
    if( exp >=100 ) {
        lvl+=1;
        exp-=100;
    }
    return exp,lvl;
}
exp is a local variable which is destroyed when function finishes. What it actually does:
1) assigns passed value (in your case experience) to exp
2) Does some manipulations with exp
3) Discards exp.
So your manipulations are not visible outside.

Also return exp,lvl; line is equal to return lvl;. Do not use comma operator unless you know how it works.
been waiting, finally someone replied lol

exp is a local variable which is destroyed when function finishes.

i dont get it... look at my others functions theres a lot local variables but it all works well
It works well because you do not expect outside values to change after your function finishes.
FOr example your hp, enemy_hp and battle_win_lose members are never changed, it is just you never used them in context where it would be noticeable
sorry pings too high

bool Menu::battleCommand(char battle_command,int hp_battle ,int battle_enemy_hp,bool win_lose_func)
how about the bool win_lose_func which is a local variable too and it does changing and still works well...

can you fix this? so that i can understand it
1
2
3
4
5
6
7
8
9
int Menu::expAndLevelObtainWhenWin(int exp = 0,int lvl = 1) //// experience and lvl computer
{
    exp+=20;
    if( exp >=100 ) {
        lvl+=1;
        exp-=100;
    }
    return exp,lvl;
}
it does changing and still works well
It is always true. You never see lose message, do you?

To fix your function, just drop input parameters and manipulate members directly:
1
2
3
4
5
6
7
8
void Menu::expAndLevelObtainWhenWin()
{
    experience += 20;
    if( experience >= 100 ) {
        ++level;
        experience -= 100;
    }
}
Last edited on
It is always true. You never see lose message, do you?

i have tried to lose it but still same behavior

To fix your function, just drop input parameters and manipulate members directly:


i see thats how it is. its because in my code when i call the function and when the function is exiting the variables from that function will be destroyed even it is passed.
should i do the same in bool battle_win_lose?
Yes.
i was too greedy that i use bool in the wrong place. i just want to know how bool type works.


can you give me advice by using a returning function?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void Menu::battle()
{
  /*...*/
    battle_win_lose = battleCommand(choose, hp, enemy_hp);
    if(battle_win_lose == true) 
    //...
}


bool Menu::battleCommand(char battle_command, int hp_battle , int battle_enemy_hp)
//Parameters are not actually used, but I left them for now
{
  /*...*/
    //if(hp_battle <= 0) { win_lose_func=false; }
    //...
    return (hp_battle > 0)
}
   battle_win_lose = battleCommand(choose, hp, enemy_hp);
    if(battle_win_lose == true)

i never know this
okay thank you .. i understand it now a lot
Topic archived. No new replies allowed.