【问题标题】:Average always displaying 3平均总是显示 3
【发布时间】: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++


【解决方案1】:

你遇到了两个问题:

  1. 您没有备份存储在numBoxes 中的第一个值,因此存在数据丢失。
  2. 您遇到整数除法问题(即:截断错误)。

以下是关于整数除法的一些重要内容。

Division in C++ not working as expected

What is the behavior of integer division?

Dividing two integers to produce a float result

C++ Best way to get integer division and remainder

这是固定代码。只需要两个单行修复。

代码清单


#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;
    totalBoxes += 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--;
    // 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 = ((double)totalBoxes / (double)numSeller);
        cout << "\n\nThe average number of boxes sold per seller is: " << averageBoxes << endl;

    }

    return 0;
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-11-02
    • 1970-01-01
    • 2015-11-18
    • 1970-01-01
    • 1970-01-01
    • 2017-08-10
    • 2016-09-18
    • 1970-01-01
    相关资源
    最近更新 更多