我需要帮助学校的课程。我们必须编写一个程序,询问用户有关棒球运动员的信息。我们需要计算球员击球平均值,比赛次数,击球次数和击球次数。我遇到一个问题,我的平均值计算输出一组数字而不执行任何计算。我正在为所有用于计算的变量输入整数。所以我会输入数字,如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;
}
答案 0 :(得分: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
&amp; tHits = 3
,您希望表达式为3.333
但它只是3
要解决此问题,建议您更改为:
((double)ba[index].nBats / ba[index].tHits)
关于gPlayed
和gcalled
的表达式再次相同。
答案 1 :(得分:0)
在计算过程中,您的索引值是错误的。
一旦我将您的代码放入调试器并逐步完成它,我发现了这一点, 真正 应该自己完成。
您从int index = 0;
开始,并在用户输入每个玩家的数据时递增它。
在输入循环结束时,索引现在与玩家数量相同
(例如,如果你有5个玩家,索引现在是5,玩家数据存储在ba[0]
,ba[1]
,ba[2]
,ba[3]
和{ {1}})
请注意,此时ba[4]
是 NOT 有效数据。但这正是ba[5]
所在的地方!
你在ba[index]
上做了所有的计算,这是无效的数据,你想知道为什么你会得到毫无意义的结果?
我建议您在开始计算之前将索引设置回0,并为每个玩家0 ... 4制作另一个循环,以进行必要的计算。