問題是要求員工編號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++;
}
}
你爲什麼要爲每位員工增加三次? – 2010-12-12 20:47:25
當我將我的代碼複製到編輯中時,它會移動我的縮進等,對於此不便,我深表歉意。 – Rootz 2010-12-12 21:55:08
如果你使用chrome,有一個很好的擴展,它允許你在textarea中選擇文本,並且只需按Tab鍵縮進(Textarea格式化程序)。 – 6502 2010-12-12 22:20:16