Question

I need to update this C++ code according to these instructions. The team name should be "Scooterbacks".

I appreciate any help!

Create a text based menu to perform the following functions: -Enter Display team name. -Enter 1 Sort players from highest score to lowest score and display to screen -Enter 2 Search by player name. Enter 3 Search by jersey number Enter 4 Search by position played -Enter 5 Sort by player name and display to screen. -Enter 6 Sort by jersey number and display to screen. Enter 7 Sort by position and display to screen. -Enter 8 Print to file. -Enter9 to Exit.

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 filename,string names[],int jNo[],string pos[],int scores[],int size);

const int SIZE=25;

int main()
{
string names[SIZE],pos[SIZE];
int jNo[SIZE],scores[SIZE];
string filename;
int ch=0;

cout<<"Enter file name to read player stats : ";cin>>filename;
int size=loadFile(filename,names,jNo,pos,scores);


while(ch!=8)
{
menu();
cin>>ch;//input choice

switch(ch)
{
case 1:
//Displays lowest scorer and their score.
cout<<"\nLowest scorer : "<<lowestScorer(names,scores,size);
break;
case 2:
//Displays highest scorer and their score.
cout<<"\nHighest scorer : "<<highestScorer(names,scores,size);
break;

case 3:
/*Asks user to enter a name. If the user enters one of the players'
names, the program displays that player's name, their
jersey number, their position on the team, and their score.
*/
searchByName(names,jNo,pos,scores,size);
break;

case 4:
//Displays the team's combined point total.
cout<<"\nTotal Points : "<<totalPoints(scores,size);
break;

case 5:
/*Alphabetically sorts the player names, then displays the sorted
list of players along with their respective jersey numbers,
positions, and individual scores.
*/
sortByName(names,jNo,pos,scores,size);
cout<<"\nSorted list of players "<<endl;
displayToScreen(names,jNo,pos,scores,size);
break;

case 6:
/*Displays the list of players along with their respective
jersey numbers, positions, and individual scores. If the user
selects this option before sorting the names, the player stats
will be displayed in the order they appear in
the file "scores.txt".
*/
displayToScreen(names,jNo,pos,scores,size);
break;
case 7:
/*Writes player stats to a file. If the user
selects this option before sorting the names, the player stats
will be written to the new file in the order they appear in
the file "scores.txt".
*/
cout<<"\nEnter filename to write : ";cin>>filename;
writeToFile(filename,names,jNo,pos,scores,size);
break;
case 8:
//Quits and closes the program.
cout<<"Goodbye.";
break;
default:
cout<<"Invalid menu choice, try again!!";
}
}

return 0;
}

void menu()
{
cout<<"\n1. Lowest scorer"<<endl;
cout<<"2. Highest scorer"<<endl;
cout<<"3. Search by Name"<<endl;
cout<<"4. Total team points"<<endl;
cout<<"5. Sort by names"<<endl;
cout<<"6. Display all"<<endl;
cout<<"7. Write to file"<<endl;
cout<<"8. Exit"<<endl;
}

int loadFile(string file,string names[],int jNo[],string pos[],int scores[])
{
ifstream infile(file.c_str());//open file for reading
int count=0;//initially count is 0

infile>>names[count]>>jNo[count]>>pos[count]>>scores[count];

count++;
while(!infile.eof()) //read until end of file
{
infile>>names[count]>>jNo[count]>>pos[count]>>scores[count];
count++;
}
infile.close(); //close file
return count;
}

string lowestScorer(string names[],int scores[],int size)
{
int score=scores[0];
string name;
for(int i=0;i<size;i++)
{
if(score>scores[i])
{
score = scores[i];
name=names[i];
}
}
return name;
}

string highestScorer(string names[],int scores[],int size)
{
int score=scores[0];
int indx=0;
for(int i=0;i<size;i++)
{
if(score<scores[i])
{
//as in each new high score it needs to be updated
score = scores[i];
indx=i;
}
}

return names[indx];
}

