Hey guys, I'm brand new to this form, and I really need some backup. I've searched around and haven't found anyone with this issue yet.
I am sending more than one int variable from my main, into a function. I change I change the values of the variables in that function, but when we get out of that function the variables go back to 0 or original value.. here is my code for my whole program so far... I have a lot of extra couts in there right now, I was trying to find where the issue was, i will clean it up once its working.
Thanks for your help! and I'm looking forward to being in the cplusplus forums!
int setdefaultvalues(int hp, int min, int max, int gold, int playerhp, int pchp)
{
hp = 100;
min = 5;
max = 10;
gold = 60;
cout << "Starting HP for you and PC = 100" << endl;
cout << "Starting minimum damage = 5" << endl;
cout << "Starting maximum damage = 10" << endl;
cout << "Starting gold = 60" << endl;
playerhp = hp;
pchp = hp;
cout << "player hp = " << playerhp << endl;
cout << "pc hp = " << pchp << endl;
cout << "TEST inside setdefaultvalues function .... hp = " << hp << " and playerhp = " << playerhp << " pchp = " << pchp << endl;
return 0;
}
void chosevalues (int hp, int min, int max, int gold, int playerhp, int pchp)
{
cout << "You are fighting the computer!" << endl;
cout << "Chose the hit points you and the computer will have ";
cin >> hp;
cout << "what would you like the minimum damge to be? ";
cin >> min;
cout << "what would you like the maximum damage to be? ";
cin >> max;
cout << "starting gold? ";
cin >> gold;
system("cls");
playerhp = hp;
pchp = hp;
cout << "player hp = " << playerhp << endl;
cout << "pc hp = " << pchp << endl;
}
void win(int playerhp, int pchp) // Winning function, displays final HP afer clearing screen
{
if (playerhp < 1) // if player hp was allso 0 at time of win, this will ad 1 to it to make sure there is no tie
playerhp = playerhp + 1;
system ("cls");
cout << " YOU WIN!! " << endl;
cout << "Your hp = " << playerhp << endl;
cout << "Computer hp = " << pchp << endl;
}
void loss(int playerhp, int pchp) // function for loss, clear screen, display game over and hp
{
if (pchp < 1)
pchp = pchp + 1;
system ("cls");
cout << " G A M E - O V E R " << endl;
cout << "Your hp = " << playerhp;
cout << "Computer hp = " << pchp;
}
int rand(int mindmggiven, int maxdmggiven)
{
return mindmggiven + (rand() % (int)(maxdmggiven - mindmggiven + 1));
}
cout << "" << endl;
cout << " ***Results***" << endl;
cout << "You have " << playerhp << " hit points left" << endl;
cout << "The computer has " << pchp << " hit points left" << endl;
cout << "" << endl;
if (pchp <= 0)
{
win(playerhp, pchp);
}
else if (playerhp <= 0)
{
loss(playerhp, pchp);
}
}
int main()
{
srand((unsigned)time(0)); //sets start point of random generators to system time (never the same) This makes the progarm start its random numbers different each time
int max = 0;
int min = 0;
int hp = 0;
int gold = 0;
int playerhp = 0;
int pchp = 0;
int choicea = 0;
while (choicea == 0)
{
system ("cls");
cout << "Welcome to my txt based game!" << endl;
cout << "1 : Instructions" << endl;
cout << "2 : Start w/ default values" << endl;
cout << "3 : Chose all starting values" << endl;
cin >> choicea;
if (choicea == 1)
{
system ("cls");
cout << "instructions!" <<endl;
//instructions();
system ("pause");
choicea = 0;
}
else if (choicea == 2)
{
setdefaultvalues(hp, min, max, gold, playerhp, pchp);
cout << "TEST RIGHT after setdefaultvalues function.... hp = " << hp << " and playerhp = " << playerhp << " pchp = " << pchp << endl;
There's 3 ways to pass datatypes into functions.
1-by value
2-by reference
3-pointers
You're doing it by value since anything the function does to the variable is not returned.
You want either references or pointers. References are easier to explain and often all that's needed. It is simply a matter of changing the prototype and definition headers to, for example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//Prototype:
//Notice the reference operator '&' and the syntax.
//That's the only thing u need to change
int MyFunction(int& ivalue);
int main()
{
int myval = 2;
MyFunction(myval); //calling the function doesnt change at all
//myval now == 7
return 0;
}
//Definition:
int MyFunction(int& ivalue)
{
ivalue += 5; //ivalue will increase by 5 here and in the outer scope after returning
return 0;
}
OH!!! thank you soo much :) and more importantly I understand! haha. int& means it will change the int in and OUTSIDE of that function... brilliant!
my friend and I have been self teaching ourselfs c++ for about 3 weeks now. This is really the first program I started from scratch on, we are going to keep adding and adding to it,