0

学校のプログラムで助けが必要です。野球選手に関する情報をユーザーに尋ねるプログラムを作成する必要がありました。選手の打率、打席数、安打数を計算する必要があります。平均の計算で設定された数値が出力され、計算が実行されないという問題が発生しています。計算に使用するすべての変数に整数を入力しています。したがって、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;

}

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)

nBatsとは両方ともtHits整数であるため、整数演算のみを使用しています。
答えは整数になります。

例:
nBats = 10 &tHits = 3の場合、式は であると予想されます3.333
しかし、それは3

これを修正するには、次のように変更することをお勧めします。

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

gPlayedとについての式でも同じことが言えgcalledます。

于 2013-11-01T19:42:10.500 に答える
0

計算中にインデックスの値が間違っています。

コードをデバッガーに入れてステップ実行するとすぐにこれを見つけました。これは、本当に自分で行うべきだったことです。

から始めて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 に答える