Help with Functions

UPDATE
Hello, just started learning C++ and Im writing a little game.

How would I get line 148-156 to rerun after PlayerClass is done? Ive looked at for loops, but cant find anything that would let me look around the room again after I open one of the door, window, or chest.
Also any help on errors that you might find would be great, variables are displaying strange values in the program.

I have the feeling im just writing this completely wrong, any info on how Id do it would be much appreciated.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#include <iostream>
#include <string>

using namespace std;



	int PlayerStats()
{
	
int spec;
int stamina;
int agility;
int intellect;
int luck;

cout << "\nSelect Class Stats to View\n" << endl;
cout << "1 = Warrior" << endl;
cout << "2 = Mage" << endl;
cout << "3 = Rogue" << endl;
cout << "4 = Priest" << endl;

	if (spec == 1)
	{stamina = 4, agility = 2, intellect = 0, luck = 2;}
	
	else if (spec == 2)
	{stamina = 1, agility = 1, intellect = 4, luck = 3;}
	
	else if (spec == 3)
	{stamina = 1, agility = 4, intellect = 1, luck = 4;}
	
	else if (spec == 4)
	{stamina = 1, agility = 0, intellect = 5, luck = 2;}	

cin >> spec;

cout << "Current Class Stats \n\n" << endl;
cout << "Stamina = " << stamina << endl;
cout << "Intellect = " << intellect << endl;
cout << "Agility = " << agility << endl;
cout << "Luck = " << luck << endl;



	return spec;
}



		int PlayerClass()
{
	string playerclass;
	int stam;
	int agil;
	int spell;
	int luck;
	

cout << "		  Gift of the Warrior           Gift of the Rogue\n" << endl;
cout << "		  Gift of the Mage              Gift of the Priest\n\n" << endl;

getline(cin, playerclass);

	if (playerclass == "Gift of the Warrior")
	{stam = 4, agil = 2, spell = 0, luck = 2;}
	else if (playerclass == "gift of the warrior")
	{stam = 4, agil = 2, spell = 0, luck = 2;}
	else if (playerclass == "warrior")
	{stam = 4, agil = 2, spell = 0, luck = 2;}

	else if (playerclass == "Gift of the Rogue")
	{stam = 1, agil = 4, spell = 1, luck = 4;}
	else if (playerclass == "gift of the rogue")
	{stam = 1, agil = 4, spell = 1, luck = 4;}
	else if (playerclass == "rogue")
	{stam = 1, agil = 4, spell = 1, luck = 4;}
	
	else if (playerclass == "Gift of the Mage")
	{stam = 1, agil = 1, spell = 4, luck = 3;}
	else if (playerclass == "gift of the mage")
	{stam = 1, agil = 1, spell = 4, luck = 3;}
	else if (playerclass == "mage")
	{stam = 1, agil = 1, spell = 4, luck = 3;}
	
	else if (playerclass == "Gift of the Priest")
	{stam = 1, agil = 0, spell = 5, luck = 3;}
	else if (playerclass == "gift of the priest")
	{stam = 1, agil = 0, spell = 5, luck = 3;}
	else if (playerclass == "priest")
	{stam = 1, agil = 0, spell = 5, luck = 3;}

	if (playerclass == "Gift of the Warrior")
	{cout << stam << endl;}
	else if (playerclass == "gift of the warrior")
	{cout << stam << endl;}
	else if (playerclass == "Warrior")
	{cout << stam << endl;}
	else if (playerclass == "warrior")
	{cout << stam << endl;}
	
	else if (playerclass == "Gift of Rogue")
	{cout << stam << endl;}
	else if (playerclass == "gift of the rogue")
	{cout << stam << endl;}
	else if (playerclass == "Rogue")
	{cout << stam << endl;}
	else if (playerclass == "rogue")
	{cout << stam << endl;}
	
	else if (playerclass == "Gift of the Mage")
	{cout << stam << endl;}
	else if (playerclass == "gift of the mage")
	{cout << stam << endl;}
	else if (playerclass == "Mage")
	{cout << stam << endl;}
	else if (playerclass == "mage")
	{cout << stam << endl;}
	
	else if (playerclass == "Gift of the Priest")
	{cout << stam << endl;}
	else if (playerclass == "gift of the priest")
	{cout << stam << endl;}
	else if (playerclass == "Priest")
	{cout << stam << endl;}
	else if (playerclass == "priest")
	{cout << stam << endl;}

	
}

int main ()
{
	
cout << "V0.0 \n\n" << "Welcome to Lights Deceit, a retro RPG where your intuition will be your guide. \n\n" << endl;
cout << "Once their was a mighty Hero, in a war torn land. His journey began here... \n" << endl;
cout << "You awaken feeling rested at the tavern. \n" <<  endl;
cout << "You hear chatter and laughter outside the small room, and look up to meet\nthe sunshine on your face.\n\n";
cout << "You grasp the Stone on your neck, feeling the soothing warmth of its energy, what gift would it bestow you today?\n\n\n";

		string action1 = "";
		
	PlayerClass();
	
	cout << "\nYou get up and look around, you see a chest, window, and door.\n" << endl;
		
	getline(cin, action1);

	if(action1 == "open chest")
	{cout << "You find your rusty knife and clothes and gear up to set out on an adventure.\n"; PlayerStats();}
	if(action1 == "open window")
	{cout << "You peek your head out the window and peer into the distance";}
	if(action1 == "open door")
	{cout << "You step out into the tavern, theirs grubby men, beautiful ladies, and everything in between." << endl;
	cout << "Near the barkeep a fight breaks out, a man in a tattered jacket sneaks up with a small shank to take advantage of the opportunity" << endl;
	cout << "You think to yourself, could I attack him?\n Would it even be worth it?..\n";
	cout << "I could just run and be on with my adventure, or perhaps I could wait and take the thief after hes had his way with his victims";}







	return 0;
}
Last edited on
Well it returns 0 because in line 146, you have 'return 0'. What do you want that function to return? the type of that function is 'int' and I don't think you want it to return 0, do you?
You have several other problems:

Line 50: spec is an uninitialized variable. Your comparisons at lines 56,59,62,65 are going to result in undefined behavior.

Lines 51-54: stamina, agility, intellect, luck are local variables and go out of scope at line 86.

Line 96-99: These are also local variables that go out of scope at line 170.

Line 178: Actions1() is not defined.

Thank you my man, you helped me understand, not working yet but seriously helped me out. Was up waay to long my mind was mush thats where the undefined variable came from :p But seriously thanks dude
Okay I dont get whats wrong with spec, it gets initialized with cin >> spec; then after cout outputs, works the same as all my other variables
Last edited on
closed account (E0p9LyTq)
MoreUmph wrote:
Okay I dont get whats wrong with spec, it gets initialized with cin >> spec

Your variable spec is only initialized by std::cin at line 35 after your if/else block, lines 23-33.

Move line 35 up so it executes before the if/else block.
Last edited on
Thank you, dont know how I misunderstood that. Any advice on rerunning the code to look around the room again after opening the chest?
The easiest way is a simple loop.
1
2
3
4
5
6
7
8
9
  bool chest_opened = false;

  do 
  {  //  look around the room (ideally, call a function).
      if (action1 == "open chest")
      {  chest_opened = true;
      }
  }
  while (! chest_opened);

Topic archived. No new replies allowed.