How do I get this function to work? (Intro to C++)

I'm creating a text based game for my project in Intro to C++. This game has 5 functions and I can't get the function, FirstPhase, to work. It's supposed to take in the score value from main by reference, run through the first level of the game and return the score back to main as an int into score.

Here's some of my code:


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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
#include<iostream>
#include<iomanip>
#include<fstream>
#include<ctime>
#include<string>

using namespace std;


	// ***********************
	// * Struct Declarations *
	// ***********************

	 // Declare a struct called: Player

	struct Player 
	{
		string name;
		int score;
		int health;
		int randomNum;
		int SIZE;
		int playerChoice; 
		int attackDamage[];
		string attackName[];
		int highScore;
		int totalScore;

		// ***************
		// * Constructor       *
		// * *************
	
		Player ()
		{
			score = 0;
			health = 1200;
			randomNum = 0;

			SIZE = 3;
			playerChoice = 0;
			
		
			attackDamage[SIZE];
			attackDamage[0] = 100;
			attackDamage[1] = 200;
			attackDamage[2] = 0;   // Tail wag gives no damage.			

			attackName[SIZE];
			attackName[0] = "Scratch";
			attackName[1] = "Bite";
			attackName[2] = "Tail Wag";
			
			highScore = 0;
			totalScore = 0;
		}
	};

         // *********************
	// * Function Protypes           *
	// *********************
	
	 // 1
	// Data Type: void
	// Parameters: NONE
	// Purpose: Displays the rules & back story to the game 
	void DisplayRules();
		

	 // 2
	// Data Type: int
	// Parameters: 1 int --> To pass the score into main by reference
	// Purpose: Goes through the laser phase of the game
	int FirstPhase(int& );



// *****************
	// * MAIN FUNCTION *
	// *****************
					
	int main ()
	{
		// Declare variables in main
		int menuChoice = 0, score = 0, totalScore = 0;

	
	cout << "\n\n\n";




	//Begin Outer Loop (loops as long as player doesn't choose 3 to exit)
		do
		{
	
		//Reset score
		score= 0;
		totalScore=0;

		//Reset MenuChoice
		//menuChoice= 0;


		// Display the menu choices
			cout << "1.)  See Rules\n"
			     << "2.)  Play Game\n"
			     << "3.)  Quit\n\n";
			
			
		// Prompt for menu choice
		cout << "Enter Choice: ";
		cin >> menuChoice;
		cout << "\n\n";

		// Input validation: User input must be a choice from 1-3
				if (menuChoice < 1 || menuChoice > 3)
					{
				cout << "Invalid. Please enter a number 1-3 in accordance to the menu choices\n\n\n";
					}
	
		// ******************
		// * MenuChoice = 1 *
		// ******************
		// Displays the rules & back story to the game
			if (menuChoice == 1)
			{
			DisplayRules();
			}

	
		// ******************
		// * MenuChoice == 2 *
		// ******************
		
		else if (menuChoice == 2)
		{
		
		score = FirstPhase(score);


		}
	
		
	}
	while(menuChoice !=3);
		
		return 0;
}


// ************************
	// * Function Definitions *
	// ************************
	
	 // 1
	// *************************************
	// * Function Definition: DisplayRules *
	// *************************************
	// Purpose: Displays the rules & back story to the game 
	void DisplayRules()
	{

            cout << " Rules\n\n\n";

	}



	// 2
	// ***********************************
	// * Function Definition: FirstPhase                  *
	// ***********************************
	// Purpose: Goes through the laser phase of the game
	int FirstPhase(int& score)                                 //<----------------- Function with Problem
	{


	// Declare local variables
	Player user;
	const int laserNum= 3, addThousand = 1000, subHundred = 100;

	string currentScore = "\n\nScore: ";

	string laserFire = "Lasers have been fired at you! Will you:\n(1) Dodge Left\n(2) Dodge Right\n(3) Duck Down\n\nEnter Choice: ";

	string dodgeCorrect = "\n\nNice job! Who were they aiming at any ways? +1000 points\n\n";

	string dodgeWrong = "\n\nDang, they got you but don't give up! -100 points\n\n";




	//Inner loop to prompt player 3 times
		for (int i = 0; i< laserNum; i++)
			{
		
			// Seed random number generator
			srand(time(NULL));
			
		 	// Generate a random number (randomNum will be either a 1, 2 or 3)
			user.randomNum = rand()%3 + 1;
			
				do
				{

				
					//Display current score (currentScore the string) & (score the int)
					cout << currentScore << user.score << endl;
						
						
						//Display that lasers were fired at the player (laserFire)
					cout << laserFire;
					
						//prompt for menuChoice 1 or 2
					cin >> user.playerChoice;
					
						//input validation
					if (user.playerChoice < 1 || user.playerChoice > 3)
					{
						cout<<"\n\nInvalid. Please select either a 1, 2 or a 3\n\n";
					}
				}
				while (user.playerChoice < 1 || user.playerChoice > 3);

		
		
				//If the player guessed correctly string variable: dodgeCorrect
				if (user.playerChoice == user.randomNum)
				{
				cout << dodgeCorrect;

					//Add 1000 points to their score
				user.score+= addThousand;
				}
				
				//If the player guessed incorrectly display string variable: dodgeWrong
				if (user.playerChoice != user.randomNum)
				{
				cout << dodgeWrong;
				
					//Deduct 100 points from their score
				user.score-= subHundred;
				}
			
	//End for loop
		}
	
	// Set score equal to user.score
	score = user.score;

	return score;
	}




Segmentation Fault



Any ideas would be kindly appreciated.
Last edited on
I had to change a few things to even get it to compile for me, but then it ran without a segmentation fault.

Try moving main and FirstPhase outside of your struct. The }; on line 185 needs to be moved way up, before your function prototype.

That at least got the game to compile/run for me, although it doesn't appear to work correctly in all cases for me. (I can sometimes get the right answer, but it tells me I got it wrong... even though it has to be the right choice, because that was my 3rd try, and it just goes back to the menu).
@tscott8706

Hey, thanks for the reply. Sorry about that. I should of been more careful when I pasted the code. The bracket was up there but I accidentally pasted things where they didn't belong. I went back and altered the code and it compiles but I still get a Segmentation Fault.


You're right though it worked. For some reason tho, it won't work when I have this other function to display the rules. I'm not sure what it could be.
I'm not sure what you are saying. You say it works, but you still get a segmentation fault.

Does it only segmentation fault when you have "the other function" ?

If that's the case, then we will need to see your other function in order to help you.

Never mind, someone pointed out the problem. What had happened was that I took out a lot code that I thought was unnecessary before I posted it. This included a few arrays in the Player structure that were left with empty brackets. Once I put in a number, it worked like a charm.
Last edited on
Topic archived. No new replies allowed.