c++ - Variabel 每次都会给出一个看似随机的答案

标签 c++ debugging random integer

<分区>

我正在学习 C++,并在 Project Euler(问题 2)上尝试了一个练习来练习我的 C++ 能力。一切都编译得很好,但每次定义某个变量时,它都以一个看似随机的整数开头。这是代码:

#include <iostream>
#include <Windows.h>

int fibonacci(int tripoloski){
    int first = 1;
    int second = 1;
    int next;
    int result;
    std::cout << first << std::endl;
    std::cout << second << std::endl;
    for(next = 1; next < tripoloski; ){
        result = result + first + second;
        next = first + second;
        first = second;
        second = next;
        std::cout << next << std::endl;
        std::cout << "WHOOP: " << result << std::endl;
    }
    return result;
}

int main(){
    int a = fibonacci(4000000);
    std::cout << "RESULT: " << a << std::endl;
    system("pause");
    return 0;
}

这是一个输出日志(忽略“tripoloski”和“WHOOP”:)):

1
1
2
WHOOP: 1075637419
3
WHOOP: 1075637422
5
WHOOP: 1075637427
8
WHOOP: 1075637435
13
WHOOP: 1075637448
21
WHOOP: 1075637469
34
WHOOP: 1075637503
55
WHOOP: 1075637558
89
WHOOP: 1075637647
144
WHOOP: 1075637791
233
WHOOP: 1075638024
377
WHOOP: 1075638401
610
WHOOP: 1075639011
987
WHOOP: 1075639998
1597
WHOOP: 1075641595
2584
WHOOP: 1075644179
4181
WHOOP: 1075648360
6765
WHOOP: 1075655125
10946
WHOOP: 1075666071
17711
WHOOP: 1075683782
28657
WHOOP: 1075712439
46368
WHOOP: 1075758807
75025
WHOOP: 1075833832
121393
WHOOP: 1075955225
196418
WHOOP: 1076151643
317811
WHOOP: 1076469454
514229
WHOOP: 1076983683
832040
WHOOP: 1077815723
1346269
WHOOP: 1079161992
2178309
WHOOP: 1081340301
3524578
WHOOP: 1084864879
5702887
WHOOP: 1090567766
RESULT: 1090567766
Press any key to continue . . .

第二个输出日志...:

1
1
2
WHOOP: 702745584
3
WHOOP: 702745587
5
WHOOP: 702745592
8
WHOOP: 702745600
13
WHOOP: 702745613
21
WHOOP: 702745634
34
WHOOP: 702745668
55
WHOOP: 702745723
89
WHOOP: 702745812
144
WHOOP: 702745956
233
WHOOP: 702746189
377
WHOOP: 702746566
610
WHOOP: 702747176
987
WHOOP: 702748163
1597
WHOOP: 702749760
2584
WHOOP: 702752344
4181
WHOOP: 702756525
6765
WHOOP: 702763290
10946
WHOOP: 702774236
17711
WHOOP: 702791947
28657
WHOOP: 702820604
46368
WHOOP: 702866972
75025
WHOOP: 702941997
121393
WHOOP: 703063390
196418
WHOOP: 703259808
317811
WHOOP: 703577619
514229
WHOOP: 704091848
832040
WHOOP: 704923888
1346269
WHOOP: 706270157
2178309
WHOOP: 708448466
3524578
WHOOP: 711973044
5702887
WHOOP: 717675931
RESULT: 717675931
Press any key to continue . . .

正如我之前所说,我刚刚开始学习 C++(大约几天前),所以答案对您来说可能非常明显。如果您不为此抨击 (badumm tss) 我并给我一个有见地的解释,我将不胜感激:)

-ThomThom

最佳答案

当你写result = result + first + second;时,在这之前你还没有初始化变量result。这是 undefined behavior , 此时变量可以包含任何内容。在此处使用变量之前将变量初始化为 0 将解决问题。

关于c++ - Variabel 每次都会给出一个看似随机的答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40873982/

相关文章:

.net - 发布 ASP.NET 应用程序时,是否应该在 Visual Studio 配置管理器中将构建类型更改为发布?

debugging - 在 Google Wave 之外测试 Google Wave 小工具?

c++ - 调试器什么时候会撒谎?

swift - 在 swift 中创建随机数数组的最短代码?

c++ - 将 int 转换为 char*

c++ - 我如何将对象从类线程传递到帖子?

c++ - Google Benchmark 作为 cmake 依赖项

c++ - 加速 std::map 和 boost:unordered_map [] 操作

javascript - 由 Javascript 添加的 Img 显示 0x0 像素

C++ GMP 生成随机数