2010-12-12 96 views
0

問題是要求員工編號1到10(他們給我的數組數字進入數組)給出了每個員工的總銷售額,通過合併3個月在一起。在我的加法函數中它正確地執行了所有操作....對於第一部分...它完美地顯示了數組中的數字,但是當它添加數組並拋出一個元素在這裏和那裏導致不正確的總數時。在我的代碼中,我補充說它應該在數組的第一組數字之後加上一組數字,它不跟隨數組,這裏是代碼:二維數組添加問題

我跟着你們向我展示了什麼(謝謝順便說一句),我現在增加員工#1的總額,其餘我不想做的。我想相互輸入員工#1停止顯示,然後在3個月的陣列停止顯示中,從3個數字中添加員工#2的總數(繼續,直到每件顯示1〜10)。我輸入了新的代碼進行修改。我是C++編程的新手,我還沒有學過類,所以我實在不能使用它們。

#include <iostream> 
#include <iomanip> 
#include <cstdlib> 

using namespace std; 

void displaySales(int sales[10][3]); 
void displayTotalSales(int total[10][3]); 

int main() 
    { 

    //declare array  Jan Feb Mar 
    int employ[10][3] = {{2400, 3500, 2000}, 
         {1500, 7000, 1000}, 
         {600, 450, 2100}, 
         {790, 240, 500}, 
         {1000, 1000, 1000}, 
         {6300, 7000, 8000}, 
         {1300, 450, 700}, 
         {2700, 5500, 6000}, 
         {4700, 4800, 4900}, 
         {1200, 1300, 400}}; 
    //displays the sales for the month 
    displaySales(employ); 
    displayTotalSales(employ); 
    system("pause"); 
    return 0; 
    } 

//******Functions******* 

void displaySales(int sales[10][3]) 
{ 


    for(int emp = 0; emp < 10; emp++) 
    { 
     cout << "Employee # " << emp + 1 
      << ": " << endl; 
     for (int month = 0; month < 3; month++) 
     { 

      cout << " Month " << month + 1 
       << ": "; 
      cout << sales[emp][month] << endl; 

     } //end for 
    } //end for 
} //end function 

void displayTotalSales(int total[10][3]) 
{ 
    int employ = 1; //employee number 
    int totalSales = 0; // total sales for the employee 


     for (int i = 0; i < 10; i++) 
     { 
      for (int j = 0; j < 3; j++) 
      { 
       totalSales += total[i][j]; 
      cout << "Employee # " << employ << ": " << endl; 
      cout << endl; 
      cout << "Total sales for the month: " << "$" << total[i][j]; 
      cout << endl; 
      } 
      cout << " Total Sales for the three months is: $" << totalSales << endl; 
      cout << endl; 
      employ++; 
     } 
} 
+2

你爲什麼要爲每位員工增加三次? – 2010-12-12 20:47:25

+0

當我將我的代碼複製到編輯中時,它會移動我的縮進等,對於此不便,我深表歉意。 – Rootz 2010-12-12 21:55:08

+0

如果你使用chrome,有一個很好的擴展,它允許你在textarea中選擇文本,並且只需按Tab鍵縮進(Textarea格式化程序)。 – 6502 2010-12-12 22:20:16

回答

3
do { 
    ... 
    totalSales = (total[i][j] + total[i][j+1] + total[i][j+2]); 
    j++; 
} while (j < 3); 

j中的第一次迭代後去出界。

但嚴重:使用classes!並使用containers

哦,你的大括號完全搞砸了。

+0

關於最後一部分的同意 - 'boost :: multi_array'是我們的朋友:) – Kos 2010-12-12 21:32:19

+0

@對於特定大小的數據,嵌套的'boost :: array'工作正常。或者我們可以有'boost :: array'的'std :: vector'等等,這取決於具體情況。爲工作選擇正確的工具。 :) – 2010-12-12 23:13:31

+0

因此,有沒有任何情況下,手動建立索引的'std :: vector'會比'boost :: multi_array'更好執行(正如我所理解的 - 應該是一個包含相同事物的封裝在頂端)? – Kos 2010-12-13 00:03:44

1

首先請更好地形成您的代碼!縮進會使這變得更容易理解和幫助你。

我感到這是一個編程課的作業問題,但我會盡力幫助你。

基本上你的問題是,你正在運行在數組的結尾,因爲,當j == 2例如,當您使用的語句:

totalSales = (total[i][j] + total[i][j+1] + total[i][j+2]); 

你正在嘗試引用J + 2這是實際上是數組的第五個元素,它不存在。

我做你的addFunk的10秒改寫(請更好的名稱功能)

你可以嘗試這樣的事:

void addFunk(int total[10][3]) 
{ 
    int employ = 1; //employee number 
    int totalSales = 0; // total sales for the employee 


    for (int i = 0; i < 10; i++) 
    { 
      for (int j = 0; j < 3; j ++) 
      { 
       totalSales += total[i][j];   
      } 

      cout << "employee num " << employ << "earned " 
      << "$" << totalSales << endl; 
      totalSales = 0; 

      employ++; 
      totalSales = 0; 
    } 
} 
+0

+1 Plus提及編輯和良好的命名約定等不是答案的一部分,但一般教育性:)(我也糾正你的索引,其實4是第五個元素) – ralphtheninja 2011-06-01 20:27:42

0

關於你提到的更新代碼,你說:

我現在將員工#1的總數加到其他我不想做的事情上。

的問題是這一行:

int totalSales = 0; // total sales for the employee 

看你已經把那裏的評論:這是每個員工。因此,它應該進入「每員工循環」:

for (int i = 0; i < 10; i++) 
{ 
    int totalSales = 0; 
    // Proceed as before with the per-month work. 

請閱讀有關C++中變量的「範圍」。另外,對C++來說是新手,或者對一般編程來說是新手,並不是真正的避免類的藉口。關於他們本身沒有什麼特別的進步;代碼只是像你做的那樣複雜 - 而且存在類,因爲正確使用它們有助於組織(讀取:簡化)你的代碼。

1

它可能不適合我添加這個答案,但由於沒有辦法讓新手評論,我只是在這裏說。

我同意卡爾關於學習對象。當我們在大學學習c和C++時,我們開始使用結構,然後開始上課,如果您認真對待編程,學習這些東西非常重要。

一個類只是一種描述現實世界中物體的方式。它具有屬性和行爲。例如,您可以有一個員工班級,每個月可以存儲他們的所有收入,並且可以在其中包含一個功能,讓您計算他們最近的收入。對代碼的這些小的增加將使得它更易於閱讀,組織和重用。

我認真地建議你花幾個小時在Google上搜索麪向對象的概念並嘗試一些C++的例子。他們很容易。

+0

+1,這樣你可以得到一點聲譽並開始發表評論作爲評論。 ;) – 2010-12-13 06:38:12

+0

謝謝卡爾!火上澆油! – 2010-12-14 22:07:06