Question

I need this done in C++ Can someone help me get my code from crashing. The...

I need this done in C++

Can someone help me get my code from crashing. The code compiles but it can be easily crashed with user input. The rubric and code below

Instructions:

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.

no break statements, infinite loops, or go-to labels

#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

The problem you were facing was probably due to the while loop you inserted in the 'getPlayersInfo' function. The problem is solved. Also I have inserted you extra features missing in you program.

  • If more than one Player have the same number of points then it was not reflected in you program, but i have inserted a function 'showHighestModified' which shows if more than one player have the same points.
  • 2 players can not have have same number, if there was no such constraint in your program, which is inserted in the main program.

The modified code:

#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);
void showHighestModified(Player[], int);
//***********************************************
// Function main *
//***********************************************

int main()
{
struct Player team[numPlayers];
int index;
int index_temporary;//Newly inserted index for checking if more than one players have the same jersy number
for (index = 0; index < 12; index++)
{
cout << "\nPLAYER #" << (index + 1) << "\n";
cout << "---------\n";
getPlayerInfo(team[index]);
for(index_temporary=0 ; index_temporary<index ; index_temporary++)//this for loops checks if more than one players    //have same jersy number
{
if(team[index].number==team[index_temporary].number)
{
cout<<"2 players number can not be same, Please reenter!";//if yes, it asks to reenter
          index--;
          break;
}
   }
   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;
showHighestModified(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: ";
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;
}

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

void showHighestModified(Player p[], int size)
{
int highPoints = p[0].points;
int highestIndex[12],numberofHighest=1,temp;
highestIndex[0]=0;
for (int index = 1; index < size; index++)
{
if (p[index].points > highPoints)
{
highPoints = p[index].points;
numberofHighest=1;
highestIndex[numberofHighest-1]=index;
}
else if(p[index].points == highPoints)
{
numberofHighest++;  
highestIndex[numberofHighest-1]=index;
}
else

{  
}
}
cout << "The player who scored the most points is:\n ";
for(int indexTemporary=0; indexTemporary < numberofHighest ;indexTemporary++)
{
temp=highestIndex[indexTemporary];  
cout <<"*"<< p[temp].name << endl;
}
}

//also the screenshot

Add a comment
Know the answer?
Add Answer to:
I need this done in C++ Can someone help me get my code from crashing. 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
  • 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...

  • Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem...

    Can anyone help me with my C++ assignment on structs, arrays and bubblesort? I can't seem to get my code to work. The output should have the AVEPPG from highest to lowest (sorted by bubbesort). The output of my code is messed up. Please help me, thanks. Here's the input.txt: Mary 15 10.5 Joseph 32 6.2 Jack 72 8.1 Vince 83 4.2 Elizabeth 41 7.5 The output should be: NAME             UNIFORM#    AVEPPG Mary                   15     10.50 Jack                   72      8.10 Elizabeth              41      7.50 Joseph                 32      6.20 Vince                  83      4.20 ​ My Code: #include <iostream>...

  • I am getting an error that the variable num was not declared in the scope on...

    I am getting an error that the variable num was not declared in the scope on the line of code that has; while(num != 5). Do you know how to fixe this error? #include <iostream> #include <iomanip> #include <string> #include<fstream> #include <cstring> using namespace std; //Declare the structure struct Games { string visit_team; int home_score; int visit_score; }; // Function prototypes int readData(Games *&stats); int menu(); void pickGame(Games *&stats, int size); void inputValid (Games *&stats, int size); void homeTotal(Games *&stats,...

  • In need of some help with a LCR C++ game. The code will run, but I...

    In need of some help with a LCR C++ game. The code will run, but I need it to cycle through the turns automatically so that the only input from the user is the number of players. It's also showing the winner as the person with 0 chips which is incorrect and I can't figure out why. Any help is greatly appreciated as this is already late. Game rules: Develop a program that follows the rules of Left Center Right...

  • 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...

  • I need help of how to include the merge sort code into this counter code found...

    I need help of how to include the merge sort code into this counter code found below: #include<iostream> using namespace std; int bubble_counter=0,selection_counter=0; // variables global void bubble_sort(int [], int); void show_array(int [],int); void selectionsort(int [], int ); int main() { int a[7]={4,1,7,2,9,0,3}; show_array(a,7); //bubble_sort(a,7); selectionsort(a,7); show_array(a,7); cout<<"Bubble counter = "<<bubble_counter<<endl; cout<<"Selection Counter = "<<selection_counter<<endl; return 0; } void show_array(int array[],int size) { for( int i=0 ; i<size; i++) { cout<<array[i]<< " "; } cout<<endl; } void bubble_sort( int array[],...

  • 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...

  • When running the program at the destructor  an exception is being thrown. Can someone help me out?...

    When running the program at the destructor  an exception is being thrown. Can someone help me out? vararray.h: #ifndef VARARRAY_H_ #define VARARRAY_H_ class varArray { public:    varArray(); // void constructor    int arraySize() const { return size; } // returns the size of the array    int check(double number); // returns index of element containg "number" or -1 if none    void addNumber(double); // adds number to the array    void removeNumber(double); // deletes the number from the array   ...

  • Can somebody help me with this coding the program allow 2 players play tic Tac Toe....

    Can somebody help me with this coding the program allow 2 players play tic Tac Toe. however, mine does not take a turn. after player 1 input the tow and column the program eliminated. I want this program run until find a winner. also can somebody add function 1 player vs computer mode as well? Thanks! >>>>>>>>>>>>>Main program >>>>>>>>>>>>>>>>>>>>>>> #include "myheader.h" int main() { const int NUM_ROWS = 3; const int NUM_COLS = 3; // Variables bool again; bool won;...

  • 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