C++平均计算函数返回0

C++ Average Calculation Function Returning 0

本文关键字:返回 函数 计算 C++      更新时间:2023-10-16

我正在编写一个程序,用于计算用户定义数量的球员的击球平均值。该程序显示球员的姓名、击球次数、命中次数和平均击球率。最后,它显示了球员击球的总次数、总命中次数和总平均值。出于某种原因,计算单个玩家平均值和整体平均值的函数返回0。它可能是一些小东西,但我不知道如何尝试和修复它。

//Batting Average Calculator
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;
//Create Structure
struct Record
{
    string name;
    int AB;
    int hit;
    double avg;
};

int getSize(int);
void getData(Record[], int );
int calculateTotalAB(Record[], int, int);
int calculateTotalHit(Record[], int, int);
double calculateTotalAvg(Record[], int, double);
void calculateAvg(Record[], int);
void display(Record[], int, int , int, double);

int main()
{
    const int MaxSize = 50;
    Record players[MaxSize]; 
    int size = 0;
    int totalAB = 0;
    int totalHit = 0;
    double totalAvg = 0;
    size = getSize(size);
    getData(players, size);
    totalAB = calculateTotalAB(players, size, totalAB);
    totalHit = calculateTotalHit(players, size, totalHit);
    calculateAvg(players,size);
    totalAvg = calculateTotalAvg(players, size, totalAvg);
    display(players, size, totalHit, totalAB, totalAvg);
}
//get number of players to be calculated
int getSize(int size)
{
    cout << "Please enter the number of players on the team: ";
    cin >> size;
    return size;
}
//get Player name, AB, and hit
void getData(Record players[], int size)
{
    string dummy;
    getline(cin, dummy);
    for (int i = 0; i < size; i++)
    {
        cout << "Please input the name of student " << i + 1 << ": ";
        getline(cin, players[i].name);
        cout << "Please input the number of times "<<  players[i].name << " was at bat:  ";
        cin >> players[i].AB;
        cout << "Please input the number of hits for " << players[i].name << ": ";
        cin >> players[i].hit;
        cout << " " << endl;
        getline(cin, dummy);
    }
}
int calculateTotalAB(Record players[], int size, int totalAB)
{
    for (int i = 0; i < size; i++)
    {
        totalAB = totalAB + players[i].AB;
    }
    return totalAB;
}
int calculateTotalHit(Record players[], int size, int totalHit)
{
    for (int i = 0; i < size; i++)
    {
        totalHit = totalHit + players[i].hit;
    }
    return totalHit;
}
void calculateAvg(Record players[], int size)
{
    for (int i = 0; i < size; i++)
    {
        players[i].avg = players[i].hit / players[i].AB;
    }
}
double calculateTotalAvg(Record players[], int size, double totalAvg)
{
    double j = 0;
    for (int i = 0; i < size; i++)
    {
        j = j + players[i].avg;
    }
    totalAvg = j / size;
    return totalAvg;
}
void display(Record players[], int size, int totalHit, int totalAB, double totalAvg)
{
    cout << fixed << showpoint << setprecision(3);
    cout << "Player        AB         Hit         Avg" << endl;
    cout << "  " << endl;
    for (int i = 0; i < size; i++)
    {
        cout << players[i].name << setw(8) << players[i].AB << setw(5) << players[i].hit << setw(5) << players[i].avg;
    }
    cout << "  " << endl;
    cout << "Totals      " << totalAB << "      " << totalHit << "      " << totalAvg << endl;
}

int除以计算为intint,并将其存储在double中。您要做的是先显式地将至少一个int值强制转换为双值,如下所示:

void calculateAvg(Record players[], int size)
{
    for (int i = 0; i < size; i++)
    {
        players[i].avg = players[i].hit / (double) players[i].AB;
    }
}