Question: Please help me modify the main class so I can get input datas ( the top ten scores), from the user. I don't know how
Please help me modify the main class so I can get input datas ( the top ten scores), from the user. I don't know how to call add function. The program was working, I only need to call the functions to the main class but I don't know how to call the " void add(const GameEntry& e); " to add players and player's score in the scorelist.
/**P-3.3 Write a class that maintains the top 10 scores for a game application, implementing the add and remove functions of Section 3.1.1, but use a singly linked list instead of an array. **/
#include
using namespace std;
class GameEntry // define class { private: string playerName; // variable to store player name int playerScore; // variable l to store player score GameEntry*next; // pointer to next node in the list friend class ScoresList; // friend class
public: void setData(const string &name, int score ) //The function setData assigns value to the playerName and playerScore. { // function to assign values to the members playerName = name; // assign values to playerName and player score playerScore = score; } void displays () { cout<<"Player's Name : "<< playerName< class ScoresList // define class scoresList { private: // declare private members GameEntry*headNode; // pointer to store the starting of list int scoreCounter; // variable to store the count of score public: ScoresList(); //constructor to assign maxEnt void add(const GameEntry& e); // function to add a player and score void remove(int i); // function to remove entry at index i-1 void removeLast(); // function to remove the last element void printEntries() ; // function to print the elements in the list bool empty() const; // function to check if the list is empty or not }; ScoresList::ScoresList():headNode(NULL), scoreCounter(0) // Constructor to assign values { } bool ScoresList::empty() const // function to check if list is empty { return( headNode == NULL); // return if headnode is null } void ScoresList::printEntries() // The function printEntries iterates entire list using pointer tempObj and displays the elements in the list. // function to print entries in the list { GameEntry *tempObj; // create object of GameEntry tempObj = headNode; //1 assign tempObj value of headNode while(tempObj != NULL) // loop to work till all nodes are traversed { cout << tempObj-> playerName << "\t" << tempObj-> playerScore << endl; // display the element in the node tempObj = tempObj->next; // move to next node } } void ScoresList::add(const GameEntry& e) // define function add { GameEntry*buffNode = new GameEntry; // create a buffer node for gameEntry buffNode->playerName = e.playerName; // assign values to playerName and playerscore buffNode->playerScore = e.playerScore; buffNode->next = NULL; // the next pointer is assigned NULL if(headNode == NULL) //The check to see if the list is empty or not // check if there is no node in the list { headNode = buffNode; //create the first node scoreCounter++; // increase counter by 1 } else // if there is atleast 1 node in the list { if(e.playerScore > headNode->playerScore) //If the list is not empty. then check to see if the element to be inserted is greater than the largest element in the list //check if the new score is more than the highest score in the list { buffNode->next = headNode; // add node at the beginning of the list headNode = buffNode; scoreCounter++; // increase counter by 1 } else // if the score to be added is not the highest { GameEntry *tempNode; //create a temporary node tempNode = headNode; // allocate it to the headnode while(tempNode->next != NULL) // loop to work till last node is not checked. { if(tempNode->playerScore > buffNode-> playerScore && buffNode-> playerScore >= tempNode->next->playerScore) // check if the node can be inserted in between { buffNode->next = tempNode->next; // move the addresses to insert the node tempNode->next = buffNode; scoreCounter++; // increase the counter by 1 } tempNode = tempNode->next; //move to the next node } if(tempNode->playerScore >= buffNode-> playerScore) // if the least score in the list is more than the score to be entered { cout << " Sorry! the Score is insufficient to make it to the top 10."; // display error message } else // if the least score in the list is less than the score to be added { tempNode -> playerName = e.playerName; // add new player with the least score tempNode -> playerScore = e.playerScore; scoreCounter++; // increase counter by 1 } } } // check if the list has more than 10 players in the list if(scoreCounter >= 11) // remove the last removeLast(); } void ScoresList::removeLast() // The removeLast function removes the player details at last position in the list // function to remove the last node { // pointer of Game Entry GameEntry*tempNode; // assign value of headnode tempNode = headNode; // while tempnode is not the second last node while(tempNode->next->next != NULL) // move to the last node tempNode = tempNode->next; // second last node points to null tempNode->next = NULL; // reduce counter by 1 scoreCounter--; } int main() { GameEntry GE; ScoresList sl; string playerName; // variable to store player name int playerScore; cout<<"Enter Player's Name : "; //get input here cin>>playerName; cout<<"Enter Player's Score : "; //get input here cin>>playerScore; GE.displays(); sl.printEntries(); cout<<"Enter the postion : "; //get input here sl.printEntries(); return 0; }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
