Question

Need done in C++ Can not get my code to properly display output. Instructions below. The...

Need done in C++

Can not get my code to properly display output. Instructions below. The code compiles but output is very off.

Write a program that scores 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 should also be displayed.

#include <iostream>
#include <iomanip>
using namespace std;

// Declaration of the Player structure

struct Player
{
char name[45]; // Player's name
int number; // Player's number
int points; // Points scored by the player
};

const int numPlayers = 12; // The number of players

// Function prototypes
void getPlayerInfo(Player &);
void showInfo(Player);
int getTotalPoints(Player[], int);
void showHighest(Player[], int);

//***********************************************
// Function main *
//***********************************************

int main()
{
Player team[numPlayers];
int index;

for (index = 0; index < 12; index++)
{
cout << "\nPLAYER #" << (index + 1) << "\n";
cout << "---------\n";
getPlayerInfo(team[index]);
cin.get();
}

cout.width(20);
cout.setf(ios::left);
cout << "\nNAME";
cout.width(10);
cout << "NUMBER";
cout.width(10);
cout << "POINTS SCORED\n";
for (index = 0; index < 12; index++)
showInfo(team[index]);
cout << "TOTAL POINTS: " << getTotalPoints(team, numPlayers) << endl;
showHighest(team, numPlayers);
}

//***********************************************
// Function getPlayer *
// This function accepts a reference to a Player*
// structure variable. The user is asked to *
// enter the player's name, number, and the *
// number of points scored. This data is stored *
// in the reference parameter. *
//***********************************************

void getPlayerInfo(Player &p)
{
while (p.points < 0)
{
cout << p.points << endl;
cout << "Player name: ";
cin.getline(p.name, 45);
cout << "Player's number: ";
cin >> p.number;
cout << "Points scored: ";
cin >> p.points;
}
}

//***********************************************
// Function showInfo *
// This function displays the data in the Player*
// structure variable passed into the parameter.*
//***********************************************

void showInfo(Player p)
{
cout << setw(20) << p.name;
cout << setw(10) << p.number;
cout << setw(10) << p.points << endl;
}

//***********************************************
// Function getTotalPoints *
// This function accepts an array of Player *
// structure variables as its argument. The *
// function calciulates and returns the total *
// of all the players points in the array. *
//***********************************************

int getTotalPoints(Player p[], int size)
{
int total = 0;
for (int index = 0; index < size; index++)
total += p[index].points;
return total;
}

//***********************************************
// Function showHighest *
// This function accepts an array of Player *
// structure variables. It displays the name *
// of the player who scored the most points. *
//***********************************************

void showHighest(Player p[], int size)
{
int highest = 0, highPoints = p[0].points;

for (int index = 1; index < size; index++)
{
if (p[index].points > highPoints)
{
highest = index;
highPoints = p[index].points;
}
}
cout << "The player who scored the most points is: ";
cout << p[highest].name << endl;
}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <iostream>

#include <iomanip>

#include <limits>

using namespace std;

// Declaration of the Player structure

struct Player

{

char name[45]; // Player's name

int number; // Player's number

int points; // Points scored by the player

};

const int numPlayers = 12; // The number of players

// Function prototypes

void getPlayerInfo(Player &);

void showInfo(Player);

int getTotalPoints(Player[], int);

void showHighest(Player[], int);

//***********************************************

// Function main *

//***********************************************

int main()

{

Player team[numPlayers];

int index;

for (index = 0; index < numPlayers; index++)

{

cout << "\nPLAYER #" << (index + 1) << "\n";

cout << "---------\n";

getPlayerInfo(team[index]);

cin.get();

}

cout.width(20);

cout.setf(ios::left);

cout << "\nNAME";

cout.width(10);

cout << "NUMBER";

cout.width(10);

cout << "POINTS SCORED\n";

for (index = 0; index < 12; index++)

showInfo(team[index]);

cout << "TOTAL POINTS: " << getTotalPoints(team, numPlayers) << endl;

showHighest(team, numPlayers);

}

//***********************************************

// Function getPlayer *

// This function accepts a reference to a Player*

// structure variable. The user is asked to *

// enter the player's name, number, and the *

// number of points scored. This data is stored *

// in the reference parameter. *

//***********************************************

void getPlayerInfo(Player &p)

{

cout << "Player name: ";

cin.getline(p.name, 45);

cout << "Player's number: ";

// check if input is valid or not

while (!(cin >> p.number))

{

// flush the cin data

cin.clear();

cin.ignore(numeric_limits<streamsize>::max(), '\n');

cout << "Invalid entry, Try again : ";

}

do

{

cout << "Points scored: ";

cin >> p.points;

} while (p.points < 0);

}

//***********************************************

// Function showInfo *

// This function displays the data in the Player*

// structure variable passed into the parameter.*

//***********************************************

void showInfo(Player p)

{

cout << setw(20) << p.name;

cout << setw(10) << p.number;

cout << setw(10) << p.points << endl;

}

//***********************************************

// Function getTotalPoints *

// This function accepts an array of Player *

// structure variables as its argument. The *

// function calciulates and returns the total *

// of all the players points in the array. *

//***********************************************

int getTotalPoints(Player p[], int size)

{

int total = 0;

for (int index = 0; index < size; index++)

total += p[index].points;

return total;

}

//***********************************************

// Function showHighest *

// This function accepts an array of Player *

// structure variables. It displays the name *

