c++ - 变量作用域大于for循环,为什么不改变值?

标签 c++ debugging runtime-error

我有以下代码:

void nLargestPalindrome(double number){
int i = 5; //subtract "1" because array starts at zero
int intermediate = i; //intermediate value - equal to the array index;
double *decimalDigit = new double[i + 1];
    while (0 <= i){ //acquire digits of number
        decimalDigit[i] = (number - fmod(number, pow(10, i))) / pow(10, i);
        cout << "Value of digit place " << i << " is " << decimalDigit[i] << endl;
        number = fmod(number, pow(10, i));
        i--;
    }

所以我初始化了一个数组,其值等于它所在的数字位置,即 979979 ---> dD[5] = 9,dD[4] = 7,等等...

现在我在同一个函数中有以下代码:

double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
        for (int q = 0; q < 10; q++){ //decreases size of palindrome to the next largest palindrome.
            cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << '\t';
            cout << numberVectorAddition << endl;
            decimalDigit[2]--;
            decimalDigit[3]--;
                if (decimalDigit[2] == 0){
                    cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << '\t';
                    cout << numberVectorAddition << endl; //runtime error??
...
}
}

number = 997799

时我得到以下输出
Value of digit place 5 is 9
Value of digit place 4 is 9
Value of digit place 3 is 7
Value of digit place 2 is 7
Value of digit place 1 is 9
Value of digit place 0 is 9
997799  997799
996699  997799
995599  997799
994499  997799
993399  997799
992299  997799
991199  997799
990099  997799
989989  997799
988889  997799
987789  997799

什么给了?我真诚地尝试在谷歌中四处寻找 30 分钟,因为我之前在这里遇到过一系列低质量的问题。但是我没有成功。

我的问题:因为 numberVectorAddition 大于 for 循环范围,nVA 的值不应该改变吗?

最佳答案

线

double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];

需要放在for循环中。

    for (int q = 0; q < 10; q++){ //decreases size of palindrome to the next largest palindrome.
        double numberVectorAddition = pow(10, 5)*decimalDigit[0] + pow(10, 4)*decimalDigit[1] + pow(10, 3)*decimalDigit[2] + pow(10, 2)*decimalDigit[3] + pow(10, 1)*decimalDigit[4] + decimalDigit[5];
        cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << '\t';
        cout << numberVectorAddition << endl;
        decimalDigit[2]--;
        decimalDigit[3]--;
            if (decimalDigit[2] == 0){
                cout << decimalDigit[0] << decimalDigit[1] << decimalDigit[2] << decimalDigit[3] << decimalDigit[4] << decimalDigit[5] << '\t';
                cout << numberVectorAddition << endl; //runtime error??
        ...
}

否则,它会被计算一次并保持在该值。

关于c++ - 变量作用域大于for循环,为什么不改变值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27513460/

相关文章:

java - 是什么导致没有详细信息引发java.lang.Error?

c++ - C++ 标准是否指定在某些情况下编译应该失败并出现错误?

c++ - 在 C++ 中按大小为零分配内存?

c++ - 如何创建一个宏来验证 HR 和日志?

ios - 检测有问题的数组( Debug模式)

java - Android.OS.NetworkOnMainThreadException 中

swift - CAMetalLayer nextDrawable 返回 nil 因为分配失败

c++ - 带有面部检测和形状预测的 Dlib 网络摄像头捕获速度很慢

eclipse - 如何在eclipse中的java核心类中设置断点?

debugging - gdb:仅自动退出而不崩溃