void searchByName(string names[],int jNo[],string pos[],int scores[],int size)
{
string name;
bool flag=false;
cout<<"Enter player name to search (seach is case sensitive) : ";cin>>name;
for(int i=0;i<size;i++)
{
if(name==names[i]) //compare and found
{
cout<<names[i]<<", "<<jNo[i]<<", "<<pos[i]<<", "<<scores[i]<<endl;
flag=true;
break;
}
}
if(!flag)
cout<<"\nThere is no player with that name.";
}

int totalPoints(int scores[],int size)
{
int total=0;
for(int i=0;i<size;i++)
total+=scores[i];
return total;
}

//function sorts by player name, jersey number, and score
void sortByName(string names[],int jNo[],string pos[],int scores[],int size)
{
//using bubble sort
for(int i=0;i<size-1;i++)
for(int j=0;j<size-i-1;j++)
{
if(names[j]>names[j+1])
{

string t1=names[j];
names[j]=names[j+1];
names[j+1]=t1;

int t2=jNo[j];
jNo[j]=jNo[j+1];
jNo[j+1]=t2;

string t3=pos[j];
pos[j]=pos[j+1];
pos[j+1]=t3;

int t4=scores[j];
scores[j]=scores[j+1];
scores[j+1]=t4;
}

}
}

void writeToFile(string filename,string names[],int jNo[],string pos[],int scores[],int size)
{
ofstream outfile(filename.c_str());//open file for writing
for(int i=0;i<size;i++)
{
outfile<<names[i]<<" "<<jNo[i]<<" "<<pos[i]<<" "<<scores[i]<<endl;
}

outfile.close();
cout<<"\nData is written to "<<filename;
}

void displayToScreen(string names[],int jNo[],string pos[],int scores[],int size)
{
for(int i=0;i<size;i++)
{
cout<<names[i]<<", "<<jNo[i]<<", "<<pos[i]<<", "<<scores[i]<<endl;
}
}

/*file to read from—>scores.txt:

Drake 9 LeftWideReceiver 13
Clemont 11 LeftTackle 1
Sean 7 LeftGuard 1
Noah 5 Center 7
Sam 10 RightGuard 1
Will 8 RightTackle 1
Josh 3 TightEnd 6
Matt 4 Quarterback 21
Rob 6 FullBack 6
Timmy 2 HalfBack 6
Eddy 1 RightWideReceiver 13
Alfred 12 Substitution 0
Freddy 16 Substituion 1
Billy 23 Substituiton 3
Tommy 20 Substitution 1
*/

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

#include <iostream>
#include <cstring>
#include <fstream>
#include <algorithm>

using namespace std ;

struct player {
   string name ;
   int jersynumber ;
   string position ;
   int score ;
} ;

// assuming u know about comparators

// this is comparator to sort players by high to low score
bool scoreComparator(player a, player b){
   return a.score >= b.score ;
}
// this is comparator to sort by names
bool nameComaprator(player a, player b){
   return a.name < b.name ;
}
// this is comparator to sort by jersy number
bool jersyComaparator(player a, player b){
   return a.jersynumber < b.jersynumber ;
}
// this is comparator to sort by position of the player
bool positionComparator(player a, player b){
   return a.position < b.position ;
}

// sorting the players based on the scores using scoreComaprator
void sortByScoresHighToLow(player playerList[], int n){
   sort(playerList, playerList+n, scoreComparator);
}
//searching players with playername as key
void searchByPlayerName(player playerList[], int n, string playerName){
   for (int i = 0; i < n; ++i){
       // if player name is found then printiing details and exiting
       if(playerList[i].name == playerName){
           player p = playerList[i] ;
           cout << "found ..... " << endl ;
           cout << p.name << " " << p.jersynumber << " " << p.position << " " << p.score << endl << endl;
           return ;
       }
   }
   // if anything not found then printing message
   cout << "Player Not found" << endl << endl;
}
//searching players with jersy number as key
void searchByJersy(player playerList[], int n, int jersyNumber){
   for (int i = 0; i < n; ++i){
       // if player jersy is found then printiing details and exiting
       if(playerList[i].jersynumber == jersyNumber){
           player p = playerList[i] ;
           cout << "found ..... " << endl ;
           cout << p.name << " " << p.jersynumber << " " << p.position << " " << p.score << endl << endl;
           return ;
       }
   }
   // if anything not found then printing message
   cout << "Player Not found" << endl << endl ;
}
//searching players with position as key
void searchByPosition(player playerList[], int n, string position){
   bool b = false ;
   for (int i = 0; i < n; ++i){
       // if player position is found then printiing details and exiting
       if(playerList[i].position == position){
           b = true ;
           player p = playerList[i] ;
           cout << p.name << " " << p.jersynumber << " " << p.position << " " << p.score << endl << endl;
           return ;
       }
   }
   // if anything not found then printing message
   if(!b) cout << "Player Not found" << endl << endl ;
}