// of the player who scored the most points. *

//***********************************************

void showHighest(Player p[], int size)

{

int highest = 0, highPoints = p[0].points;

for (int index = 1; index < size; index++)

{

if (p[index].points > highPoints)

{

highest = index;

highPoints = p[index].points;

}

}

cout << "The player who scored the most points is: ";

cout << p[highest].name << endl;

}

Output:

Add a comment
Know the answer?
Add Answer to:
Need done in C++ Can not get my code to properly display output. Instructions below. The...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • I need to update this C++ code according to these instructions. The team name should be...

    I need to update this C++ code according to these instructions. The team name should be "Scooterbacks". I appreciate any help! Here is my code: #include <iostream> #include <string> #include <fstream> using namespace std; void menu(); int loadFile(string file,string names[],int jNo[],string pos[],int scores[]); string lowestScorer(string names[],int scores[],int size); string highestScorer(string names[],int scores[],int size); void searchByName(string names[],int jNo[],string pos[],int scores[],int size); int totalPoints(int scores[],int size); void sortByName(string names[],int jNo[],string pos[],int scores[],int size); void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size); void writeToFile(string...

  • I want to change this code and need help. I want the code to not use...

    I want to change this code and need help. I want the code to not use parallel arrays, but instead use one array of struct containing the data elements, String for first name, String for last name,Array of integers for five (5) test scores, Character for grade. If you have any idea help would be great. #include #include #include #include using namespace std; const int NUMBER_OF_ROWS = 10; //number of students const int NUMBER_OF_COLUMNS = 5; //number of scores void...

  • using the source code at the bottom of this page, use the following instructions to make...

    using the source code at the bottom of this page, use the following instructions to make the appropriate modifications to the source code. Serendipity Booksellers Software Development Project— Part 7: A Problem-Solving Exercise For this chapter’s assignment, you are to add a series of arrays to the program. For the time being, these arrays will be used to hold the data in the inventory database. The functions that allow the user to add, change, and delete books in the store’s...

  • What's wrong with my code? : I'm trying to use recursive functions to display and count...

    What's wrong with my code? : I'm trying to use recursive functions to display and count all the even numbers of an array. However, I can't seem get the program output the number of even integers . Here's the output I want: Here are the 5 even numbers: // Luckily, however, my code does output all the even numbers. But, I also want the program to tell me how may even integers there are. 2 8 14 18 22 MY...

  • Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>...

    Convert the TreeArray C++ source code(posted below) into a BinaryTree, using this TreeNode definition: class TreeNode<T>       T data       TreeNode<T> left       TreeNode<T> right Since this TreeNode is a generic Template, use any data file we've used this quarter to store the data in the BinaryTree. To do this will likely require writing a compare function or operator. Hint: Think LEFT if the index is calculate (2n+1) and RIGHT if index is (2n+2). Source code: #include<iostream> using namespace std;...

  • Follow the instructions for starting C++ and viewing the Intermediate23.cpp file, which is contained in either...

    Follow the instructions for starting C++ and viewing the Intermediate23.cpp file, which is contained in either the Cpp8IChap11\Intermediate23 Project folder or the Cpp8\Chap11 folder. (Depending on your C++ development tool, you may need to open the project/solution file first.) The program uses an array to store the amount of money a game show contestant won in each of five days. The program should display the total amount won and the average daily amount won. It should also display the day...

  • howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE]...

    howthe   output   of   the   following   4   program segments (a)    const int SIZE=8;    int values[SIZE] = {10, 10, 14, 16, 6, 25, 5, 8};    int index;    index=0;    res = values[index];    for (int j=1; j<SIZE; j++)    {        if (values[j] > res)        {            res = values[j];            index = j;        cout << index << res << endl;        }    }    cout <<...

  • C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class...

    C++ Please Provide .cpp Overview For this assignment, implement and use the methods for a class called Player that represents a player in the National Hockey League. Player class Use the following class definition: class Player { public: Player(); Player( const char [], int, int, int ); void printPlayer(); void setName( const char [] ); void setNumber( int ); void changeGoals( int ); void changeAssists( int ); int getNumber(); int getGoals(); int getAssists(); private: char name[50]; int number; int goals;...

  • The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib>...

    The following is a sample inventory in C++, i want to ask the user to input a item number for removing from inventory. //CPP #include <iostream> #include <fstream> #include <cstdlib> #include <iomanip> #define MAX 1000 using namespace std; //Function to Add a new inventory item to the data into the array in memory void addItem(string desc[],string idNum[], float prices[], int qty[],int &num) { cout<<"Enter the names:"; cin>>desc[num]; cout<<"Enter the item number:"; cin>>idNum[num]; cout<<"Enter the price of item:"; cin>>prices[num]; cout<<"Enter the...

  • Please help fix my code C++, I get 2 errors when running. The code should be...

    Please help fix my code C++, I get 2 errors when running. The code should be able to open this file: labdata.txt Pallet PAG PAG45982IB 737 4978 OAK Container AYF AYF23409AA 737 2209 LAS Container AAA AAA89023DL 737 5932 DFW Here is my code: #include <iostream> #include <string> #include <fstream> #include <vector> #include <cstdlib> #include <iomanip> using namespace std; const int MAXLOAD737 = 46000; const int MAXLOAD767 = 116000; class Cargo { protected: string uldtype; string abbreviation; string uldid; int...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT