二维数组和嵌套循环
two dimensional array and nested looping
我正试图使用二维数组和嵌套循环打印出三年内12个月的销售额。我很困惑。有人能告诉我使用这些方法对代码做了什么错误吗。我不想有其他选择。
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
int x = 0;
int v = 0;
int y = 0;
int sum = 0;
const int year = 3;
const int month = 12;
int _tmain(int argc, _TCHAR* argv[])
{
int sales[year][month];
char * date[12] = {"january", "february", "march", "april", "may", "june", "july", "august", "september", "october", "november", "december"};
for(int z = 0; z < 3; z++)
{
{
cin >> v;
sales * year[z] = v;
}
for(int x = 0; x < 12; x++)
{
cout << "Please enter the sales for month " << date[x] << ":n";
cin >> y;
sales * month[x] = y;
sum += y;
}
}
cout << "There are the sales of the c++ crook: n";
cout << sales[3][12] << endl;
//cout << "Month 1 = " << year[0] << " " << month[0] << endl;
//cout << "Month 2 = " << year[0] << " " << month[1] << endl;
//cout << "Month 3 = " << year[0] << " " << month[2] << endl;
//cout << "Month 4 = " << year[0] << " " << month[3] << endl;
//cout << "Month 5 = " << year[0] << " " << month[4] << endl;
//cout << "Month 6 = " << year[0] << " " << month[5] << endl;
//cout << "Month 7 = " << year[0] << " " << month[6] << endl;
//cout << "Month 8 = " << year[0] << " " << month[7] << endl;
//cout << "Month 9 = " << year[0] << " " << month[8] << endl;
//cout << "Month 10 = " << year[0] << " " << month[9] << endl;
//cout << "Month 11 = " << year[0] << " " << month[10] << endl;
//cout << "Month 12 = " << year[0] << " " << month[11] << endl;
//cout << "Month 1 = " << year[1] << " " << month[0] << endl;
//cout << "Month 2 = " << year[1] << " " << month[1] << endl;
//cout << "Month 3 = " << year[1] << " " << month[2] << endl;
//cout << "Month 4 = " << year[1] << " " << month[3] << endl;
//cout << "Month 5 = " << year[1] << " " << month[4] << endl;
//cout << "Month 6 = " << year[1] << " " << month[5] << endl;
//cout << "Month 7 = " << year[1] << " " << month[6] << endl;
//cout << "Month 8 = " << year[1] << " " << month[7] << endl;
//cout << "Month 9 = " << year[1] << " " << month[8] << endl;
//cout << "Month 10 = " << year[1] << " " << month[9] << endl;
//cout << "Month 11 = " << year[1] << " " << month[10] << endl;
//cout << "Month 12 = " << year[1] << " " << month[11] << endl;
//cout << "The annual sales for c++ crook is: " << sum << " ;]";
cin.get();
cin.get();
return 0;
}
两件事:
1) 您希望确保表达式左侧的内容是有效的"左值",也就是说,它转换到可以存储RHS求值结果的位置。类似的线条
sales *month[x] = v;
不符合要求。
另一个错误:当您声明数组时
sales[year][month];
您需要确保year
和month
都存在(已声明)并且具有有效值(可能是3
和12
?)-您有一个名为date
的数组,但您在中指的是month
sales * month[x] = v;
正如我提到的,你不能只把等式左边的东西相乘。你可以考虑
sales[year][month] = v;
在您的情况下,您的外循环z
从0
变为2
,这可能是您的year
;并且内循环从CCD_ 11到CCD_。然后你可以做
sales[z][x] = y;
你可能真的想记录"这些销售数字适用的年份",在这种情况下,你需要创建一个数组
salesYears[3];
并将年份的价值存储在那里。当你期望输入时提示用户是一个非常好的主意-
std::cout << "Please enter the year of the sales" << std::endl;
等等。
这些只是一些指示。你的代码真的一团糟。记住:
Declare all variables
Make sure all arrays are the right size
Prompt for inputs
Check that inputs are valid
Address 2D arrays with arrayName[index1][index2]
Thing on left hand side of equation must be "valid lvalue"
You might need additional variables to store both the year and the sales
相关文章:
- C++:带有 "auto" 的二维数组上的嵌套 For 循环
- 为什么我需要三个嵌套的大括号来调用赋值运算符,将const引用到二维数组
- C++ 中用于二维数组的 for-each 循环
- 程序终止,状态为 -1073741510 在 for 循环的中间,将输入分配给二维数组
- 在C++中循环遍历二维数组时改进 O(n)
- 比较二维数组中的数字时的无限循环
- C 从嵌套循环中的文件中读取,并将值分配给二维数组
- 在 c++ 中使用二维数组的 For 循环
- 在C++中对二维数组进行归零.循环需要2个吗
- 如何在指针表示法和数组表示法中循环遍历二维数组
- 如何在二维数组中存储数据并使用2个循环显示二维
- 二维数组和嵌套循环
- 二维数组中的 for 循环
- 如何在C++中为矢量数据结构中的二维数组获取用户输入,而不使用行和列上的两个for循环
- c++用于循环和二维数组
- 用于二维数组中循环的特殊多边形
- 用1个循环解释二维数组迭代
- 循环通过二维数组与随机板大小对角线
- c++中二维数组中的foreach循环
- 如何使用基本循环创建二维数组矩阵