Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need help with my C++ code everything runs fine and you are able to enter the

ID: 3573122 • Letter: I

Question

I need help with my C++ code everything runs fine and you are able to enter the data, but I cannot figure out why it will not display the data for formula answers like BA, SA, OBA, and SBP. Also i could use help with the formatting to make the chart look neater. Here is my code Thanks.

#include <iostream>
#include <iomanip>

using namespace std;

const int MAXLASTNAME = 20;
const int MAXFIRSTNAME = 10;
const int MAXPLAYERS = 20;

struct Baseball {
   char FirstName[MAXFIRSTNAME+1];
   char LastName[MAXLASTNAME+1];
   float AB;
   float singles;
   float doubles;
   float triples;
   float HR;
   float walks;
   float HBP;
   float StolenBases;
   float CaughtStealing;
};

void checkPlayers(int &players);
int getData(Baseball[]);
void showData(Baseball[], int);

int main()
{
   Baseball stats[MAXPLAYERS];

   int players = getData(stats);
   showData(stats, players);

}

int getData(Baseball stats[])
{
   int i, players;

   cout << "How many players would you like to enter data for(1-20): ";
   cin >> players;
   cout << endl;

   checkPlayers(players);

   for(i=0;i<players;i++) {
       cout <<   "Please enter player #" << i+1 << "'s first name: ";
       cin >> stats[i].FirstName;

       cout << "Please enter player #" << i+1 << "'s last name: ";
       cin >> stats[i].LastName;

       cout << "Please enter the number of at bats for player #" << i+1 << ": ";
       cin >> stats[i].AB;

       cout << "Please enter the number of singles for player #" << i+1 << ": ";
       cin >> stats[i].singles;

       cout << "Please enter the number of doubles for player #" << i+1 << ": ";
       cin >> stats[i].doubles;

       cout << "Please enter the number of triples for player #" << i+1 << ": ";
       cin >> stats[i].triples;

       cout << "Please enter the number of home runs for player #" << i+1 << ": ";
       cin >> stats[i].HR;

       cout << "Please enter the number of walks for player #" << i+1 << ": ";
       cin >> stats[i].walks;

       cout << "Please enter the number of hit by pitches for player #" << i+1 << ": ";
       cin >> stats[i].HBP;

       cout << "Please enter the number of stolen bases for player #" << i+1 << ": ";
       cin >> stats[i].StolenBases;

       cout << "Please enter the number of times caught stealing for player #" << i+1 << ": ";
       cin >> stats[i].CaughtStealing;
   }

   double calcBA, calcSA, calcOBA, calcSBP;
   double sum = 0;

   for(i=0; i<players; i++)
       sum += (stats[i].singles + stats[i].doubles + stats[i].triples + stats[i].HR);

   cout.precision(3);
   cout.setf(ios::fixed);

   calcBA = sum / (stats[i].AB);
   calcSA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
   calcOBA = (sum + stats[i].walks + stats[i].HBP) / (stats[i].AB + stats[i].walks + stats[i].HBP);
   calcSBP = stats[i].StolenBases / (stats[i].StolenBases + stats[i].CaughtStealing);


    return players;
}

void showData(Baseball stats[], int players)
{
   int i;
   cout << endl << endl;
   cout << "Here is the data that you entered ";


   cout << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
   cout << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "HBP" << setw(6) << "SB" << setw(6) << "CS" << setw(6) << "BA" << setw(6) << "SA";
   cout << setw(6) << "OBA" << setw(6) << "SBP" << " ";


   cout << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
   cout << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "---" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
   cout << setw(6) << "---" << setw(6) << "---" << " ";


   for(i=0; i<players; i++)
   {
       cout << stats[i].FirstName
             << setw(14) << stats[i].LastName
             << setw(6) << stats[i].AB
             << setw(6) << stats[i].singles
             << setw(6) << stats[i].doubles
             << setw(6) << stats[i].triples
             << setw(6) << stats[i].HR
             << setw(6) << stats[i].walks
             << setw(6) << stats[i].HBP
             << setw(6) << stats[i].StolenBases
             << setw(6) << stats[i].CaughtStealing
             << endl;
   }


}

void checkPlayers(int &players)
{
   while((players < 0) || (players > 20)) {
       cout << "Invalid value entered! ";
       cout << "A valid value is between 0 and 20 inclusive!";
       cout << "Please reenter your answer: ";
       cin >> players;
   }
}

Explanation / Answer

