C++ PROGRAMMING HOW DO I DECLARE AND CALL THE 3 ITALICISED BOLDED FUNCTIONS IN M
ID: 3828433 • Letter: C
Question
C++ PROGRAMMING HOW DO I DECLARE AND CALL THE 3 ITALICISED BOLDED FUNCTIONS IN MAIN()
THE FUNCTIONS HAVE BEEN DEFINED AT THE END I JUST HAVE TROUBLE WITH DECLARING THE ARRAYS AND CALLING THE FUNCTIONS.
PLEASE DON'T USE VECTOR - IT IS VERY ADVANCED FOR MY COURSE:
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<cctype>
#include <time.h> /* time */
using namespace std;
//Function Prototypes
void DisplayIntro();
int MainMenu();
void DisplayRules();
int PlayGame(string name);
int WeaponMenu(string player, int &score);
void Door3(string player, string date, int &score);
void ReadInScores(string[], int[]);
void CompareScores(string[], int[], string, int);
void ReplaceScores(string[], int[]);
//Define the main function
int main()
{
//Declare and initialize variables
int menuChoice = 0, doorChoice = 0, weaponChoice = 0, score1 = 0, score2 = 0, totalScore = 0, myRandom = 0;
string player, date;
//Call DisplayIntro function
DisplayIntro();
//seed random number generator
srand(time(NULL));
while (menuChoice != 3)
{
//Display Main menu function
MainMenu();
menuChoice = MainMenu();
//decision structure to determine the program path
//multi-way if statement
if (menuChoice == 1)
{
//Call Display the rules
DisplayRules();
}
else if (menuChoice == 2)
{
//Play the game
//Prompt the user for player's name
cout << " Player, enter your name here: ";
cin >> player;
//Prompt the user for the date (define date format)
cout << " " << player << ", enter the date here (MM/DD/YYYY): ";
cin >> date;
cout << endl; //skip a line
//Intro to Menu Option 2
cout << player << ", You just woke up from a long sleep in the cryogenic tubes... ";
cout << "Are you ready to revenge your spouse's death and find your son? ";
cout << "Where do you go now, " << player << "? You must be careful! The Vault-Corp staff may catch you and freeze you back to sleep! ";
cout << "You have 3 doors in front of you. Watch out afer openning one of the doors! ";
doorChoice = PlayGame(player);
//decision structure to determine the program path based on doorChoice
//multi-way if statement
if (doorChoice == 1)
{
//Display winning door choice
cout << " You open the Left door and peak inside.It looks like a storage closet with a grate in the back. ";
cout << "It appears safe and you continue in... You are on the right track," << " " << player << " " << ", this door brings you to an underground tunnel! ";
cout << " You find yourself outside of the Vault in the Wasteland. ";
cout << "You are now free," << " " << player << " " << "! It's time to go find your son! ";
myRandom = rand() % 1200 + 1;
totalScore = myRandom + 1000;
//display formatted chart for the player's name, score, and the date for door 1
//set columns to a width of 15 characters each
cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " ";
cout << "-------------------------------------------------------------------------- ";
cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;
}
else if (doorChoice == 2)
{
weaponChoice = WeaponMenu(player,score1);
//decision structure to determine the program path based on weaponChoice
//multi-way if statement
if (weaponChoice == 1)
{
cout << " This pistol helps you react quickly but the Vault-Corp staffers have you outnumbered! ";
cout << player << ", Your skills help you liquidize the Vault-Corps staffers, but barely...";
myRandom = rand() % 500 + 1;
totalScore = score1 + myRandom;
//display formatted chart for the player's name, score, and the date for door 2/weapon 1
//set columns to a width of 15 characters each
cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " ";
cout << "-------------------------------------------------------------------------- ";
cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;
}
else if (weaponChoice == 2)
{
cout << player << ", Are you kidding me? How are you planning to smash a dozen of Vault-Corp staffers with a single hammer?! ";
cout << "You swing at one of them only to miss and hit the floor ";
cout << "You just got caught by Vault-Corp once again. GAME OVER!";
score2 = 200;
totalScore = score1 + score2;
//display formatted chart for the player's name, score, and the date for door 2/weapon 2
//set columns to a width of 15 characters each
cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " ";
cout << "-------------------------------------------------------------------------- ";
cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;
}
else if (weaponChoice == 3)
{
cout << " Good job, " << player << ", This granate helped you kill all the Vault-Corp staffers at once! ";
cout << "You are now are a free man! Go find your son. ";
score2 = 600;
totalScore = score1 + score2;
//display formatted chart for the player's name, score, and the date for door 2 / weapon 3
//set columns to a width of 15 characters each
cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " ";
cout << "-------------------------------------------------------------------------- ";
cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;
}
else
{
cout << "Sorry, " << player << ", You are not quick enough! ";
score2 = 150;
totalScore = score1 + score2;
//display formatted chart for the player's name, score, and the date for wrong door
//set columns to a width of 15 characters each
cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " ";
cout << "-------------------------------------------------------------------------- ";
cout << setw(15) << left << player << setw(15) << left << totalScore << setw(15) << left << date << endl;
}
}
else if (doorChoice == 3)
{
Door3(player, date,totalScore);
}
else
{
//Display invalid door choice
cout << "Sorry," << player << ", the roof is not an option! You don't have a ladder! You must choose 1 of the three doors! ";
}
}
else if (menuChoice == 3)
{
//Exit
cout << " Are you giving up? Good bye! ";
}
else
{
//Display invalid choice.
cout << "INVALID. You must choose 1 - 3 from the menu above. ";
}
ReadInScores( name[], int score[]);
CompareScores(string name[], int score[], string name1, int score1);
ReplaceScores(string name[], int score[])
}
return 0;
}
//Define functions
void DisplayIntro()
{
//Introduction
cout << "WELCOME TO MY RPG GAME! ";
}
int MainMenu()
{
int mc;
cout << " Please choose from the following Main menu:" << endl;
cout << "1) See Rules" << endl;
cout << "2) Play Game" << endl;
cout << "3) Exit ";
//Prompt the user for a choice from the main menu
cout << " Enter your main menu choice here: ";
cin >> mc;
return mc;
}
void DisplayRules()
{
cout << " You have chosen to see the rules. " << endl;
cout << "This game awards points to the player based on decisions throughout the game." << endl;
cout << "At the end of the game, the points are displayed and saved to a highscore text file to keep track of the best player. " << endl;
}
int PlayGame(string name)
{
int doorChoice;
cout << name << ", You just woke up from a long sleep in the cryogenic tubes... ";
cout << "Are you ready to revenge your spouse's death and find your son? ";
cout << "Where do you go now, " << name << "? You must be careful! The Vault-Corp staff may catch you and freeze you back to sleep! ";
cout << "You have 3 doors in front of you. Watch out afer openning one of the doors! ";
//Display door selection menu
cout << " 1) Left Door 2) Middle Door 3) Rright Door ";
//prompt for doorChoice
cout << "Which door do you choose?: ";
cin >> doorChoice;
return doorChoice;
}
int WeaponMenu(string player, int &score)
{
int weaponChoice;
//Display action door and weaponChoice menu
cout << "You Open the door slowly to a dark room. You walk into the room and the door shuts behind you ";
cout << "The lights quickly turn on and you find yourself trapped! ";
cout << "Watch out, " << player << "! There is a dozen of Vault-Corp staffers in front of you! Look!, There are three weapons on the wall. ";
cout << "Grab one quickly: ";
cout << " 1.) SILENT PISTOL - this weapon helps you react quickly!" << endl;
cout << "2.) SLEDGE HAMMER - a construction tool with a large, flat, metal head which allows you to apply more force over a wide area." << endl;
cout << "3.) GERNADES - a handfull of small bomb typically thrown by hand." << endl;
score += 500;
//prompt the user for weaponChoice
cout << " Grab a weapon quickly: ";
cin >> weaponChoice;
return weaponChoice;
}
void Door3(string player, string date, int &score)
{
//Display losing door choice
cout << " The right door is not always the right choice, " << player << "You just got shot by one of the Vault-Corp Staffers! " << endl;
cout << "GAME OVER!" << endl;
score += 100;
//display formatted chart for the player's name, score, and the date for Door 3
//set columns to a width of 15 characters each
cout << setw(15) << left << " Player Name" << setw(15) << left << "High Score" << setw(15) << left << "Date" << " ";
cout << "-------------------------------------------------------------------------- ";
cout << setw(15) << left << player << setw(15) << left << score << setw(15) << left << date << endl;
}
void ReadInScores(string name[], int score[])
{
//declare the file pointer for input
ifstream infile;
//open the file
infile.open("highscore.txt", ios::in);
//read in the first three lines of the file
infile >> name[0] >> score[0];
infile >> name[1] >> score[1];
infile >> name[2] >> score[2];
}
void CompareScores(string name[], int score[], string name1, int score1)
{
if (score1 > score[0])
{
name[2] = name[1];
score[2] = score[1];
name[1] = name[0];
score[1] = score[0];
name[0] = name1;
score[0] = score1;
}
else if (score1 > score[1])
{
name[2] = name[1];
score[2] = score[1];
name[1] = name1;
score[1] = score1;
}
else if (score1 > score[2])
{
name[2] = name1;
score[2] = score1;
}
}
void ReplaceScores(string name[], int score[])
{
//file pointer for output
ofstream outfile;
//open the file for appending
outfile.open("highscore.txt", ios::app);
outfile << name[0] << " " << score[0] << endl;
outfile << name[1] << " " << score[1] << endl;
outfile << name[2] << " " << score[2] << endl;
}
Explanation / Answer
Just add these two lines
string name[3]; // string array of size 3
int score[3]; // integer array of size 3
above the function calls in main()
ReadInScores(name, score);
CompareScores(name, score, name1 , score1); //name1 will be undefined here
ReplaceScores(name, score);
**since in CompareScores(), you are passing - (name1 and score1), I think you want to do this -
CompareScores(name, score, player , totalScore);
Please check and let me know.
Thanks
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.