I am having some problems with my structures program for school. The problem comes after inputting the data in the struct and then trying to display the information in the struct in a chart format. I get some weird hex symbols while for the chart output. If anyone can help me that would be great! Thanks!
#include <iostream>
#include <iomanip>
usingnamespace std;
constint MAXLASTNAME = 20;
constint MAXFIRSTNAME = 10;
constint 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;
}
}
How many players would you like to enter data for(1-20): 2
Please enter player #1's first name: 1
Please enter player #1's last name: 1
Please enter the number of at bats for player #1: 1
Please enter the number of singles for player #1: 1
Please enter the number of doubles for player #1: 1
Please enter the number of triples for player #1: 1
Please enter the number of home runs for player #1: 1
Please enter the number of walks for player #1: 1
Please enter player #2's first name: 2
Please enter player #2's last name: 2
Please enter the number of at bats for player #2: 2
Please enter the number of singles for player #2: 2
Please enter the number of doubles for player #2: 2
Please enter the number of triples for player #2: 2
Please enter the number of home runs for player #2: 2
Please enter the number of walks for player #2: 2
Here is the data that you entered, in a nice chart format!
First Name Last Name AB 1B 2B 3B HR BB BA SA OBA
---------- --------- -- -- -- -- -- -- -- -- ---
1 1 1.000 1.000 1.000 1.000 1.000 1.000
2 2 2.000 2.000 2.000 2.000 2.000 2.000
Process returned 0 (0x0) execution time : 10.093 s
Press any key to continue.
You can mess around with the interface to get the formatting fixed...