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;
}
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).
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.
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.