To Rapunzel: I copied and pasted the incorrect coment in to the rating. What I m
ID: 3622608 • Letter: T
Question
To Rapunzel: I copied and pasted the incorrect coment in to the rating.
What I ment to say was:
That is one of the best explanations I have gotten from Cramster. I had problems with it initially because I forgot to add #include. Obviously, that was my fault. People like you make Cramster worth the expense. I finally understand this chapter. I wish you the best. The site wouldn't let me update the comment after I posted it. I assure you, no disrespect was meant. I was writing my responses in word (spell check helps you not look special) and copying them over. I apparently copied the wrong one. Thank you very much for your time and effort.
Here is the Programming Challenge:
Write a program that stores the following data about a soccer player in a structure:
Player's Name
Player's Number
Points Scored by Player
The Program should keep an array of 12 of these structures. Each element is for a different player on a team. When the program runs it should ask the user to enter the data for each player. It should then show a table that lists each player's number, name, and points scored. The program should also calculate and display the total points earned by the team. The number and name of the player who has earned the most points shoud also be displayed.
Input Validation: Do not accept negative values for players' numbers or points scored.
This is what was given as a solution in cramster. If you enter a first and last name it causes an infinite loop, it does not display the correct information, and does not have the input validation. Please rework this so that it includes clear explinations for the steps and values with meaningful names (no letters please). I want to understand this which is why I'm asking for all of the detail.
The following solution was given by Cramster.
Please rework the solution so that the following are included.
The problem specifies that input validation is required so that NO negative numbers can be entered.
It needs to also show a table that lists each player’s number, name, and points scored.
If you enter a first and last name it causes an infinite loop. The solution needs to accept and display first and last names. I think this requires cin.getline but I'm not sure how to do that in this program.
Please use meaningful variable names and not letters. The letters i and j are used in this and I'm not completely clear how it's being indexed. I am teaching myself so this is my only instructional resource.
Thaks for your time.
#include
#include
using namespace std;
// Structure Decloration
struct soccerPlayer
{string name;
int number;
int points;};
void main()
{
//Variable Declorations
soccerPlayer players[12];
int totalPoints = 0;
// Request information for each player
cout << "Enter information for each player:" << endl;
for (int i=0; i<12; i++)
{
do
{
// Do while loop for input validation
// While loop repeats untill non negative input
cout << "Enter Player Name:";
cin >> players[i].name;
cout << "Enter Player Number:";
cin >> players[i].number;
cout << "Enter Player Points:";
cin >> players[i].points;
}
while (players[i].number < 0 || players[i].points <0);
}
// Calculating total points for team
for (int i=0; i<12; i++)
{
totalPoints = totalPoints=players[i].points;
}
cout << "total points in a Team:" << totalPoints << endl;
int highPoints, j=0;
highPoints = players[0].points;
for (int i=1; i<12; i++)
{
if (players[i].points > highPoints)
{
highPoints = players[i].points;
j=i;
}
cout << "Top points player:" << endl;
cout << "Player:" << players [j].name << endl;
cout << "Player Number:" << players[j].number << endl;
cout << "Player Points:" << highPoints << endl;
system("pause");
}
}
Explanation / Answer
please rate - thanks
i is the index of the array you are up to. The index is traditionally noted as i, j or k etc... back from the days of FORTRAN
#include <iostream>
#include <iomanip>
using namespace std;
// Structure Decloration
struct soccerPlayer
{string name;
int number;
int points;
};
int main()
{
//Variable Declorations
soccerPlayer players[12];
int totalPoints = 0,i,highpoints;
// Request information for each player
cout << "Enter information for each player:" << endl;
for (i=0; i<12; i++)
{cout<<"Player: "<<i+1<<endl;
// Do while loop for input validation
// While loop repeats untill non negative input
cout << "Enter Player Name:";
getline(cin,players[i].name);
cout << "Enter Player Number:";
cin >> players[i].number;
while(players[i].number<0)
{cout<<"must be >=0 ";
cout << "Enter Player Number:";
cin >> players[i].number;
}
cout << "Enter Player Points:";
cin >> players[i].points;
while(players[i].points<0)
{cout<<"must be >=0 ";
cout << "Enter Player Points:";
cin >> players[i].points;
}
cin.ignore(80,' ');
}
cout<<" Team Statistics ";
cout<<"Player points Name number scored ";
for(i=0;i<12;i++)
cout<<setw(15)<<left<<players[i].name<<" "<<players[i].number
<<" "<<players[i].points<<endl;
cout<<endl;
// Calculating total points for team
for (int i=0; i<12; i++)
{
totalPoints += players[i].points;
}
cout << "total points scored by the Team:" << totalPoints << endl<<endl;
highpoints=0;
for (int i=1; i<12; i++)
if (players[i].points > players[highpoints].points)
highpoints = i;
cout << "Top points player:" << endl;
cout << "Name: " << players [highpoints].name << endl;
cout << "Number: " << players[highpoints].number << endl;
cout << "Points: " << players[highpoints].points << endl;
system("pause");
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.