question archive To compose a C++ console application that allows your user to enter the names and scores for up to 20 golfers

To compose a C++ console application that allows your user to enter the names and scores for up to 20 golfers

Subject:Computer SciencePrice:3.86 Bought6

To compose a C++ console application that allows your user to enter the names and scores for up to 20 golfers. The program should accept full names for golfers, that is first name and last name separated by a space. The program should validate user entries for golf scores to ensure that no golf score values are entered that are less than 42. Your program will use a sentinel value of your choosing to permit fewer than 20 entries. After all entries have been made, the program should display a list of the golfer's names and their corresponding golf score values sorted in order by golf score values, from highest to lowest.

 

pur-new-sol

Purchase A New Answer

Custom new solution created by our subject matter experts

GET A QUOTE

Answer Preview

#include <iostream>
#include <string>
using namespace std;
int main() {
  int noOfGolf;
  cout<<"Enter number of golfer: ";
  cin>>noOfGolf; //get input of golfer count
  while(noOfGolf<0||noOfGolf>20){ //if not in range re-enter 
    cout<<"Not in range. Re-enter: ";
    cin>>noOfGolf;
  } 
  //dynamic array to use speciifc space 
  string *name= new string[noOfGolf];
  int *score= new int[noOfGolf];


  //get input and check condition likewise 
  for(int i=0;i<noOfGolf;i++){
    cout<<"Enter "<<i+1<<" golfer name: ";
    cin.ignore();
    getline(cin,name[i]);
    cout<<"Enter "<<i+1<<" golfer score: ";
    cin>>score[i];
    while(score[i]<42){
      cout<<"Score should be greater than 42. Re-enter: ";
      cin>>score[i];
    }
    cout<<endl;
  }
  //sort results 


  for (int i = 0; i < noOfGolf-1; i++) 
    for (int j = 0; j < noOfGolf-i-1; j++) 
      if (score[j] < score[j+1]) { 
        // swap arr[j+1] and arr[j] 
        int temp = score[j]; 
        score[j] = score[j+1]; 
        score[j+1] = temp; 


        string tempName=name[j]; 
        name[j] = name[j+1]; 
        name[j+1] = tempName; 


      } 
//print results
for(int i=0;i<noOfGolf;i++){
  cout<<"Name: "<<name[i]<<endl;
  cout<<"Score: "<<score[i]<<endl<<endl;
}


}

Please see the attached file for the complete solution

Related Questions