我將數組初始化爲0。當我走過調試器時,我在[5] [4]元素中出現垃圾。存儲值的變量是emp4tot。我得到每個員工銷售總額。每個產品都由一排代表有五種不同的產品。這些列表示四名員工。我通過總結每個專欄的總數來計算所有員工的銷售額。爲什麼我總是爲數組獲取垃圾?
#include "stdafx.h"
#include <iostream>
void printtot(float[5][4], int, int);
int main()
{
using namespace std;
float sales[5][4] = { 0.0 };
int prodnum, empnum;
float prodtot=0;
const int numemp = 4;
const int numprod = 5;
/*do{
cout << "Enter Employee Number" << endl;
cin >> empnum;
cout << endl;
cout << "Enter Product Number" << endl;
cin >> prodnum;
cout<< endl;
cout << " Enter Product Sales" << endl;
cin >> prodtot;
cout << endl;
if ((empnum > 0) && (empnum < 5)){
--prodnum;
sales[prodnum][empnum] = prodtot;
}
}while ((empnum > 0) && (empnum < 5));
*/
printtot(sales, numemp, numprod);
cin.clear();
cin.ignore(255, '/n');
cin.get();
return 0;
}
void printtot(float salesarry[5][4],int numberempl, int numberprod){
using namespace std;
float product_tot;
float employee_tot;
float emp1tot, emp2tot, emp3tot, emp4tot;
int i, j;
employee_tot = product_tot = emp1tot = emp2tot = emp3tot = emp4tot = 0.0;
cout << "\t"<<"\t"<<"\tEmp 1" << "\tEmp 2" << "\tEmp 3" << "\tEmp 4"<< endl;
for (i = 0; i < numberprod; ++i){
product_tot = 0;
cout << "\t" << "Product " << i + 1;
for (j = 1; j < numberempl; j++){
cout <<"\t"<<salesarry[i][j];
product_tot += salesarry[i][j];
}
cout << "\t" << product_tot << endl;
emp1tot += salesarry[i][1];
emp2tot += salesarry[i][2];
emp3tot += salesarry[i][3];
emp4tot += salesarry[i][4];
}
cout <<"\tEmployee Totals"<<"\t"<<emp1tot<<"\t"<<emp2tot<<"\t"<<emp3tot <<
"\t" << emp4tot << endl;
}
OK感謝在想行的索引開始於1而不是零! – 2015-02-06 01:59:00