Function trouble (beginner)

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Oct 2009
Posts: 127
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster

Function trouble (beginner)

 
0
  #1
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. *

/*  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";
}
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 281
Reputation: jonsca is an unknown quantity at this point 
Solved Threads: 31
jonsca jonsca is online now Online
Posting Whiz in Training
Join Date: Oct 2009
Posts: 43
Reputation: Fbody is an unknown quantity at this point 
Solved Threads: 5
Fbody Fbody is offline Offline
Light Poster
 
1
  #3
21 Days Ago
Originally Posted by restrictment View Post
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)
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 127
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #4
21 Days Ago
Originally Posted by Fbody View Post
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 127
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #5
21 Days Ago
Does not seem to work.
  1. /* Game Created by Jacob Caron. Started on 11/2/09 */
  2.  
  3. //header declarations
  4. #include <iostream>
  5. #include <stdlib.h>
  6. #include <conio.h>
  7. #include <iomanip>
  8. #include <string>
  9. #include <time.h>
  10. #include <windows.h>
  11. using namespace std;
  12.  
  13. //used to maximize screen
  14. #pragma comment(lib,"kernel32.lib")
  15. #pragma comment(lib,"user32.lib")
  16.  
  17. //initializing functions
  18. void level2();
  19. void level3();
  20. void medic(int & money, int & potionsize[5]);
  21.  
  22. //main function
  23. int main()
  24. {
  25. //tells screen to maximize
  26. HWND hWnd = GetConsoleWindow();
  27. ShowWindow(hWnd,SW_MAXIMIZE);
  28.  
  29. /*****array declarations*****/
  30.  
  31. //armor
  32. int armorcost[9] = {4, 9, 15, 22, 30, 39, 49, 60, 72};
  33. char armorname[9][25] = {"Shirt", "Fur", "Leather", "Studded Leather", "Chain Mail", "Scale Mail", "Plate Mail", "Spiked Plate Mail", "Berzerker Aromor"};
  34. int armorstrength[9] = {2, 7, 13, 20, 27, 35, 44, 54, 65};
  35. //weapons
  36. int weaponcost[9] = {9, 14, 20, 27, 35, 44, 54, 65, 77};
  37. char weaponname[9][25] = {"Dagger", "Club", "Axe", "Short Sword", "Cutlass", "******* Sword", "Long Sword", "Great Sword", "Excaliber"};
  38. int weaponstrength[9] = {7, 12, 18, 25, 32, 40, 49, 59, 70};
  39. //menu array
  40. char menu[5][25] = {"Visit the town medic", "Visit the Weapon Smith", "Go to the Armory", "Go to the trail", "View Stats"};
  41. //counter
  42. int count1;
  43. //name
  44. string name;
  45. //money
  46. int money = 10;
  47.  
  48. /*****Introduction*****/
  49. cout << "Please Enter your name (One Word): ";
  50. cin >> name;
  51. system("cls");
  52. cout << "Welcome to Grundor, " << name << ", the village were you were born.\n"
  53. << "Your mother, Marlene was abducted by the Horrid Dragon, Helsmich, in the land \n"
  54. << "of fire. In order for you to save your mother, you must travel along the \n"
  55. << "twisted trail into lands that are filled with terror. I bid you the best of \n"
  56. << "luck, and here is $10 to spend on supplies. I know it is not much, but I am \n"
  57. << "not the wealthiest man alive!\n\n";
  58. system("pause");
  59. system("cls");
  60.  
  61. /*****Main Menu*****/
  62.  
  63. //ask what user wants to do
  64. cout << "What would you like to do?\n\n";
  65. for(count1=0;count1<5;count1++)
  66. cout << count1+1 << ". " << menu[count1] << endl;
  67. cout << endl << ">> ";
  68. cout << money;
  69. cin >> count1;
  70. system("cls");
  71. //switch to change main event
  72. switch(count1)
  73. {
  74. case 1:
  75. medic(money, potionsize);
  76. break;
  77. }
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84.  
  85. }
  86. /*****Menu Options*****/
  87.  
  88. void medic(int & money, int & potionsize[5])
  89. {
  90. /*****Visiting the Medic*****/
  91.  
  92. //potions
  93. char potionname[5][25] = {"Small Tonic", "Medium Tonic", "Large Tonic", "Grand Tonic", "Godly Tonic"};
  94. int potioncost[5] = {5, 10, 15, 20, 30};
  95. int potioneffect[5] = {5, 10, 20, 40, 100};
  96. int potionsize[5] = {0, 0, 0, 0, 0};
  97.  
  98. system("cls");
  99. cout << "Welcome to the Medic, what can I do you for?\n" << endl;
  100. int x=0;
  101. for(x;x<5;x++)
  102. cout << x+1 << ". " << potionname[x] << " - " << potioncost[x] << endl;
  103. cout << "\n>> ";
  104. cin >> x;
  105. system("cls");
  106. money = money - potioncost[x-1];
  107. potionsize[x-1] = potionsize[x-1] + 1;
  108. cout << "You have succesfully purchased a " << potionname[x-1] << "!\n"
  109. << "You have lost " << potioncost[x-1] << " dollars!\n"
  110. << "You now have " << money << " dollars.\n\n";
  111. cout << "----------------------------\n"
  112. << "You have: \n";
  113. for(x=0;x<5;x++)
  114. cout << potionsize[x] << " " << potionname[x] << "(s)" << endl;
  115. cout << "----------------------------\n\n";
  116. system("pause");
  117. system("cls");
  118. cout << "Would you like to buy more?\n\n";
  119. cout << "1. yes\n2. no\n\n>> ";
  120. cin >> x;
  121. switch(x)
  122. {
  123. case 1:
  124. medic();
  125. break;
  126. case 2:
  127. main();
  128. break;
  129. }
  130. }
  131.  
  132.  
  133. //Other Functions
  134. void level2()
  135. {
  136. cout << "As you look around the trail, you see a fox tearing apart a corpse in the dirt." << endl
  137. << "As you try to shoo away the fox it turns on you, and acts aggrssivly.\nPrepare for battle!\n\n";
  138. }

Errors:
  1. 1>.\gamex.cpp(20) : error C2234: 'potionsize' : arrays of references are illegal
  2. 1>.\gamex.cpp(75) : error C2065: 'potionsize' : undeclared identifier
  3. 1>.\gamex.cpp(88) : error C2234: 'potionsize' : arrays of references are illegal
  4. 1>.\gamex.cpp(96) : error C2082: redefinition of formal parameter 'potionsize'
  5. 1>.\gamex.cpp(124) : error C2660: 'medic' : function does not take 0 arguments
  6. 1>Build log was saved at "file://c:\Users\Jake\Documents\Visual Studio 2008\Projects\gamex\gamex\Debug\BuildLog.htm"
  7. 1>gamex - 5 error(s), 0 warning(s)
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
1
  #6
21 Days Ago
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:

  1. 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:

  1. for (int i = 0; i < 5; i++)
  2. {
  3. potionsize[i] = 0;
  4. }
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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 127
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #7
21 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
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):

  1. do
  2. {
  3. // display user's options
  4. // ask user to input option.
  5.  
  6. switch (/* user option variable */)
  7. {
  8. case 1:
  9. // call medic function
  10. break;
  11. case 2:
  12. // do something else
  13. break;
  14. // more cases
  15. };
  16. }
  17. 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.
Reply With Quote Quick reply to this message  
Join Date: Oct 2009
Posts: 127
Reputation: restrictment is on a distinguished road 
Solved Threads: 9
restrictment's Avatar
restrictment restrictment is offline Offline
Junior Poster
 
0
  #9
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?
Last edited by restrictment; 21 Days Ago at 1:53 am.
Reply With Quote Quick reply to this message  
Join Date: Jan 2008
Posts: 3,810
Reputation: VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute VernonDozier has a reputation beyond repute 
Solved Threads: 501
Featured Poster
VernonDozier VernonDozier is offline Offline
Senior Poster
 
0
  #10
21 Days Ago
Originally Posted by restrictment View Post
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:

  1. return;

See my last post on what you need to do inside main().
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC