我正在嘗試創建一個計算每年總圖書銷售額的循環。我還需要計算過去三年的圖書銷售總額。使用for循環和二維數組的C++求和
我已經算出瞭如何計算所有3年的總和,但是我每年的總書籍訂單的計算都有問題。這是我到目前爲止。
const int months = 12;
const int years =3;
string namonths [months] = {"January", "February", "March", "April",
"May", "June", "July", "August", "September",
"October", "November", "December"};
int bookorders[years][months];
int sum=0;
for (int i = 0; i < years ; i++) {
for (int n = 0; n < months; n++) {
std::cout << "Year " << i + 1 << " Month " << namonths[n] <<":"<< std::endl;
cin >> bookorders[i][n];
sum += bookorders[i][n];
}
}
// std::cout << "total orders are for each year are: " << sum <<std::endl;
std::cout << "total orders are " << sum <<std::endl;
使用了'namonths'作爲'string'和數組索引 - 這是否甚至編譯? – 2013-04-27 09:34:14