简体   繁体   English

初始化后,布尔数组填充了不正确的值

[英]Bool array filled with incorrect values after initialization

Short Question: 简短问题:

I initialize the value in an array and show that it's a certain value, then the next time I check it the value has changed. 我在数组中初始化该值并显示它是某个值,然后在下次检查该值已更改时。 What have I done to make that happen? 我做了什么才能做到这一点?

Details: 细节:

In the interest of learning how to code, I'm practicing random programming challenges online. 为了学习编码,我正在在线练习随机编程挑战。 The current one is to take a string of numbers and determine if it's "magic". 当前的方法是采用一串数字并确定它是否“神奇”。 This involves having only one of each number in the string, and going through the whole string by having one number point to another (like if you had 113, first 1 would count to second 1, which would count to the 3, which would count to itself). 这涉及到字符串中每个数字仅包含一个,然后通过将一个数字指向另一个数字来遍历整个字符串(例如,如果您有113,则第一个1会计数到第二个1,这会计数到3,这会计数本身)。

Going through the number works out well, and I got that working without a problem, and I figured since I was going through the number this way already, I would make a bool array to count whether a digit had been used. 通过数字可以很好地工作,并且我可以毫无问题地进行工作,而且我认为自从我已经以这种方式浏览数字以来,我将创建一个布尔数组来计算是否使用了数字。 An array of 10 bools, and array[i] would be a bool value saying whether i had been used. 一个由10个布尔组成的数组,而array [i]是一个布尔值,表示是否使用过我。 Probably not the best implementation, but it seemed simple. 可能不是最好的实现,但这似乎很简单。

The array "visited" works, but "numUsed" doesn't seem to work. 数组“ visited”有效,但“ numUsed”似乎无效。 I've initialized every value just in case that was an issue, and when testing I've checked every value starts as 0. When actually checking the array though, the values aren't correct. 我已经初始化了每个值,以防万一这是一个问题,并且在测试时,我检查了每个值都从0开始。但是实际上检查数组时,这些值是不正确的。

The following is my test outputting. 以下是我的测试输出。 The first string of numbers is me printing off the "numUsed" array, to show I've initialized it all zero. 数字的第一个字符串是我打印出“ numUsed”数组,以显示我已将其全部初始化为零。 Then the number I'm checking, the value of the number at the first check, and then the value of the numUsed array at that 然后是我要检查的数字,第一次检查时是数字的值,然后是那个处的numUsed数组的值

0000000000 - 10[0][0]
0000000000 - 11[1][1]
0000000000 - 12[2][254]
0000000000 - 13[3][40]
0000000000 - 14[4][0]
0000000000 - 15[5][10]
0000000000 - 16[6][0]
0000000000 - 17[7][0]
0000000000 - 18[8][0]
0000000000 - 19[9][0]
0000000000 - 20[2][254]

And the Code: 和代码:

bool visited[7] = {0};
bool numUsed[10] = {0};

for(int i = 0; i < 10; i++) {
    cout << (numUsed[i] = false);
}

int charPlace = 0;
cout << " - " << numString;
for(int i = 0; i < numString.length(); i++) {
    charPlace = (charPlace + numString.at(charPlace)) % (numString.length());

    if(visited[charPlace] == false) visited[charPlace] = true;
    else                            return false;

    cout << "[" << numString.at(charPlace) << "][" << numUsed[(char)numString.at(charPlace)] << "]";
    if(numUsed[numString.at(i)] == false) numUsed[numString.at(i)] = true;
    else {
        return false;
    }
}

问题很可能是numString.at(charPlace)是一个像'1'这样的char而不是像1这样的整数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM