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.
#include<iostream>
#include<iomanip>
#include<fstream>
#include<ctime>
#include<string>
usingnamespace 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 *
// ******************
elseif (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;
constint 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;
}
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.
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).