Segmentation Fault In a function

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
#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
Well since your code shouldn't even compile I wonder how you got a Segmentation Fault.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
main.cpp|24|error: ISO C++ forbids zero-size array ‘attackDamage’ [-Wpedantic]|
main.cpp|25|error: ISO C++ forbids zero-size array ‘attackName’ [-Wpedantic]|
main.cpp||In constructor ‘Player::Player()’:|
main.cpp|33|warning: ‘Player::name’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|33|warning: ‘Player::score’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|33|warning: ‘Player::health’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|33|warning: ‘Player::randomNum’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|33|warning: ‘Player::SIZE’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|33|warning: ‘Player::playerChoice’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|33|warning: ‘Player::highScore’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|33|warning: ‘Player::totalScore’ should be initialized in the member initialization list [-Weffc++]|
main.cpp|43|warning: statement has no effect [-Wunused-value]|
main.cpp|48|warning: statement has no effect [-Wunused-value]|
main.cpp||In function ‘int FirstPhase(int&)’:|
main.cpp|198|warning: zero as null pointer constant [-Wzero-as-null-pointer-constant]|
main.cpp|198|error: ‘srand’ was not declared in this scope|
main.cpp|201|error: ‘rand’ was not declared in this scope|
||=== Build finished: 4 errors, 11 warnings (0 minutes, 1 seconds) ===|


Array sizes in C++ must be compile time constants, and you can't have an "empty" or zero sized array.

Sorry about that, I went back and fixed the compiling issue (or at least it compiles for the linux terminal).
1
2
3
4
5
6
7
8
SIZE = 3;
			playerChoice = 0;
			
		
			attackDamage[SIZE];            // <----------------- Doesn't this create the size for the array?
			attackDamage[0] = 100;
			attackDamage[1] = 200;
			attackDamage[2] = 0;   // Tail wag gives no damage.		 




So then how would I initialize the size of attackName[] and attackDamage[] ?
Last edited on
Nevermind, I see what you're saying. That did the trick, I just put a 3 in the brackets for each one of the variables in the struct (not in the constructor).


Thanks for your help, very much appreciated!
Topic archived. No new replies allowed.