void lose()
{
cout<<"Game over! You lose";
cout<<endl;
if(exp>=50)
{
cout<<"You exp has been down into "<<exp-50;
}
else
{
cout<<"You exp has been down into "<<50-exp;
}
}
int experience(int exp=0)
{
if(win)
{
exp=exp+20;
if(exp == 100)
{
lvl=lvl+1;
if(true)
{
exp=0;
}
}
}
}
in the first function the exp was na declared in the scope but i dont know how to fix it haha. should i global the variable exp ? instead of putting it in experience function?
void lose()
{
int exp_down;
cout<<"Game over! You lose";
cout<<endl;
if(exp_down>=50)
{
cout<<"You exp has been down into "<<exp_down-50;
}
else
{
cout<<"You exp has been down into "<<50-exp_down;
}
experience(exp_down);
}
int experience(int exp)
{
int exp_=0;
if(win)
{
exp=exp+20;
if(exp == 100)
{
lvl=lvl+1;
if(true)
{
exp=0;
}
}
}
}
@AbstractionAnon
yeah i forgot to initialize exp_down
line 16 is a typo, ignore exp_
lvl19: win is defined this is just some part of my code
same with line 24
thank you im going to fix 16-31 myself.
i already read it like a million times i still dont understand haha. a little bit
but this is my first time to code.
I think the way to approach that page is to focus closely on the sample code. Copy it, try to compile and run it. When you see what it actually does, go back and re-read the explanation.
In addition, you could take the sample code and play around with it, if you think you understand it, change some part and see whether or not it behaves as expected. Gradually, bit by bit you build up knowledge by a combination of both reading and doing.
yeah i can see what you are saying. but its the one and only one i dont know why i cant understand it.. but thanks for your advice gonna credit you when i already did it :) thank you
what if i call the function experience in main function every time i win? would it still be lost?
Yes. It doesn't matter where you call experience() from. experience() never communicates the new value of exp back to the caller.
There are two ways to do this.
1) Make experience() and int function and have it return the new value of exp. The caller is then responsible for updating his copy of exp with the returned value.
2) Pass exp by reference to experience(). This allows experience() to update the caller's variable.
1) Make experience() and int function and have it return the new value of exp. The caller is then responsible for updating his copy of exp with the returned value
o yah yah.. i forgot that i used int in function which the main expecting to return a value
2) Pass exp by reference to experience(). This allows experience() to update the caller's variable.
i understand.
another question would the void lose function still work properly? look, i call the experience function inside the void which will not return value or should i really have to pass it by reference ?
If you take approach #1, every function called needs to return exp back to the point where the original definition of exp is (presumably main).
Return by value is fine where a function returns a simple value such as rand() or sqrt(), etc. Return by value gets ugly when a called function wants to update more than one value in the caller. This is where pass by reference (#2) is preferred. If you need to update more than one value in the caller, you can simply add another pass by reference argument to your function.