Hello, I am trying to make an rpg, and I am having troubles with functions. I am currently working on the 'potion' aspect of my rpg, which I have highlighted in red.

My question is this: Is it possible to make the function return money, potion number, potion size, etc? I really need all of that data to be transferred into the main function. HELP!

*I will try to pay back the favor to the best of my ability. :)*

/*  Game Created by Jacob Caron. Started on 11/2/09  */

//header declarations
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
#include <string>
#include <time.h>
#include <windows.h>
using namespace std;

//used to maximize screen
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")

//initializing functions
void level2();
void level3();
int medic(int money);

//main function
int main()
{
	//tells screen to maximize
	HWND hWnd = GetConsoleWindow();
	ShowWindow(hWnd,SW_MAXIMIZE);

	/*****array declarations*****/

	//armor
	int armorcost[9] = {4, 9, 15, 22, 30, 39, 49, 60, 72};
	char armorname[9][25] = {"Shirt", "Fur", "Leather", "Studded Leather", "Chain Mail", "Scale Mail", "Plate Mail", "Spiked Plate Mail", "Berzerker Aromor"};
    int armorstrength[9] = {2, 7, 13, 20, 27, 35, 44, 54, 65};
	//weapons
	int weaponcost[9] = {9, 14, 20, 27, 35, 44, 54, 65, 77};
	char weaponname[9][25] = {"Dagger", "Club", "Axe", "Short Sword", "Cutlass", "Bastard Sword", "Long Sword", "Great Sword", "Excaliber"};
    int weaponstrength[9] = {7, 12, 18, 25, 32, 40, 49, 59, 70};
	//menu array
	char menu[5][25] = {"Visit the town medic", "Visit the Weapon Smith", "Go to the Armory", "Go to the trail", "View Stats"};
	//counter
	int count1;
	//name
	string name;
	//money
	int money = 10;

	/*****Introduction*****/
	cout << "Please Enter your name (One Word): ";
	cin >> name;
	system("cls");
	cout << "Welcome to Grundor, " << name << ", the village were you were born.\n"
		<< "Your mother, Marlene was abducted by the Horrid Dragon, Helsmich, in the land \n"
		<< "of fire. In order for you to save your mother, you must travel along the \n"
		<< "twisted trail into lands that are filled with terror. I bid you the best of \n"
		<< "luck, and here is $10 to spend on supplies. I know it is not much, but I am \n"
		<< "not the wealthiest man alive!\n\n";
	system("pause");
	system("cls");

	/*****Main Menu*****/

	//ask what user wants to do
	cout << "What would you like to do?\n\n";
	for(count1=0;count1<5;count1++)
		cout << count1+1 << ". " << menu[count1] << endl;
	cout << endl << ">> ";
	cout << money;
	cin >> count1;
	system("cls");
	//switch to change main event
	switch(count1)
	{
	case 1:
		medic(money);
		break;
	}
	






}
/*****Menu Options*****/

int medic(int money)
{
	/*****Visiting the Medic*****/

	//potions
    char potionname[5][25] = {"Small Tonic", "Medium Tonic", "Large Tonic", "Grand Tonic", "Godly Tonic"};
    int potioncost[5] = {5, 10, 15, 20, 30};
    int potioneffect[5] = {5, 10, 20, 40, 100};
	int potionsize[5] = {0, 0, 0, 0, 0};

	system("cls");
	cout << "Welcome to the Medic, what can I do you for?\n" << endl;
	int x=0;
	for(x;x<5;x++)
		cout << x+1 << ". " << potionname[x] << " - " << potioncost[x] << endl;
	cout << "\n>> ";
	cin >> x;
	system("cls");
	money = money - potioncost[x-1];
	potionsize[x-1] = potionsize[x-1] + 1;
	cout << "You have succesfully purchased a " << potionname[x-1] << "!\n"
		<< "You have lost " << potioncost[x-1] << " dollars!\n"
		<< "You now have " << money << " dollars.\n\n";
	cout << "----------------------------\n"
		<< "You have: \n";
	for(x=0;x<5;x++)
		cout << potionsize[x] << " " << potionname[x] << "(s)" << endl;
	cout << "----------------------------\n\n";
	system("pause");
	system("cls");
	cout << "Would you like to buy more?\n\n";
	cout << "1. yes\n2. no\n\n>> ";
	cin >> x;
	switch(x)
	{
	case 1:
		medic(money);
		break;
	case 2:
		main();
		break;
	}
}


//Other Functions
void level2()
{
	cout << "As you look around the trail, you see a fox tearing apart a corpse in the dirt." << endl
		<< "As you try to shoo away the fox it turns on you, and acts aggrssivly.\nPrepare for battle!\n\n";
}

Hello, I am trying to make an rpg, and I am having troubles with functions. I am currently working on the 'potion' aspect of my rpg, which I have highlighted in red.

My question is this: Is it possible to make the function return money, potion number, potion size, etc? I really need all of that data to be transferred into the main function. HELP!

*I will try to pay back the favor to the best of my ability. :)*

The return statement can only return one (1) value. In order to get around this, you will have to pass all of the desired variables to the function as reference parameters. To pass as a reference parameter add the ampersand character "&" between the dataType and the parameter name

Standard function declaration (pass by value, arguments not changeable): int medic( int money); (must use return and return an int variable.)

Modified function declaration (pass by reference, arguments may be changed): void medic(int & money, int & potionSize [, ...]); (no return required)

commented: Very Helpful +1

The return statement can only return one (1) value. In order to get around this, you will have to pass all of the desired variables to the function as reference parameters. To pass as a reference parameter add the ampersand character "&" between the dataType and the parameter name

Standard function declaration (pass by value, arguments not changeable): int medic( int money); (must use return and return an int variable.)

Modified function declaration (pass by reference, arguments may be changed): void medic(int & money, int & potionSize [, ...]); (no return required)

Thanks. I will try this soon.

Does not seem to work. :(

/*  Game Created by Jacob Caron. Started on 11/2/09  */

//header declarations
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
#include <string>
#include <time.h>
#include <windows.h>
using namespace std;

//used to maximize screen
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")

//initializing functions
void level2();
void level3();
void medic(int & money, int & potionsize[5]);

//main function
int main()
{
	//tells screen to maximize
	HWND hWnd = GetConsoleWindow();
	ShowWindow(hWnd,SW_MAXIMIZE);

	/*****array declarations*****/

	//armor
	int armorcost[9] = {4, 9, 15, 22, 30, 39, 49, 60, 72};
	char armorname[9][25] = {"Shirt", "Fur", "Leather", "Studded Leather", "Chain Mail", "Scale Mail", "Plate Mail", "Spiked Plate Mail", "Berzerker Aromor"};
    int armorstrength[9] = {2, 7, 13, 20, 27, 35, 44, 54, 65};
	//weapons
	int weaponcost[9] = {9, 14, 20, 27, 35, 44, 54, 65, 77};
	char weaponname[9][25] = {"Dagger", "Club", "Axe", "Short Sword", "Cutlass", "Bastard Sword", "Long Sword", "Great Sword", "Excaliber"};
    int weaponstrength[9] = {7, 12, 18, 25, 32, 40, 49, 59, 70};
	//menu array
	char menu[5][25] = {"Visit the town medic", "Visit the Weapon Smith", "Go to the Armory", "Go to the trail", "View Stats"};
	//counter
	int count1;
	//name
	string name;
	//money
	int money = 10;

	/*****Introduction*****/
	cout << "Please Enter your name (One Word): ";
	cin >> name;
	system("cls");
	cout << "Welcome to Grundor, " << name << ", the village were you were born.\n"
		<< "Your mother, Marlene was abducted by the Horrid Dragon, Helsmich, in the land \n"
		<< "of fire. In order for you to save your mother, you must travel along the \n"
		<< "twisted trail into lands that are filled with terror. I bid you the best of \n"
		<< "luck, and here is $10 to spend on supplies. I know it is not much, but I am \n"
		<< "not the wealthiest man alive!\n\n";
	system("pause");
	system("cls");

	/*****Main Menu*****/

	//ask what user wants to do
	cout << "What would you like to do?\n\n";
	for(count1=0;count1<5;count1++)
		cout << count1+1 << ". " << menu[count1] << endl;
	cout << endl << ">> ";
	cout << money;
	cin >> count1;
	system("cls");
	//switch to change main event
	switch(count1)
	{
	case 1:
		medic(money, potionsize);
		break;
	}
	






}
/*****Menu Options*****/

void medic(int & money, int & potionsize[5])
{
	/*****Visiting the Medic*****/

	//potions
    char potionname[5][25] = {"Small Tonic", "Medium Tonic", "Large Tonic", "Grand Tonic", "Godly Tonic"};
    int potioncost[5] = {5, 10, 15, 20, 30};
    int potioneffect[5] = {5, 10, 20, 40, 100};
	int potionsize[5] = {0, 0, 0, 0, 0};

	system("cls");
	cout << "Welcome to the Medic, what can I do you for?\n" << endl;
	int x=0;
	for(x;x<5;x++)
		cout << x+1 << ". " << potionname[x] << " - " << potioncost[x] << endl;
	cout << "\n>> ";
	cin >> x;
	system("cls");
	money = money - potioncost[x-1];
	potionsize[x-1] = potionsize[x-1] + 1;
	cout << "You have succesfully purchased a " << potionname[x-1] << "!\n"
		<< "You have lost " << potioncost[x-1] << " dollars!\n"
		<< "You now have " << money << " dollars.\n\n";
	cout << "----------------------------\n"
		<< "You have: \n";
	for(x=0;x<5;x++)
		cout << potionsize[x] << " " << potionname[x] << "(s)" << endl;
	cout << "----------------------------\n\n";
	system("pause");
	system("cls");
	cout << "Would you like to buy more?\n\n";
	cout << "1. yes\n2. no\n\n>> ";
	cin >> x;
	switch(x)
	{
	case 1:
		medic();
		break;
	case 2:
		main();
		break;
	}
}


//Other Functions
void level2()
{
	cout << "As you look around the trail, you see a fox tearing apart a corpse in the dirt." << endl
		<< "As you try to shoo away the fox it turns on you, and acts aggrssivly.\nPrepare for battle!\n\n";
}

Errors:

1>.\gamex.cpp(20) : error C2234: 'potionsize' : arrays of references are illegal
1>.\gamex.cpp(75) : error C2065: 'potionsize' : undeclared identifier
1>.\gamex.cpp(88) : error C2234: 'potionsize' : arrays of references are illegal
1>.\gamex.cpp(96) : error C2082: redefinition of formal parameter 'potionsize'
1>.\gamex.cpp(124) : error C2660: 'medic' : function does not take 0 arguments
1>Build log was saved at "file://c:\Users\Jake\Documents\Visual Studio 2008\Projects\gamex\gamex\Debug\BuildLog.htm"
1>gamex - 5 error(s), 0 warning(s)

Lines 20, 88. Arrays already are passed by reference. Get rid of the ampersand:

void medic(int & money, int & potionsize[5]);

Delete the ampersand in red above on line 20 and on line 88.

Line 96:

int potionsize[5] = {0, 0, 0, 0, 0};

Declaring a new array called potionsize defeats the purpose of passing it to the function. Replace that line with this:

for (int i = 0; i < 5; i++)
{
   potionsize[i] = 0;
}

Now when you change potionsize[] in then function, the changes will be reflected when you return from the function, which was the whole idea of passing potionsize[] by reference.

commented: Helpful +1

Ah, thanks a bunch. However, my variables keep getting reset, because when I call the main function, it re-declares all of the variables! >.<

Is there any way I could tell it to start back at the menu if the user selects '2'(no) after the medic function?

Do NOT call the main() function. If you want to repeat, your program should look something like this (starting at line 61):

do
{
  // display user's options
  // ask user to input option.

  switch (/* user option variable */)
  {
     case 1:
         // call medic function
         break;
     case 2:
         // do something else
          break;
     // more cases
  };
}
while (/* user hasn't chosen to exit */);

So if the user chooses 1, you call the medic function. When you're done with the medic function, just return.

oh..so I can't get back to the main function again?

Edit: I just want the user to go back to the menu section if they select 2..but this is not possible?

oh..so I can't get back to the main function again?

Edit: I just want the user to go back to the menu section if they select 2..but this is not possible?

That's what "return" does. It takes you back to main, to the place right after where you called the medic function, which puts you back in the main function. So if the person enters 2 in the medic function. you execute "return" instead of "main()". I said don't CALL main(), I didn't say don't RETURN to main (). Replace line 127 with this line:

return;

See my last post on what you need to do inside main().

oh..so I can't get back to the main function again?

Edit: I just want the user to go back to the menu section if they select 2..but this is not possible?

Logically, you never actually left the main function.

When you are in a function and call another function, the called function executes as a small part of the calling function's instructions. Once it's complete, the program automatically jumps back to the next instruction after the function call.

You NEVER have to call main, it's called automatically when the program starts and terminates when the program ends.

Like VernonDozier mentioned, to re-display the menu and perform another action, you will want to create a loop (inside main) that contains the statements to display and process the menu, then re-start the loop each time you want to display the menu.

Oh, ok. Thanks you two for helping me out.

actually, it didn't solve my problem. Although I can go from the medic function to the main function quite easily, the money did not transfer! Even if I buy 15$ worth of stuff (to make my money -5$), when I go back to the main function I still have 10$!!

Please help.
Here is the current code I have:

/*  Game Created by Jacob Caron. Started on 11/2/09  */
 
//header declarations
#include <iostream>
#include <stdlib.h>
#include <conio.h>
#include <iomanip>
#include <string>
#include <time.h>
#include <windows.h>
using namespace std;
 
//used to maximize screen
#pragma comment(lib,"kernel32.lib")
#pragma comment(lib,"user32.lib")
 
//initializing functions
void level2();
void level3();
void medic(int money,int potionsize[5],int potioncost[5],int potioneffect[5]);
 
//main function
int main()
{
	//tells screen to maximize
	HWND hWnd = GetConsoleWindow();
	ShowWindow(hWnd,SW_MAXIMIZE);
 
	/*****array declarations*****/

	//potions

    char potionname[5][25] = {"Small Tonic", "Medium Tonic", "Large Tonic", "Grand Tonic", "Godly Tonic"};
    int potioncost[5] = {5, 10, 15, 20, 30};
    int potioneffect[5] = {5, 10, 20, 40, 100};
	int potionsize[5] = {0, 0, 0, 0, 0};
	//armor
	int armorcost[9] = {4, 9, 15, 22, 30, 39, 49, 60, 72};
	char armorname[9][25] = {"Shirt", "Fur", "Leather", "Studded Leather", "Chain Mail", "Scale Mail", "Plate Mail", "Spiked Plate Mail", "Berzerker Aromor"};
    int armorstrength[9] = {2, 7, 13, 20, 27, 35, 44, 54, 65};
	//weapons
	int weaponcost[9] = {9, 14, 20, 27, 35, 44, 54, 65, 77};
	char weaponname[9][25] = {"Dagger", "Club", "Axe", "Short Sword", "Cutlass", "Bastard Sword", "Long Sword", "Great Sword", "Excaliber"};
    int weaponstrength[9] = {7, 12, 18, 25, 32, 40, 49, 59, 70};
	//menu array
	char menu[5][25] = {"Visit the town medic", "Visit the Weapon Smith", "Go to the Armory", "Go to the trail", "View Stats"};
	//counter
	int count1;
	//name
	string name;
	//money
	int money = 10;
 
	/*****Introduction*****/
	cout << "Please Enter your name (One Word): ";
	cin >> name;
	system("cls");
	cout << "Welcome to Grundor, " << name << ", the village were you were born.\n"
		<< "Your mother, Marlene was abducted by the Horrid Dragon, Helsmich, in the land \n"
		<< "of fire. In order for you to save your mother, you must travel along the \n"
		<< "twisted trail into lands that are filled with terror. I bid you the best of \n"
		<< "luck, and here is $10 to spend on supplies. I know it is not much, but I am \n"
		<< "not the wealthiest man alive!\n\n";
	system("pause");
	system("cls");
 
	/*****Main Menu*****/

	//ask what user wants to do
	do{
		cout << "What would you like to do?\n\n";
		for(count1=0;count1<5;count1++)
			cout << count1+1 << ". " << menu[count1] << endl;
		cout << endl << ">> ";
		cout << money;
		cin >> count1;
		system("cls");
		//switch to change main event
		switch(count1)
		{
		case 1:
			medic(money, potionsize, potioncost, potioneffect);
			break;
		default:
			break;
		}
	}while(count1!=5);

}
/*****Menu Options*****/
 
void medic(int money,int potionsize[5],int potioncost[5],int potioneffect[5])
{
	/*****Visiting the Medic*****/

	//minor decloration
    char potionname[5][25] = {"Small Tonic", "Medium Tonic", "Large Tonic", "Grand Tonic", "Godly Tonic"};
 
	system("cls");
	cout << "Welcome to the Medic, what can I do you for?\n" << endl;
	int x=0;
	for(x;x<5;x++)
		cout << x+1 << ". " << potionname[x] << " - " << potioncost[x] << endl;
	cout << "\n>> ";
	cin >> x;
	system("cls");
	money = money - potioncost[x-1];
	potionsize[x-1] = potionsize[x-1] + 1;
	cout << "You have succesfully purchased a " << potionname[x-1] << "!\n"
		<< "You have lost " << potioncost[x-1] << " dollars!\n"
		<< "You now have " << money << " dollars.\n\n";
	cout << "----------------------------\n"
		<< "You have: \n";
	for(x=0;x<5;x++)
		cout << potionsize[x] << " " << potionname[x] << "(s)" << endl;
	cout << "----------------------------\n\n";
	system("pause");
	system("cls");
	cout << "Would you like to buy more?\n\n";
	cout << "1. yes\n2. no\n\n>> ";
	cin >> x;
	switch(x)
	{
	case 1:
		medic(money, potionsize, potioncost, potioneffect);
		break;
	default:
		break;
	}
	system("cls");
}
 
 
//Other Functions
void level2()
{
	cout << "As you look around the trail, you see a fox tearing apart a corpse in the dirt." << endl
		<< "As you try to shoo away the fox it turns on you, and acts aggrssivly.\nPrepare for battle!\n\n";
}

Lines 20 and 92. You passed the money variable by value. You need to pass it by reference. Put ampersands between int and money on both lines

Line 20: void medic(int money,int potionsize[5],int potioncost[5],int potioneffect[5]); needs to be: void medic(int & money,int potionsize[5],int potioncost[5],int potioneffect[5]); Do the same with line 92

You missed the point in references. When a variable is passed to a function, a copy is made, so you can't change the original. When a pointer, or an array, is passed you CAN change the value, it just tells the function where to find the original.
To be able to change a variable from a function, you have to tell it where to find the original instead of passing it a copy. Just place a '&' behind the type to pass it's location, as mentioned before.

In short, change line 20 and 92 to:

void medic(int& money,int potionsize[5],int potioncost[5],int potioneffect[5])

Ha, I finally figured it out. I am sorry I am such a newb at this, and didn't get it the first time..but thanks a bunch for your help. :)

If you want to continue program this game, you should learn more about c++, specially classes and inheritance and try to use it in your program. Programming like that you will soon stop at the moment, when adding something new will become too painful and overcomplicated.

If you want to continue program this game, you should learn more about c++, specially classes and inheritance and try to use it in your program. Programming like that you will soon stop at the moment, when adding something new will become too painful and overcomplicated.

Actually, I think I will finish the program without too much complication. Please see my other RPG I managed to complete: http://www.daniweb.com/code/snippet234266.html
~as you can see it is extremely disorderly, but I am able to edit it just fine. I am sure if I was able to get through that one, I will be able to get through this one as well. :)

Thanks for your concern though.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.