#include <iostream>
#include <iomanip>
#include <string>
using namespace std;
const int numplayers = 10;
const int numscores = 3;
void playerNames(string[],int);
void batsHits(int[][numscores],double[][numscores],int, int,string[]);
void battingAverage(int[][numscores],double[][numscores],int,int);
void displayStats(int[][numscores],double[][numscores],double[], string[], int, int);
int main()
{
// Declaration and Initialization Statements
int bats[numplayers][numscores]; //Players number of at bats
double playerhits[numplayers][numscores]; //Players number of hits
string playernames[numplayers]; //Players names
double avgplayerscores[numplayers]; //Players average scores
// Function Calls
playerNames(playernames,numplayers);
batsHits(bats, playerhits, numplayers,numscores,playernames);
battingAverage(bats,playerhits,numplayers,numscores);
displayStats(bats,playerhits,avgplayerscores,playernames,numplayers,numscores);
}
//Collects the baseball players names from user
void playerNames(string playernames[], int numplayers)
{
int i;
for(i=0;i<numplayers;i++)
{
cout << "Please enter Player #" << i+1 << "'s name: ";
getline(cin,playernames[i]);
}
}
//Gets the number at bats and hits for each player
void batsHits(int bats [][numscores],double hits [][numscores], int numplayers, int numscore,string playernames[])
{
int i;
int j;
for(i=0;i<numplayers;i++)
{
for(j=0;j<numscores;j++)
{
cout << "Enter " << playernames[i] << "'s At Bats: " << endl;
cin >> bats[i][j];
cout << "Enter " << playernames[i] << "'s Hits: " << endl;
cin >> hits[i][j];
}
}
}
//Calculates the batting average for each player
void battingAverage(int bats[][numscores],double avgplayerscores[],int numplayers,int numscores)
{
int i = 0;
int j = 0;
double total = 0;
for(i;i<numscores;i++)
{
for(j=0;j<numplayers;j++)
{
total+=bats[j][i];
}
avgplayerscores[i]=(total/(j+1)*1.0);
total=0;
}
}
//Displays the players stats and calculates the total number of hits and bats for the whole team.
void displayStats(int bats[][numscores],double playerhits[][numscores],double avgplayerscores[],
string playernames[], int numplayers, int numscores)
{
int i = 0;
int j = 0;
double avgbats[10];
double avghits[10];
double total = 0;
for(i;i<numscores;i++)
{
for(j=0;j<numplayers;j++)
{
total+=bats[j][i];
}
avgbats[i]=(total/(j+1)*1.0);
total=0;
}
for(i;i<numscores;i++)
{
for(j=0;j<numplayers;j++)
{
total+=playerhits[j][i];
}
avghits[i]=(total/(j+1)*1.0);
total=0;
}
cout << endl << endl;
cout << "Player AB HITS AVE"
<< endl;
cout << playernames[0] << setw(17) << bats[0][0] << setw(17)
<< playerhits[0][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[0] << endl;
cout << playernames[1] << setw(17) << bats[1][0] << setw(17)
<< playerhits[1][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[1] << endl;
cout << playernames[2] << setw(17) << bats[2][0] << setw(17)
<< playerhits[2][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[2] << endl;
cout << playernames[3] << setw(17) << bats[3][0] << setw(17)
<< playerhits[3][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[3] << endl;
cout << playernames[4] << setw(17) << bats[4][0] << setw(17)
<< playerhits[4][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[4] << endl << endl;
cout << playernames[5] << setw(17) << bats[5][0] << setw(17)
<< playerhits[5][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[0] << endl;
cout << playernames[6] << setw(17) << bats[6][0] << setw(17)
<< playerhits[6][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[1] << endl;
cout << playernames[7] << setw(17) << bats[7][0] << setw(17)
<< playerhits[7][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[2] << endl;
cout << playernames[8] << setw(17) << bats[8][0] << setw(17)
<< playerhits[8][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[3] << endl;
cout << playernames[9] << setw(17) << bats[9][0] << setw(17)
<< playerhits[9][0] << setw(17)
<< fixed << showpoint << setprecision(1) << avgplayerscores[4] << endl << endl;
cout << "Totals" << setw(14) << fixed << showpoint << setprecision(1)
<< avgbats << setw(17) << avghits
<< setw(17) << endl << endl;
}
错误LNK2019:未解析的外部符号“void __cdecl battingAverage(int(* const)[3],double(* const)[3],int,int)”(?battingAverage @@ YAXQAY02HQAY02NHH @ Z)引用在函数_main
它显然必须处理main函数中的函数battingAverage调用,我在这里搜索了这个错误,试图将c ++编译器选项更改为'Console',但它没有帮助。请帮助大家,我相信这是一个小问题,但我很累,试图解决它。
答案 0 :(得分:4)
您的声明:
void battingAverage(int[][numscores],double[][numscores],int,int);
您的定义:
void battingAverage(int bats[][numscores],double [],int numplayers,int numscores)
正如您所看到的,第二个参数存在差异 - double[][numscores]
vs double[]
。所以你实际上是在定义一个完全不同的功能。