2013-11-01 28 views
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; 

}

+0

描述什么是“怪异的数字”。你给你的程序输入了什么?你期望输出什么? – abelenky

+0

我有一个dejavue。 – user1810087

+0

你在做整数除法。做浮点,你会没事的。在整数除法中,smaller_number/greater_number总是= 0。换句话说,如果你不打击1000,那么你的打击为0. :-) –

回答

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。

+0

好的,谢谢你修复它,你会推荐什么调试器? – user2946166

+0

真的取决于你的平台。在Windows上,使用Visual Studio内置调试器。在Linux上,GDB和/或Eclipse是常见且有据可查的。我不太了解苹果。 – abelenky

+0

好的,谢谢你的信息。 – user2946166

2

在此行中:因为这两个nBatstHits是整数,您只使用整数运算

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

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

你有这样的表达。
答案将是一个整数。

例如:
nBats = 10 & tHits = 3,你所期望的表达是3.333。再以约gPlayedgcalled表达

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

同一件事:
,但只会是3

为了解决这个问题,我建议改变来。

+0

好吧,我试过了,输出不会改变。我需要一个数学库吗?如果没有其他想法? – user2946166

+0

你还没有澄清你的问题。你说你的程序回应一个“奇怪的号码”。你给了什么输入?你期望输出什么?你实际得到了什么输出? (*“奇怪的数字”*根本不准确) – abelenky

+0

对不起,我正在寻找3位小数位数。我输入完整的数字,然后对它们进行分区。输出是15903.876。但是我正在寻找从.100到.999的范围。有任何想法吗? – user2946166