Public Group

# Need help with calling a Function.

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

## Recommended Posts

#include <iostream>
using namespace std;

int UpgradeShop ( Hp , Defense , Agility , Intellegence , Dexerity , WillPower , Luck )
{
cout << " Hello and welcome to my Shop.\n\n";
cout << " Is there anything That I can Intrest you in./n/n";
cout << " 1 - Hp Upgrade.\n";
cout << " 2 - Defense UpGrade.\n";
cout << " 3 - Agility.\n";
cout << " 4 - Intellegence.\n";
cout << " 5 - Dexerity.\n";
cout << " 6 - WillPower.\n";
cout << " 7 - Luck.\n";

int choice;
coout << " Choice.\n";
cin >> choice;

switch ( choice ) {
case 1:
cout << " Aye I see that you'r intrested in the Hpgrade.\n";
cout << " It will cost 400 gold for 1 point if Hp.\n";
cout << " Do you want to do this.\n";
int choice;
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
cin >> choice;

if ( choice == 1 )
{
cout << " Ah thank you for the money.\n\n";
Gold -= 400;
cout << " Your Gold left after paying the man is " << Gold << endl;
Hp += 1;
cout << " Your Hp after the Upgrade is " << Hp << endl;
}
else
cout << " Please come again.\n";
break;
case 2:
cout << " Aye I see that you'r intrested in the Defense Upgrade.\n";
cout << " That will be 600 gold, for 1 point of Defense.\n";
cout << " Do you want to do this.\n";
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
int choice2;
cout << " Choice.\n";
cin >> " choice2;

if ( choice2 == 1 )
{
cout << " Ah thank you for the money!\n";
Gold -= 600;
cout << " Your Gold Left after paying the man is " << Gold << endl;
Defense += 1;
cout << " Your Defense after the Upgrade is " << Defense << endl;
}
else
cout << " Please come back when you have the Money.\n";
break;
case 3;
cout << " Aye I see that you'r intrested in the Agility Upgrade.\n";
cout << " That will be 200 for, 1 point of Agility.\n";
cout << " Do you want to do this?\n";
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
int choice3;
cout << " choice .\n";
cin >> choice3;

if ( choice == 3 )
{
cout << " Ah Thank you for the Money!\n";
Gold -= 200;
cout << " Your gold after paying the man is " << Gold << endl;
Agility += 1;
cout << " You'r Agility after the upgrade is << Agility << endl;
}
else
cout << " Please come back when you have the money!\n";
break;
case 4:
cout << " Aye I see that you'r interested in the Intellegence Upgrade.\n";
cout << " That will be 700 for 1 point of Intellegence.\n";
cout << " Do you want to do this?\n";
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
int choice4;
cout << " Choice.\n";
cin >> choice4;

if ( choice4 == 1 )
{
cout << " Aye Thank you for the Money.\n";
Gold -= 700;
cout << " Your gold after paying the man is " << Gold << endl;
Intellegence += 1;
cout << " You Intellegence after the upgrade is " << Intellegence << endl;
}
else
cout << " Please come back when u have the money.\n";
break;
case 5:
cout << " Aye I see that you are intrested in the Dexerity upgrade.\n";
cout << " It will cost you 600 gold, for 1 point of Dexerity.\n";
cout << " Do you wish to do this?\n";
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
int choice5;
cout << " Choice.\n";
cin >> choice5;

if ( choice5 == 1 )
{
cout << " Aye Thank you for the money.\n":
Gold -= 600;
cout << " Your gold after paying the man is " << Gold << endl;
Dexerity += 1;
cout << " You Decerity after the Upgrade is " << Dexerity << endl;
}
else
cout << " Please come back when u have the money.\n";
break;
case 6:
cout << " Aye I see that you are intrested in the willpower Upgrade.\n";
cout << " It will cost 400 for, 1 point of willpower\n";
cout << " Do you want to do this?\n";
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
int choice6;
cout << " Choice.\n";
cin >> choice6;

if ( choice6 == 1 )
{
cout << " Aye Thank you for the Money.\n";
Gold -= 400;
cout << " Your gold after paying the man is " << Gold << endl;
WillPower += 1;
cout << " Your WillPower after the upgrade is " << WillPower << endl;
}
else
cout << " Please come back When you have the money.\n";
break;
case 7:
cout << " Aye I see that you are intrested in the Luck upgrade.\n";
cout << " It will cost you 100 gold, for 1 point of Luck.\n";
cout << " Do you want to do this?\n";
cout << " 1 - Yes.\n";
cout << " 2 - No.\n";
int choice7;
cout << " Choice.\n";
cin >> choice7;

if ( choice7 == 1 )
{
cout << " Aye Thank you for the Money.\n";
Gold -= 100;
cout << " Your gold after paying the man is " << Gold << endl;
Luck += 1;
cout << " Your Luck after upgrade is " << Luck << endl;
}
else
cout << " Please come back when you have the money.\n";

}			break;
}
}
return 0;
}



##### Share on other sites
I wrote that function today and I have to question about it. Well the fist one is that I am trying to fugre out to call that function. The second is that this function is meant to ad stats to the player sats I was wondering if I should leave it int a the return value are should I do void. I think it is return becasue it is returing the upgrades to hp and so on.

##### Share on other sites
wow ok I dont know why people are giving me bad rating's i have not done anything to anyone on this forum. So can people stop giving me bad rating I am just asking fror help alot casue I am a being c++ programing. I can understand if I was rude to someone are did something wrong but all my post have ither been asking for help are teeling somone they did a good job.