// sorting the players based on the names using nameComaprator
void sortByName(player playerList[], int n){
   sort(playerList, playerList+n, nameComaprator);
}
// sorting the players based on the Jersy number using jersyComaparator
void sortByJersy(player playerList[], int n){
   sort(playerList, playerList+n, jersyComaparator);
}
// sorting the players based on the positions using positionComparator
void sortByPosition(player playerList[], int n){
   sort(playerList, playerList+n, positionComparator);
}

void printToScreen(player playerList[], int n){
   player p ;
   // printing all values to screen
   for (int i = 0; i < n; ++i){
       p = playerList[i] ;
       cout << p.name << " " << p.jersynumber << " " << p.position << " " << p.score << endl;
   }
   cout << endl ;
}


int loadFile(char filename[], player plist[]){
   ifstream fin ;
   fin.open(filename); // loading file object
   int index = 0 ;
   while(!fin.eof()){
       // reading from file
       fin >> plist[index].name >> plist[index].jersynumber >> plist[index].position >> plist[index].score ;
       index++;
   }
   // returning number of players read
   return index ;
}

void printToFile(char filename[], player plist[], int n){
   player p ;
   ofstream fout ; // loading output file object
   fout.open(filename);
   // wrinting every player into a file
   for (int i = 0; i < n; ++i){
       p = plist[i] ;
       fout << p.name << " " << p.jersynumber << " " << p.position << " " << p.score << endl ;
   }

}

// you can change this depending on the contents of the files
const int SIZE=25;

int main(int argc, char const *argv[])
{
   player plist[SIZE] ;

   // u can write code to display the menu
   int option ;
   char filename[1000] ;
   cout << "enter filename : " ;
   cin >> filename ;
   int n = loadFile(filename, plist);
   string tp ;
   int pt ;
   do{
       // you can just display the menu here ...
       cout << "enter a option : " ;
       cin >> option ;
       switch(option){
           case 0 :
               printToScreen(plist, n);
               break ;
           case 1 :
               sortByScoresHighToLow(plist, n);
               printToScreen(plist, n);
               break ;
           case 2 :
               cout << "enter name :" ;
               cin >> tp ;
               searchByPlayerName(plist, n, tp);
               break ;
           case 3 :
               cout << "enter jersy number : ";
               cin >> pt ;
               searchByJersy(plist, n, pt);
               break ;
           case 4 :
               cout << "enter position : " ;
               cin >> tp ;
               searchByPosition(plist, n, tp);
               break ;
           case 5 :
               sortByName(plist, n);
               printToScreen(plist, n);
               break ;
           case 6 :
               sortByJersy(plist, n);
               printToScreen(plist, n);
               break ;
           case 7 :
               sortByPosition(plist, n);
               printToScreen(plist, n);
               break ;
           case 8 :
               cout << "enter filename : " ;
               cin >> filename ;
               printToFile(filename, plist, n);
               break ;
           case 9 :
               cout << "\nexiting ..... \n" ;
               break ;
           default :
               cout << "wrong option\n " ;
               break ;
       }

   }while(option != 9) ;


   return 0;
}

