在c++中,循环后无法改变整数的值
unable to change value of integer after looping, C++
在程序开始时,我声明了变量i
和j
。
int i, j;
在执行过程中,我也使用变量名i
和j
作为oop索引变量。我意识到这可能不是清晰的最佳选择,但由于这是一个玩具项目,我认为这无关紧要。
问题是,在将数组的内容打印到txt文件的循环之后,i == N == 29
:
ofstream a_file("2d_array.txt");
for (int i = 0; i<N; ++i) {
for (int j = 0; j<N; ++j)
a_file << m[i][j] << ' ';
a_file << endl;
}
当我试图在程序中再次使用i
:
for (int num_slices_processed = h + 1; num_slices_processed < N;
num_slices_processed++){
i = 0;
j = num_slices_processed;
...
i
仍然设置为29,即使在应该设置为0的行之后。j
设置正确。这里发生了什么?
以下是问题部分的所有代码:
#include <string>
#include <iostream>
#include <fstream>
using namespace std;
int smallest(int x, int y, int z);
bool basesMatch(char b1, char b2);
int main() {
const int N = 29;
int h, l, mm;
int i, j;
l = 10;
mm = 2;
h = 5;
int m[N][N];
//initialize m
for (int i = 0; i < N; i++){
for (int j = i; j < N; j++) {
if (j-i <= h)
m[i][j] = 0;
}
}
ofstream a_file("2d_array.txt");
for (int i = 0; i<N; ++i) {
for (int j = 0; j<N; ++j)
a_file << m[i][j] << ' ';
a_file << endl;
}
for (int num_slices_processed = h + 1; num_slices_processed < N; num_slices_processed++){
//while j is in bounds, ie j < N. This fills in one diagonal slice of m from L->R top -> bottom.
i = 0; //************ i is not being set here
j = num_slices_processed;
while (j < N) {
if (basesMatch(seq[i], seq[j])) {
t = m[i + 1][j - 1];
m[i][j] = m[i + 1][j - 1];
}
else {//bases don't match
m[i][j] = smallest(m[i + 1][j] + 1, m[i][j - 1] + 1, m[i + 1][j - 1] + 1);
}
i++;
j++;
}
}
检查它是否进入循环。Num_slices_processed可以是null或者已经是<N,所以你永远不会碰到赋值语句>
相关文章:
- 如何反转整数参数包
- enum是C++中的宏变量还是整数变量
- 努力将整数转换为链表。不知道我在这里做错了什么
- 整数不会重复超过随机数
- 在C++中手动调整数组大小
- 检查输入是否不是整数或数字
- C++使用整数的压缩数组初始化对象
- 在C++中,将大的无符号浮点数四舍五入为整数的最佳方法是什么
- 将"打开的CV图像"中的"颜色"转换为整数格式
- 变量没有改变?通过向量的函数调用
- 通过套接字[TCP]传输数据 如何在C / C ++中打包多个整数并使用send() recv()传输数据
- 如何只允许用户输入正整数
- 如何在c++中从文本文件中逐行读取整数
- 为什么不同的整数声明会改变结果?
- C++整数不会改变
- 为什么使用 24 位整数时结构大小不会改变
- 为什么两个连续的转换会改变一个整数
- 在c++中不改变倒数第二位的情况下移动整数中的位
- 改变一个整数的位
- 在c++中,循环后无法改变整数的值