##### Share on other sites
To call that function you will need to change the function declaration to:
int UpgradeShop ( int &Hp , int &Defense , int &Agility , int &Intellegence , int &Dexerity , int &WillPower , int &Luck );
That's assuming all your stat variables are integers. If they are not, simply replace the data type. Now somewhere in your code when the player enters the shop, you will simply call:
UpgradeShop( Player.HP, ... , Player.Luck );
Where the variables used represent the current players stat variables. Note that you do not use the ..., I just didn't want to type out everything. You should end up passing in a variable for each int used in the declaration in the specific order. I.E. don't pass in the Luck first, you have to pass in the HP variable.

That's about it really. You should probabally just change it to return void, since the function always just returns 0, so there is no need for a return value.

I'm a little busy at the moment with HW, but I'll post a little upgrade alternative to you function if you decide to check back on this thread.

##### Share on other sites
int choice;
coout > choice;

Might want to change that to cout not coout. Also slow down and use check your spelling. You do live in an English speaking country to have such horrid grammar and spelling is just wrong.

Also you did not say actually ASK any questions...

Also not sure if that's how your code is actually formatted or if it's the site but man is it hard to read. I think you're trying to do to much in one function. Maybe make up a class to hold the data and just pass a pointer to the class so you can update that. You also don't say what any of the data is... int, float, char in the declaration of the function:
int UpgradeShop ( Hp , Defense , Agility , Intellegence , Dexerity , WillPower , Luck )

Should be:
int UpgradeShop ( int Hp , float Defense , char Agility , int Intellegence , long Dexerity , int WillPower , int Luck )

Note that the above is just an example.

I think you need to hit a few more "Learning C++" websites, just check google there's dozens of good ones!

##### Share on other sites
1) C++ is very strict about how you type things in. The compiler cannot read your mind. With practice (and lots of 'yelling' from the compiler) you will learn to be more careful. :)

2) When you declare or define a function, you need to specify a type for each "parameter" that is passed in. When you call the function, you provide variables that will be used for the parameters. Then the return value is used "in place of" the function call in the expression containing the call.

  #include <iostream>  using namespace std;  int doSomeMathWith(int a, int b) {//^^^1     ^^^2      ^^^3^4 ^^^5^6// 1 = type of the function return value.// 2 = name of function.// 3 = type of first parameter (or "argument").// 4 = name that the first parameter will have within the function.// 5, 6 = same for second parameter.// Since we have an { at the end of the line, this is the beginning of the// function implementation.// We could write the same thing with ; at the end; that line by itself would// then just say "hey, I am going to write a function matching this later".// (Sometimes the compiler requires such "prototypes", because, well, that's// how it used to work. Back when we didn't know as much about writing// compilers, and the computers were a lot slower, it helped out a lot, though.)    int c = 42 * a + 23 * b;    // Here 'c' is local to this function.    return c + 123; // The return statement tells us what we will return.    // It will be implicitly cast to the return type if it doesn't match;    // if that cast is not possible, the compiler will complain.    // Here we have an int value to return already, so we're good to go.  }  int main() {// "main" is a function too! So you've really been using them all along.    int foo = 3;    int bar = 4;    // Calling the function:    int baz = doSomeMathWith(foo, bar);    //             ^^^1      ^^^2 ^^^3    // 1 = name of function we want to call    // 2,3 = values to use for the call.    // For this time calling the function, 'foo' gets passed in as 'a' and    // 'bar' gets passed in as 'b'. So this is the same as doSomeMathWith(3, 4).    // The function does its stuff with 'a' and 'b' already having those values    // and then returns whatever value; finally, that return value is assigned    // to baz (because the function call is replaced by its result in the    // expression).    cout << baz << endl; // if my mental arithmetic is up to par,                         // this should output 341.    cout << doSomeMathWith(foo, bar) << endl; // you can do it this way, too.  }

There is one bit not really demonstrated by the example: the parameters are "passed by value" normally, which means that the locals 'a' and 'b' are separate variables from 'foo' and 'bar' during that function call - they just are assigned the same value to start out with. So if we changed a or b within doSomeMathWith(), and then output foo and bar, they would still be 3 and 4 respectively. To get the other behaviour (i.e. make the variables "be the same variable" during the function call, so that a change inside the function is "seen" by the caller), you need to "pass by reference".

The function is responsible for making this decision (not the caller), and it applies for every call of the function. However, you at least get to do it on a per-parameter basis. This is what the &'s are for in the "function signature":

int doOtherMathWith(int& a, int b) {  a = 3; // affects the caller  b = 3; // does not affect the caller  return a*b; // always is 9, of course, but that's beside the point ;)}int main() {  int foo = 42;  int bar = 23;  cout << doOtherMathWith(foo, bar) << endl; // shows 9  cout << foo << bar << endl; // shows 3 23}

##### Share on other sites
Ok guys I am understanding fucntions a little better. as to my spelling I know it is bad and I work hard on making it right, i dont have alot of time to code. I work three jobs and when ever I have free time I read and work on writting code. and yes Drew_Benton it would be very cool to see a altervie to the code I wrote.

1. 1
Rutin
34
2. 2
3. 3
4. 4
5. 5

• 12
• 14
• 9
• 9
• 9
• ### Forum Statistics

• Total Topics
633334
• Total Posts
3011410
• ### Who's Online (See full list)

There are no registered users currently online

×