2015-06-21 144 views
-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; 
} 
+5

使用調試器來檢測你的錯誤。首先不要問這個問題。 –

+1

請在發佈您的代碼之前提供更完整的「問題」。告訴我們您採取了哪些措施來嘗試解決此問題。提出一個非常明確,簡潔和直接的問題。不要只是發佈代碼,並說「它不工作」。 – DevNull

回答

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; 
}