我刚刚开始使用C ++ Primer Plus,我遇到了一些小问题。
const int MONTHS = 12;
const int YEARS = 3;
int sales[YEARS][MONTHS] = {0};
const string months[MONTHS] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
for (int year = 0; year < YEARS; year++)
{
for (int month = 0; month < MONTHS; month++)
{
cout << "Please enter year " << year + 1 << " book sales for the month of " << months[month] << ": \t";
cin >> sales[year][month];
}
}
int yearlyTotal[YEARS][3] = {0};
int absoluteTotal = 0;
cout << "Yearly sales:" << endl;
for (int year = 0; year < YEARS; year++)
{
cout << "Year " << year + 1 << ":";
for (int month = 0; month < MONTHS; month++)
{
absoluteTotal = (yearlyTotal[year][year] += sales[year][month]);
}
cout << yearlyTotal[year][year] << endl;
}
cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;
我希望显示这三年的总数。其余的代码工作正常:输入很好,个人年度输出很好,但我不能将三年加在一起,最终总数。
每个选项的示例数据都会输入1
,为我提供三个12
总计:
第1年:12年 第2年:12 第3:12年3年期间销售的书籍总数为:12
最终的12
显然应该是36
。
我确实在一个点上完成了工作,但我没有完成个人工作。我搞砸了它并扭转了局面。
答案 0 :(得分:1)
看起来你每次迭代都会重置absoluteTotal
。你真的想要吗?
也许这就是你想要的?:
absoluteTotal += (yearlyTotal[year][year] += sales[year][month]);
答案 1 :(得分:1)
#include <iostream>
#include <string>
using namespace std;
int main (void)
{
const int MONTHS = 12;
const int YEARS = 3;
int sales[YEARS][MONTHS] = {0};
const string months[MONTHS] = {"January", "February", "March", "April", "May", "June", "July",
"August", "September", "October", "November", "December"
};
for (int year = 0; year < YEARS; year++)
{
for (int month = 0; month < MONTHS; month++)
{
cout << "Please enter year " << year + 1 << " book sales for the month of " << months[month] << ": \t";
cin >> sales[year][month];
}
}
int yearlyTotal[YEARS] = {0};
int absoluteTotal = 0;
cout << "Yearly sales:" << endl;
for (int year = 0; year < YEARS; year++)
{
cout << "Year " << year + 1 << ":";
for (int month = 0; month < MONTHS; month++)
{
yearlyTotal[year] += sales[year][month];
}
absoluteTotal += yearlyTotal[year];
cout << yearlyTotal[year] << endl;
}
cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;
return 0;
}
当涉及到这样的事情时,首先将它们写在纸上是有帮助的。
答案 2 :(得分:0)
问题在于,你有absoluteTotal
增量,你多次计算前几个月(因为yearlyTotal
是一个计数器,它每个月递增一次,所以将它添加到absoluteTotal
每次计算1个月12次,2个月11次等等。
相反,您希望该循环看起来像这样:
for (int year = 0; year < YEARS; year++)
{
cout << "Year " << year + 1 << ":";
for (int month = 0; month < MONTHS; month++)
{
(yearlyTotal[year][year] += sales[year][month]);
}
absoluteTotal += yearlyTotal[year][year];
cout << yearlyTotal[year][year] << endl;
}
所以你每个月只计算一次。
编辑:Good Person关于只需要一维数组的评论当然也是正确的。 :)
答案 3 :(得分:0)
检查以下代码。它会起作用。
int yearlyTotal[YEARS];
int absoluteTotal = 0;
cout << "Yearly sales:" << endl;
for (int year = 0; year < YEARS; year++)
{
yearlyTotal[year] = 0;
cout << "Year " << year + 1 << ":";
for (int month = 0; month < MONTHS; month++)
{
yearlyTotal[year] += sales[year][month];
absoluteTotal += sales[year][month];
}
cout << yearlyTotal[year] << endl;
}
cout << "The total number of books sold over a period of " << YEARS << " years is: " << absoluteTotal << endl;