Saludos, soy nuevo en el foro, gracias a los que se tomen su tiempo tratando de ayudarme.
Estoy trabajando con un proyecto para sacar estadísticas a jugadores de pelota. La situación es que quisiera integrarle a lo que ya esta hecho, Clases y Listas para ir entendiendo mejor el lenguaje, pero cada vez que trato de integrar una clase o una lista en este código me da errores. Si alguien pudiera orientarme el camino o darme un ejemplo donde o como pudiera integrar Clases y Listas, seria grandioso y por supuesto explicarme porque, aquí esta el código:
Código:
#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;
};
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;
}
double calcBA, calcSA, calcOBA;
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].AB + stats[i].walks);
return players;
}
void showData(Baseball stats[], int players)
{
int i;
cout << endl << endl;
cout << "Here is the data that you entered, in a nice chart format!\n\n";
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) << "BA" << setw(6) << "SA";
cout << setw(6) << "OBA" << "\n";
cout << "----------" << setw(14) << "---------" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--" << setw(6) << "--";
cout << setw(6) << "---" << "\n";
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
<< endl;
}
}
void checkPlayers(int &players)
{
while((players < 0) || (players > 20)) {
cout << "Invalid value entered!!!\n";
cout << "A valid value is between 0 and 20 inclusive!";
cout << "Please reenter your answer: ";
cin >> players;
}
}