Jump to content
  • Advertisement
Sign in to follow this  
RebornCrosis

Please help me I think I did this wrong

This topic is 4814 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#include <iostream>
using namespace std;

int HpUpgrade (  int Hp );

int main ()
{
	int Hp; // This is the player Hit points.
	int Gold; // This is the player gold.

	Hp = 40;
	Gold = 900;

	cout << "Hello and welcome to the Hp shop.\n";
	cout << " Do you wish to to upgrade your hp?\n";
	int choice;
	cout << " 1 - Yes.\n";
	cout << " 2 - No.\n";
	cin >> choice;

	if ( choice == 1 && Gold >= 500)
	{
		cout << " Aye thank you for the Busines.\n";
		Gold -= 500;
		cout << " You Gold After Paying the man is " << Gold << endl;
	        int HpUpgrade ( Hp );
		cout << " After the upgrade you Hp is " << Hp << endl;
	}
	return 0;
}


// function def.
 int HpUpgrade ( int Hp )
{
	
	Hp += 1;

	return Hp;
}

I was trying to see if I could get the Function to add the +1 to the it complies and everthing and runs but it does not add the Hp to the int Hp. Please Help.

Share this post


Link to post
Share on other sites
Advertisement
When you pass a parameter to a function that you want to modify, you must pass by reference rather than pass by value.

Anyways, all you need to do is to use an & attached as a prefix to the variable name in the function and it will pass by reference.

int HpUpgrade ( int &Hp );
int HpUpgrade ( int &Hp )
{
...
}

Share this post


Link to post
Share on other sites

#include <iostream>
using namespace std;

int HpUpgrade ( int &Hp );

int main ()
{
int Hp; // This is the player Hit points.
int Gold; // This is the player gold.

Hp = 40;
Gold = 900;

cout << "Hello and welcome to the Hp shop.\n";
cout << " Do you wish to to upgrade your hp?\n";
int choice;
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
cin >> choice;

if ( choice == 1 && Gold >= 500)
{
cout << " Aye thank you for the Busines.\n";
Gold -= 500;
cout << " You Gold After Paying the man is " << Gold << endl;
int HpUpgrade ( Hp );
cout << " After the upgrade you Hp is " << Hp << endl;
}
return 0;
}


// function def.
int HpUpgrade ( int &Hp )
{

Hp += 1;

return Hp;
}


So should it look like that?



Share this post


Link to post
Share on other sites
Yes, except that you don't specify the return type of a function when calling it (the line int HpUpgrade ( Hp ); should be HpUpgrade ( Hp );).

Share this post


Link to post
Share on other sites
The way you do it, it'd work too, but you should call it like this:
Hp=HpUpgrade(Hp);


This, or just make the function like Drew_Benton mentioned:

void HpUpgrade(int& Hp)
{
Hp+=1;
}


Hope this helps.

Edit: Seems I was a bit too slow :)

Share this post


Link to post
Share on other sites
Whoops, I see that I kinda misread everything and did it wrongly [lol]. Oh well, Roboguy and Wuntvor are right. Maybe I should get some sleep now...

Share this post


Link to post
Share on other sites
Ok thank you guys for the input I got it to work finally. Yayyay I understand it alot better now, thank you all again for the input. lol it is my fault I was so happy and that I got in to making functions I was only about 17 pages away from " References and Reference Parameters". So I am going to finish reading this ch before I do anything else.

Share this post


Link to post
Share on other sites
Returning the new value as a parameter works as well. But in general, you should either return something *useful*, or nothing at all (specify 'void' in the return-type position). Anyway, the main problem here was the actual function call - it should only specify the function name along with the parameters to use for that particular invocation (and not their types).

Now let's cover some basic style issues :)

1) Initializing your variables is a good idea. Basically, at the point where you declare a variable, it might have any value (out of those possible for the type) until you first assign something to it - so using it before then can cause nasty bugs. The safest thing is to do that assignment immediately - you can do slightly better than what is in the code now, by using the initialization syntax:


// before:
int Hp;
int Gold;
Hp = 40;
Gold = 900;

// after:
int Hp = 40;
int Gold = 900;

// or (C++ only):
int Hp(40);
int Gold(900);


2) The function can do a bit more work, and should in order to be useful. Good code organization means keeping the input/output stuff separated away from the calculation stuff. So we'll put all the calculation into the function. The calculation is:

a) Check if enough gold is available.
b) If so, decrease gold and increase the stat.
c) Otherwise, do nothing.

There's one extra complication here, though: when we call this function to do the math, the input/output stuff also needs to know if enough gold was available, so that it can print a message if a change happened. We can do this with the function return value: we'll return a boolean that is true iff the HP are upgraded.

So we make a function like this:


bool upgradeHp(int& gold, int& HP) {
// we pass in gold and HP by reference, because we potentially change both.
// we also return the bool to say whether or not there was a change.
// So in the function we do the math:
if (Gold >= 500) {
Gold -= 500;
HP += 1;
return true; // because there was a change.
} else {
return false; // oops! Not enough gold!
}
}


And now we can call it like this:


if (choice == 1 && upgradeHp(Gold, Hp)) {
// if the choice wasn't 1, this will *not* call upgradeHp at all;
// this is called 'short-circuit evaluation'.
// If it is 1, then upgradeHp gets called; it will return false if there
// is not enough gold, so that this part also gets skipped.
// If upgradeHp returns true, then the whole conditional is true, and we
// print the message as required.
// Also notice that the parameter names don't match between the function call
// and the function implementation: the names are consistent on both "sides",
// but you can pass any variable you like to a function (as long as it's the
// right *type*).
cout << " Aye thank you for the Busines.\n";
cout << " You Gold After Paying the man is " << Gold << endl;
cout << " After the upgrade you Hp is " << Hp << endl;
}



As an exercise, pull out the printing into a function as well (think: what does it need to "know"? Will it change anything provided to it? Does the calling code require any communication back?), and then go back to your original code and set up these functions for the other cases. Then I can show you how to make good *use* of these functions in order to get rid of the duplication and just generally make your life easier ;)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!