找出一个正方形逻辑中可能的网格数量,但代码不接受超过六个输入,它停止并说.exe停止工作
find out the number of grids possible in a square logic is write but the code not accepts more than six inputs ,it stops and says .exe stopped working
这里怎么了?我们不能这样增加数组大小:
int main() {
int k = 0, c;
int b[k];
cin >> c;
do {
b[k++] = c;
cin >> c;
} while (c != 0);
}
int k=0,c; int b[k]
定义了大小 0
的数组b
。因此,您将无法解决此数组,例如通过b[1]=10
,因为这超出了数组的边界并产生未定义的行为。
是C ,使用std::vector
代替" C风格"动态数组:
#include <iostream>
#include <vector>
int main() {
int c;
std::vector<int> b;
cin >> c;
do {
b.push_back(c);
cin >> c;
} while (c != 0);
for (auto c : b) {
cout << c << endl;
}
}
尝试一下:
vector<int> b{istream_iterator<int>(cin), istream_iterator()}
这将从cin
读取并填充vector<int>
。请注意,您必须发送EOF才能终止循环。在我的计算机上, ctrl z
相关文章:
- C++多个输入和输出
- 为什么有时我输入一个整数,程序将第一个输入的数字打印成十进制数?
- 我正在尝试根据 4 个方程找出六个值
- 该程序只接受 5 个输入,而我给出 n=8
- 在 2D 矢量中获取多个输入时出现问题
- 使用多个输入向量中值的笛卡尔乘积调用 lambda
- 如何获取C++中的输入并将其"split"到列表中?换句话说,取 N 个输入并放入 N 长度的数组中
- 多个输入输入 for 循环
- Cin 需要两个输入
- 如何通过一次运行将多个输入样本馈送到C++张量流模型
- 将文本文件中的文本存储到六个不同的矢量中(C++)
- C++ 链表:它不会删除第一个输入
- 多个输入的代码失败
- 如何从用户的5个输入中找到最大值
- 为什么这个程序在我插入前2个输入后崩溃
- 计算着色器Open GL ES的多个输入
- 我正在尝试使用 c++ 编写二叉搜索树,但该程序仅接受 2 个输入并停止
- 为什么我的代码在要求打印长度和宽度的值后不接受多个输入?
- std::具有两个输入参数的矢量构造函数
- 找出一个正方形逻辑中可能的网格数量,但代码不接受超过六个输入,它停止并说.exe停止工作