C++ PROGRAMMING How do I add a high score file that contains only one name and o
ID: 3823276 • Letter: C
Question
C++ PROGRAMMING
How do I add a high score file that contains only one name and one score and then compare the user's score to the high score and replace if the user's score was greater?
HERE IS THE ASSIGNMENT AND CURRENT CODE:
/*rpgv3.cpp
This program is a version 3 of the RPG.
*/
#include<fstream>
#include<iostream>
#include<string>
#include<iomanip>
#include<cctype>
using namespace std;
//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;
//Introduction
cout << "WELCOME TO MY RPG GAME!!! ";
//seed random number generator
srand(time(NULL));
while (menuChoice != 3)
{
//Display Main menu
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 >> menuChoice;
//decision structure to determine the program path
//multi-way if statement
if (menuChoice == 1)
{
//Display the rules
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;
}
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! ";
//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;
//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)
{
//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;
score1 = 500;
//prompt the user for weaponChoice
cout << " Grab a weapon quickly: ";
cin >> weaponChoice;
//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)
{
//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;
totalScore = 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 << totalScore << setw(15) << left << date << endl;
}
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. ";
}
//declare a file pointer for output
ofstream outfile;
//open file "highscore.txt" for appending
outfile.open("highscore.txt", ios::app);
outfile << date << " " << player << " " << totalScore << endl;
//close file
outfile.close();
}
return 0;
}
Explanation / Answer
Following code can be added where file writing is happening:
#include <vector>
vector<string> data;
std::ifstream infile("highscore.txt");
std::string line;
while (std::getline(infile, line))
{
data.push_back(line);
}
infile.close();
outfile.open("highscore.txt", ios:out);
for (unsigned int i=0; i < data.size(); i++)
{
if (data.at(i).find(player) != std::string::npos) {
outfile << date << " " << player << " " << totalScore << endl;
}
else {
outfile << data.at(i) << endl;
}
}
outfile.close();
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.