【发布时间】:2015-06-22 05:18:07
【问题描述】:
以下是我的每个卖家平均售出盒子的代码。无论我输入的盒子数量或卖家数量如何,我总是得到 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;
}
【问题讨论】:
-
使用您的调试器来检测您的错误。不要先问 SO。
-
请在发布您的代码之前提供一个更完整的“问题”。告诉我们您已采取哪些步骤来尝试解决此问题。问一个非常清晰、简洁和直接的问题。不要只是发布代码并说“它不起作用”。
标签: c++