0

我需要关于学校计划的帮助。我们必须编写一个程序来询问用户有关棒球运动员的信息。我们需要计算球员的平均击球次数、击球次数和击球次数。我遇到了一个问题,即我的平均值计算输出一个设定的数字而不执行任何计算。我正在为所有用于计算的变量输入整数。所以我会输入像 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;

}

4

2 回答 2

2

在这条线上:

ba[index].playeraverage = (ba[index].nBats / ba[index].tHits) * (ba[index].gPlayed / ba[index].gcalled);//error

你有这个表达:

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

因为nBatstHits都是整数,所以您只使用整数数学。
答案将是一个整数。

例如:
nBats = 10 &tHits = 3,您希望表达式是3.333
但这只会是3

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

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

表达式 aboutgPlayed和 也是一样的gcalled

于 2013-11-01T19:42:10.500 回答
0

在计算过程中,您的 index 值是错误的。

我一将您的代码放入调试器并单步执行,就发现了这一点,这是您真正应该自己完成的事情。

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

请注意,此时ba[5]不是有效数据。但这正是在哪里ba[index]

您对ba[index]无效数据进行了所有计算,您想知道为什么会得到毫无意义的结果?

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

于 2013-11-02T22:00:50.717 回答