我需要有关学校课程的帮助。我们必须编写一个程序,要求用户提供有关棒球运动员的信息。我们需要计算球员的平均击球次数,比赛次数,击球次数和击球次数。我遇到了一个问题,我的平均值计算正在输出一个设定值,而不执行任何计算。我正在为用于计算的所有变量输入整数。所以我将输入数字,例如1、4、10等。当程序显示该值时,我的公式将自身设置为15903.876。我用于平均值公式的所有变量均声明为整数,而打击平均值本身声明为double。我已经进行了一些自我调试,发现将蝙蝠击球次数除以击球次数会弄乱计算。如果有人可以帮助我解决问题,我将不胜感激。
//libaries
#include <iostream>
#include <string>
#include <sstream>
#include <iomanip>
using namespace std;
class battingAverage
{
public:
string pName;
int nBats;
int tHits;
int gPlayed;
int gcalled;
double average;
double average1;
double playeraverage;
};
int main()
{
string numberPlayers;
int nplayers;
//enters the number of players the user wants to enter data for
cout << "Enter the number of players you want to enter data for: ";
cin >> numberPlayers;
cout << endl;
//converts the value of numberPlayers to nplayers
istringstream convert(numberPlayers);
//sets integer nplayers equal to the value of the string numberPlayers
if(! (istringstream(numberPlayers) >> nplayers) )
{
nplayers = 0;
}
cout << "This program calculates the batting average of baseball players.\nYou may enter data for " << nplayers << " players." << endl;
battingAverage ba[nplayers];
int index = 0;
//while statement to get data
while(index < nplayers)
{
cout << "Enter the players last name: " << endl;
cin >> ba[index].pName;
cout << "Enter the number of games the player played: " << endl;
cin >> ba[index].gPlayed;
cout << ba[index].gPlayed << endl;
cout << "Enter the number of games the player was called in for: " << endl;
cin >> ba[index].gcalled;
cout << ba[index].gcalled << endl;
cout << "Enter the number of times the player was at bat: " << endl;
cin >> ba[index].nBats;
cout << ba[index].nBats << endl;
cout << "Enter the number of time the player hit: " << endl;
cin >> ba[index].tHits;
cout << ba[index].tHits << endl;
if(ba[index].tHits > ba[index].nBats)
{
cout << "Enter a valid value for the number of times the player hit: ";
cin >> ba[index].tHits;
}
cout << endl;
index++;
}
//rounds average to 3 decimal places
cout << fixed << setprecision( 3 );
//average formula
ba[index].playeraverage = (ba[index].nBats / ba[index].tHits) * (ba[index].gPlayed / ba[index].gcalled);//error
cout << ba[index].playeraverage << endl << endl;//just temp line to check calculation of average.
ba[index].average = .000;
ba[index].average1 = .099;
while(ba[index].average < 1 && ba[index].average1 < .899)
{
ba[index].average +=.100;
ba[index].average1 += .1;
//prints chart
cout << setprecision( 1 ) << ba[index].average << "00" << setprecision( 3 ) << setw(12) << ba[index].average1 << endl;
}
cout << "1.000" << setw(12) << "1.000" << endl;
//version of system pause
cout << "\nPress enter to continue...";
cin.sync();
cin.ignore();
return 0;
}
最佳答案
您的索引值在计算过程中是错误的。
我将代码放入调试器并逐步进行调试后,立即发现了这一点,您确实应该自己做。
您从int index = 0;
开始,并随着用户输入每个玩家的数据而递增。
在输入循环的末尾,索引现在与播放器的数量相同。
(例如,如果您有5位玩家,则索引现在为5,并且玩家数据存储在ba[0]
,ba[1]
,ba[2]
,ba[3]
和ba[4]
中)
请注意,此时ba[5]
是无效数据。但这正是ba[index]
的所在!
您对ba[index]
进行了所有计算,这是无效数据,您想知道为什么会得到毫无意义的结果吗?
我建议您在开始计算之前将索引设置回0,并进行另一个循环,为每个玩家0 ... 4进行必要的计算。
关于c++ - 棒球击球平均程序,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19734136/