Please give a Detailed step by step instructions on how this program works. Than
ID: 667085 • Letter: P
Question
Please give a Detailed step by step instructions on how this program works.
Thanks in advance
#include <iostream>
#include <iomanip>
#include <string.h>
using namespace std;
const int SIZE = 50;
class Players
{
char name[SIZE]; //declaring instance variables or data members
int playNum;
int Points;
public:
Players() //constructor default for initialization
{
strcpy(name,"");
playNum=Points=0;
}
void readPlayerName() //reader method for input name
{
cout<<"Enter player name : ";
cin>>name;
}
void readPlayerNumber() //reader method for input number
{
while(1)//repeat if invalid player number
{
cout<<"Enter player number : ";
cin>>playNum;
if(playNum>0) break; //if positive input then break loop
cout << "Zero or negative numbers not allowed "; //otherwise error message
}
}
void readPoints() //reader method for input points
{
while(1) //repeat if invalid player points
{
cout<<"Enter points scored by the player : ";
cin>>Points; //read points
if(Points>0) break; //if positive number then break
cout << "Zero or negative numbers not allowed "; //otherwise error message
}
}
void showPlayers() //for printing player data
{
cout << setw(8) <<name;
cout << setw(8) <<playNum;
cout << setw(8) <<Points << endl;
}
int getPoints() //getter function returns points value
{return Points;}
};
int main()
{
int totalPoints=0;
const int NUM_PLAYERS = 12; //Number of Players
// Dynamically allocate the memory needed.
Players *players = new Players[NUM_PLAYERS]; //Array of structures
int index; //Loop
cout<<"Enter 12- players data : ";
for(index=0;index<12;index++) //repeat for 12 players
{
players[index].readPlayerName(); //call each methods
players[index].readPlayerNumber();
players[index].readPoints();
totalPoints+=players[index].getPoints(); //calculating totalPoints by getPoints
}
//showing players data
cout<<" Players data ";
for(index=0;index<12;index++) //repeat for 12 players
players[index].showPlayers(); //show each player data
cout<<" Total points scored : "<<totalPoints; //show total Points
return 0;
}
Explanation / Answer
This program basically stores the player name, player numbers and points scored and then it represent it in a table.
User has to enter the details of all the 12 players along with their number and points and it will basically print the details in a table without boundaries.
There are 4 functions in the class
1. void readPlayerName() - it is used to read player name
2. void readPlayerNumber() - It is used to read player number. It also checks whether the number entered is > 0 or not. If it is less than zero it again prompts the user to enter the information.
3. void readPoints() - It is used to read player points. It also checks whether the number entered is > 0 or not. If it is less than zero it again prompts the user to enter the information.
4. void showPlayers() - It is used to display the final result of the the information in the tabular form.
In the main function there is a loop which executes this information 12 times.