hi every thing is file but you have to declare calcBA, calcSA, calcOBA, calcSBP in global

The below code is working properly..

Program

#include <iostream>
#include <iomanip>
using namespace std;
const int MAXLASTNAME = 20;
const int MAXFIRSTNAME = 10;
const int MAXPLAYERS = 20;
struct Baseball {
char FirstName[MAXFIRSTNAME+1];
char LastName[MAXLASTNAME+1];
float AB;
float singles;
float doubles;
float triples;
float HR;
float walks;
float HBP;
float StolenBases;
float CaughtStealing;
};
//you have declare in global
double calcBA, calcSA, calcOBA, calcSBP;
void checkPlayers(int &players);
int getData(Baseball[]);
void showData(Baseball[], int);
int main()
{
Baseball stats[MAXPLAYERS];
int players = getData(stats);
showData(stats, players);
   return 0;
}
int getData(Baseball stats[])
{
int i, players;
cout << "How many players would you like to enter data for(1-20): ";
cin >> players;
cout << endl;
checkPlayers(players);
for(i=0;i<players;i++) {
cout << "Please enter player #" << i+1 << "'s first name: ";
cin >> stats[i].FirstName;
cout << "Please enter player #" << i+1 << "'s last name: ";
cin >> stats[i].LastName;
cout << "Please enter the number of at bats for player #" << i+1 << ": ";
cin >> stats[i].AB;
cout << "Please enter the number of singles for player #" << i+1 << ": ";
cin >> stats[i].singles;
cout << "Please enter the number of doubles for player #" << i+1 << ": ";
cin >> stats[i].doubles;
cout << "Please enter the number of triples for player #" << i+1 << ": ";
cin >> stats[i].triples;
cout << "Please enter the number of home runs for player #" << i+1 << ": ";
cin >> stats[i].HR;
cout << "Please enter the number of walks for player #" << i+1 << ": ";
cin >> stats[i].walks;
cout << "Please enter the number of hit by pitches for player #" << i+1 << ": ";
cin >> stats[i].HBP;
cout << "Please enter the number of stolen bases for player #" << i+1 << ": ";
cin >> stats[i].StolenBases;
cout << "Please enter the number of times caught stealing for player #" << i+1 << ": ";
cin >> stats[i].CaughtStealing;
}
  
double sum = 0;
for(i=0; i<players; i++)
sum += (stats[i].singles + stats[i].doubles + stats[i].triples + stats[i].HR);
cout.precision(3);
cout.setf(ios::fixed);
calcBA = sum / (stats[i].AB);
calcSA = (stats[i].singles + (2*stats[i].doubles) + (3*stats[i].triples) + (4*stats[i].HR)) / (stats[i].AB);
calcOBA = (sum + stats[i].walks + stats[i].HBP) / (stats[i].AB + stats[i].walks + stats[i].HBP);
calcSBP = stats[i].StolenBases / (stats[i].StolenBases + stats[i].CaughtStealing);

return players;
}
void showData(Baseball stats[], int players)
{
int i;
cout << endl << endl;
cout << "Here is the data that you entered ";

cout << "First Name" << setw(14) << "Last Name" << setw(6) << "AB" << setw(6) << "1B" << setw(6) << "2B";
cout << setw(6) << "3B" << setw(6) << "HR" << setw(6) << "BB" << setw(6) << "HBP" << setw(6) << "SB" << setw(6) << "CS" << setw(6) << "BA" << setw(6) << "SA";
cout << setw(6) << "OBA" << setw(6) << "SBP" << " ";

cout << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "---" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "---" << setw(6) << "---" << " ";

for(i=0; i<players; i++)
{
cout << stats[i].FirstName
<< setw(14) << stats[i].LastName << " "
<< setw(6) << stats[i].AB
<< setw(6) << stats[i].singles
<< setw(6) << stats[i].doubles
<< setw(6) << stats[i].triples
<< setw(6) << stats[i].HR
<< setw(6) << stats[i].walks
<< setw(6) << stats[i].HBP
<< setw(6) << stats[i].StolenBases
<< setw(6) << stats[i].CaughtStealing
           <<" " << calcBA << " " << calcSA << " " << calcOBA << " " << calcSBP
<< endl;
}

}


void checkPlayers(int &players)
{
while((players < 0) || (players > 20)) {
cout << "Invalid value entered! ";
cout << "A valid value is between 0 and 20 inclusive!";
cout << "Please reenter your answer: ";
cin >> players;
}
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote