更新两个数组以跟踪月份和天数
updating two arrays to keep track of months and days
我很难更新两个数组——下面的代码似乎只更新了两天。
int month[365], day[365];
int countMonths[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int i = 0;
int currentmonth = 0;
int currentday = 1;
while(i < 365 && i < countMonths[currentmonth])
{
month[i] = currentmonth+1;
day[i] = currentday;
i++;
currentday++;
if(currentday > countMonths[currentmonth]);
{
currentmonth++;
currentday = 1;
}
}
在if
语句上,您有一个额外的分号
if(currentday > countMonths[currentmonth]);
你不应该把它放在那里。
问题是while循环中有这样的条件:i < countMonths[currentmonth]
-你停止迭代(因为i
将是29,countMonths[1]
是28),这就是为什么你的月份没有增加的原因。只要保持第一种状态,你就应该很好。
相关文章:
- Mongodb c++驱动程序:如何查询元素的数组
- 将数组的地址分配给变量并删除
- 从C++本机插件更新Vector3数组
- lambda参数转换为constexpr技巧,然后获取带链接的数组
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 数组索引的值没有增加
- 将对象数组的引用传递给函数
- 为char数组调整zlib-zpipe
- 2D数组来自文本输入,中间有空格
- std::向量与传递值的动态数组
- 在c++中用vector填充一个简单的动态数组
- 使用strcpy将char数组的元素复制到另一个数组
- 使用指针从C++中的数组中获取最大值
- C++使用整数的压缩数组初始化对象
- 告诉一个 const char 数组,除了编译时 C 样式的字符串外,它不以 '