Returning Values from function

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!

$TextAdventureCMD.cpp
#include <stdlib.h>
#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;


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

int rand2(int mindmgtaken ,int maxdmgtaken)
{
return mindmgtaken + (rand() % (int)(maxdmgtaken - mindmgtaken + 1));
}

void store (int gold, int playerhp, int mindmggiven, int maxdmgtaken)
{
int storechoice = 5;
system ("cls");
while (storechoice != 4)
{
cout << " *** S H O P ***" << endl;
cout << "You have " << gold << " Gold remaining - and " << playerhp << "hit points" << endl;
cout << "1 : Health Potion (10hp) : cost 10 Gold" <<endl;
cout << "2 : Sword Upgrade (+5 to minimum damage) : 50 Gold" << endl;
cout << "3 : Armor Upgrade (-5 to maximum damage taken) : 50 Gold" << endl;
cout << "4 : Exit Shop" << endl;

cin >> storechoice;


if (storechoice == 1)
{
if (gold < 10)
{
cout << "You do not have enough Gold to buy this." << endl;
system ("pause");
}
else
{
playerhp = playerhp + 10;
gold = gold - 10;
}
}
else if (storechoice == 2)
{
if (gold < 50)
{
cout << "You do not have enough Gold to buy this." << endl;
system ("pause");
}
else
{
mindmggiven = mindmggiven + 5;
gold = gold - 50;
}
}
else if (storechoice == 3)
{
if (gold < 50)
{
cout << "You do not have enough Gold to buy this." << endl;
system ("pause");
}
else
{
maxdmgtaken = maxdmgtaken - 5;
gold = gold - 50;
}
}
else if (storechoice == 4)
{
system ("cls");
break;
}
system ("cls");
}
}

void fight(int playerhp, int pchp, int maxdmgtaken, int maxdmggiven, int mindmgtaken, int mindmggiven, int gold)
{

system ("cls");
cout << " ***F I G H T !***" << endl;

int dmggiven = rand(mindmggiven, maxdmggiven);
if (dmggiven > pchp)
dmggiven = pchp;
int dmgtaken = rand2(mindmgtaken,maxdmgtaken);
if (dmgtaken > playerhp)
dmgtaken = playerhp;

cout << "you have delt " << dmggiven << " damage" << endl;
cout << "Computer has delt " << dmgtaken << " damage" << endl;
cout << "" << endl;


playerhp = playerhp - dmgtaken;
pchp = pchp - dmggiven;

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;

}
else if (choicea == 3)
{
chosevalues(hp, min, max, gold, playerhp, pchp);

}
else
choicea = 0;
}

int maxdmgtaken = max;
int maxdmggiven = max;
int mindmgtaken = min;
int mindmggiven = min;

system ("pause");
cout << "TEST before loop.... hp = " << hp << " and playerhp = " << playerhp << " pchp = " << pchp << endl;

while (playerhp > 0 && pchp > 0)
cout << "TEST inside loop.. hp = " << hp << " and playerhp = " << playerhp << " pchp = " << pchp << endl;
{
int choice = 0;

cout << "Press 1 to fight!" << endl;
cout << "Press 2 to enter the store" << endl;
cin >> choice;

if (choice == 1)
{
fight(playerhp, pchp, maxdmgtaken, maxdmggiven, mindmgtaken, mindmggiven, gold);
}
else if (choice == 2)
store (gold, playerhp, mindmggiven, maxdmgtaken);

}

system ("pause");
return 0;
}
on the set default values function, ive tried int with a return 0 as i did in above code, i would rather use a void like my others, but whatever works
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;
}
Last edited on
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,

Thank you again!!! day saver!
Topic archived. No new replies allowed.