Open Save !!#include <iostream» <cstring> <fstream> <algorithm> 2#include 3#include 4#include 6 using namespace std 7 8 struct player string name int jersynumber 10 12 13 14 15 // assuming u know about comparators 16 17// this is comparator to sort players by high to low score 18 bool scoreComparator (player a, player b) 19 int score; return a.score >-b.score 28 sorting the players based on the scores using scoreComaprator 35 void sortByScoresHighToLow(player playerList, int n) C++Tab Width: 8 Ln 1, Col1 INS

Add a comment
Know the answer?
Add Answer to:
I need to update this C++ code according to these instructions. The team name should be...
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 have 2 issues with the C++ code below. The biggest concern is that the wrong...

    I have 2 issues with the C++ code below. The biggest concern is that the wrong file name input does not give out the "file cannot be opened!!" message that it is coded to do. What am I missing for this to happen correctly? The second is that the range outputs are right except the last one is bigger than the rest so it will not output 175-200, it outputs 175-199. What can be done about it? #include <iostream> #include...

  • Am I getting this error because i declared 'n' as an int, and then asking it...

    Am I getting this error because i declared 'n' as an int, and then asking it to make it a double? This is the coude: #include "stdafx.h" #include <iostream> #include <fstream> #include <string> #include <algorithm> #include <vector> using namespace std; void sort(double grades[], int size); char calGrade(double); int main() {    int n;    double avg, sum = 0;;    string in_file, out_file;    cout << "Please enter the name of the input file: ";    cin >> in_file;   ...

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

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

  • fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string>...

    fully comments for my program, thank you will thumb up #include <iostream> #include <fstream> #include <string> #include <iomanip> using namespace std; struct book { int ISBN; string Author; string Title; string publisher; int Quantity; double price; }; void choice1(book books[], int& size, int MAX_SIZE) { ifstream inFile; inFile.open("inventory.txt"); if (inFile.fail()) cout <<"file could not open"<<endl; string str;    while(inFile && size < MAX_SIZE) { getline(inFile, str); books[size].ISBN = atoi(str.c_str()); getline(inFile, books[size].Title);    getline(inFile, books[size].Author); getline(inFile, books[size].publisher);          getline(inFile,...

  • Hi there! I need to fix the errors that this code is giving me and also...

    Hi there! I need to fix the errors that this code is giving me and also I neet to make it look better. thank you! #include <iostream> #include <windows.h> #include <ctime> #include <cstdio> #include <fstream> // file stream #include <string> #include <cstring> using namespace std; const int N=10000; int *freq =new int [N]; int *duration=new int [N]; char const*filename="filename.txt"; int songLength(140); char MENU(); // SHOWS USER CHOICE void execute(const char command); void About(); void Save( int freq[],int duration[],int songLength); void...

  • Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string>...

    Can you tell me what is wrong and fix this code. Thanks #include <iostream> #include <string> #include <fstream> #include <sstream> using namespace std; //function void displaymenu1(); int main ( int argc, char** argv ) { string filename; string character; string enter; int menu1=4; char repeat; // = 'Y' / 'N'; string fname; string fName; string lname; string Lname; string number; string Number; string ch; string Displayall; string line; string search; string found;    string document[1000][6];    ifstream infile; char s[1000];...

  • Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly....

    Hi there. I tried to submit this before, but the copy/paste didn't copy my vectors correctly. Hello there. I am trying to implement the djikstras shortest path algorithm into my code in C++ with a vector of vectors. However, when I test I get an error "Exception thrown at 0x75FFC762 in graphMain.exe: Microsoft C++ exception: std::out_of_range at memory location 0x009CF26C" for line 115 in graph.cpp. Could you help me debug? I am so close! ----------------------------FILE NAME: pch.h--------------------------------------- // pch.cpp: source...

  • I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include...

    I need a detailed pseudocode for this code in C ++. Thank you #include <iostream> #include <string> #include <iomanip> using namespace std; struct Drink {    string name;    double cost;    int noOfDrinks; }; void displayMenu(Drink drinks[], int n); int main() {    const int size = 5;       Drink drinks[size] = { {"Cola", 0.65, 2},    {"Root Beer", 0.70, 1},    {"Grape Soda", 0.75, 5},    {"Lemon-Lime", 0.85, 20},    {"Water", 0.90, 20} };    cout <<...

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