| | |
Function trouble (beginner)
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
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.
*
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", "******* 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";
}•
•
Join Date: Oct 2009
Posts: 43
Reputation:
Solved Threads: 5
1
#3 21 Days Ago
•
•
•
•
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.*
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 nameStandard 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) 0
#4 21 Days Ago
•
•
•
•
Thereturnstatement 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 usereturnand return an int variable.)
Modified function declaration (pass by reference, arguments may be changed):
void medic(int & money, int & potionSize [, ...]);(noreturnrequired)
0
#5 21 Days Ago
Does not seem to work. 
Errors:

C++ Syntax (Toggle Plain Text)
/* 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", "******* 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:
C++ Syntax (Toggle Plain Text)
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)
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
1
#6 21 Days Ago
Lines 20, 88. Arrays already are passed by reference. Get rid of the ampersand:
Delete the ampersand in red above on line 20 and on line 88.
Line 96:
Declaring a new array called potionsize defeats the purpose of passing it to the function. Replace that line with this:
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.
void medic(int & money, int & potionsize[5]);Delete the ampersand in red above on line 20 and on line 88.
Line 96:
C++ Syntax (Toggle Plain Text)
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:
C++ Syntax (Toggle Plain Text)
for (int i = 0; i < 5; i++) { potionsize[i] = 0; }
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
0
#8 21 Days Ago
Do NOT call the main() function. If you want to repeat, your program should look something like this (starting at line 61):
So if the user chooses 1, you call the medic function. When you're done with the medic function, just return.
C++ Syntax (Toggle Plain Text)
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.
Last edited by VernonDozier; 21 Days Ago at 1:43 am.
•
•
Join Date: Jan 2008
Posts: 3,810
Reputation:
Solved Threads: 501
0
#10 21 Days Ago
•
•
•
•
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?
C++ Syntax (Toggle Plain Text)
return;
See my last post on what you need to do inside main().
![]() |
Similar Threads
- Help with my first step on Php (PHP)
- Newbie With Function Trouble (Python)
- bool function problems! (C++)
- Pascal calculator program (Pascal and Delphi)
- cin.ignore() ?????? (C++)
- Delete function for linked list (C++)
- Mac address / DHCP Problem (Networking Hardware Configuration)
- need a bit of help with my homework(operator overloading) (C++)
- Transferring data to Excel (ASP.NET)
- Pointers to struct (C++)
Other Threads in the C++ Forum
- Previous Thread: Issues with functions and pass by reference!
- Next Thread: Problems With Simple C++ Battle System; Need Help
| Thread Tools | Search this Thread |
api array based beginner bitmap c++ c/c++ calculator char class classes code coding compile compiler console conversion count database delete deploy desktop developer directshow dll download dynamic email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp homeworkhelper iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news node number output parameter pointer problem program programming project python random read recursion recursive return sorting string strings struct temperature template templates test text text-file tree unix url variable vector video visualstudio win32 windows winsock word wordfrequency wxwidgets






