C++ 棒球击球平均程序

C++ 棒球击球平均程序,c++,C++,我需要学校的帮助。我们必须编写一个程序,向用户询问有关棒球运动员的信息。我们需要计算球员在比赛中的平均击球率,击球次数和命中次数。我遇到了一个问题,我对平均值的计算是输出一个设定的数字,而不是执行任何计算。我为所有用于计算的变量输入整数。所以我会输入数字,比如1,4,10等等。。。当程序运行时,公式自身设置的值等于15903.876。我所有用于平均值公式的变量都声明为整数,击球平均值本身声明为双精度。我自己做了一些调试,发现当它将击球次数除以击球次数时,计算会出错。如果有人能帮我解决这个问题,我

我需要学校的帮助。我们必须编写一个程序,向用户询问有关棒球运动员的信息。我们需要计算球员在比赛中的平均击球率,击球次数和命中次数。我遇到了一个问题,我对平均值的计算是输出一个设定的数字,而不是执行任何计算。我为所有用于计算的变量输入整数。所以我会输入数字,比如1,4,10等等。。。当程序运行时,公式自身设置的值等于15903.876。我所有用于平均值公式的变量都声明为整数,击球平均值本身声明为双精度。我自己做了一些调试,发现当它将击球次数除以击球次数时,计算会出错。如果有人能帮我解决这个问题,我将不胜感激

//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;
//图书馆
#包括
#包括
#包括
#包括
使用名称空间std;
班次平均值
{
公众:
字符串pName;
国际nBats;
int-tHits;
int gPlayed;
int gcalled;
双倍平均;
双平均值1;
双玩家平均数;
};
int main()
{
字符串数字层;
int nplayers;
//输入用户要为其输入数据的玩家数
cout>层数;
cout>n层)
{
n层=0;
}
cout在这一行:

ba[index].playeraverage = (ba[index].nBats / ba[index].tHits) * (ba[index].gPlayed / ba[index].gcalled);//error
你有这样的表达:

(ba[index].nBats / ba[index].tHits)
因为
nbat
tHits
都是整数,所以只使用整数数学。
答案将是一个整数

例如:
nBats=10
&
tHits=3
,您希望表达式为
3.333

但它只会是
3

要解决此问题,我建议更改为:

((double)ba[index].nBats / ba[index].tHits)

关于
gPlayed
gcalled

的表达式同样如此,在计算过程中,您的索引值是错误的

当我把你的代码放入调试器并逐步执行它时,我发现了这一点,这是你真正应该自己做的事情

int index=0;
开始,并随着用户输入每个玩家的数据而递增。 在输入循环结束时,索引现在与玩家数量相同。
(例如,如果您有5名玩家,索引现在是5,玩家数据存储在
ba[0]
ba[1]
ba[2]
ba[3]
ba[4]

请注意,此时
ba[5]
不是有效数据。但这正是
ba[index]
所在的位置

您在
ba[index]
上进行所有计算,这是无效的数据,您想知道为什么会得到无意义的结果


我建议您在开始计算之前将索引设置回0,并进行另一个循环,为每个玩家进行必要的计算0…4。

请描述一个“奇怪的数字”是。你给你的程序什么输入?你期望什么输出?你在做整数除法。做浮点运算,你会没事的。在整数除法中,小号/大号总是=0。换句话说,如果你没有打1000,你就在打0。:-)公共数据,见鬼!我知道了:)好的,我试过了,输出没有c我需要一个数学库吗?如果没有其他想法的话?你还没有澄清你的问题。你说你的程序用一个“奇怪的数字”来回答。你给了它什么输入?你期望得到什么输出?你实际得到了什么输出?(“奇怪的数字”一点也不精确)对不起,我在找一个小数点后3位的数字。我正在输入整数,然后对它们进行除法运算。输出是15903.876。但我在找一个从.100到.999的范围。有什么想法吗?你为什么不回答这个简单的问题:“你给你的程序输入了什么?”这是一个非常基本的调试问题,您一直拒绝回答。请编辑您的实际问题以包含此类信息。(您确实应该首先包含的信息。)不,不是。对于任何帮助您的人,我们希望重现您看到的相同问题。我们希望您使用相同的输入。语句如“我为所有变量输入整数”和“我将输入数字,如…”没有帮助。我们需要准确的输入、输入的预期结果以及实际得到的输出。好的,谢谢你修复了它,你会推荐什么调试器?这取决于你的平台。在Windows上,使用Visual Studio内置调试器。在Linux上,GDB和/或Eclipse是常见的,并且有很好的文档记录。我不知道苹果版本很好。