-2
以下是我的平均每個賣家出售的箱子的代碼。無論箱子的數量或我輸入的賣家的數量如何,我都會得到3。平均始終顯示3
#include <iostream>
using namespace std;
int main()
{
int numBoxes, // Number of boxes of cookies sold by one child
totalBoxes = 0, // Accumulator - Accumulates total boxes sold by the entire troop
numSeller = 1; // Counter - Counts the number of children selling cookies
double averageBoxes; // Average number of boxes sold per child
cout << " **** Cookie Sales Information **** \n\n";
// Get the first input
cout << "Enter number of boxes of cookies sold by seller " << numSeller
<< " (or -1 to quit): ";
cin >> numBoxes;
while (numBoxes != -1)
{
cout << "Please input the number of boxes sold by the next seller (or -1 to quit): ";
cin >> numBoxes;
totalBoxes += numBoxes; // Accumulates the running total
numSeller++;
}
numSeller = numSeller - 1;
// WHEN THE LOOP IS EXITED, THE VALUE STORED IN THE numSeller COUNTER
// WILL BE ONE MORE THAN THE ACTUAL NUMBER OF SELLERS. SO WRITE CODE
// TO ADJUST IT TO THE ACTUAL NUMBER OF SELLERS.
if (numSeller == 0)
cout << "\nNo boxes were sold.\n\n";
else
{
averageBoxes = (totalBoxes/numSeller);
cout << "\n\nThe average number of boxes sold per seller is: " << averageBoxes << endl;
}
return 0;
}
使用調試器來檢測你的錯誤。首先不要問這個問題。 –
請在發佈您的代碼之前提供更完整的「問題」。告訴我們您採取了哪些措施來嘗試解決此問題。提出一個非常明確,簡潔和直接的問題。不要只是發佈代碼,並說「它不工作」。 – DevNull