2013-11-01 30 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