0

I'm learning C++ and tried an exercise on Project Euler (Problem 2) to practise my C++ abilities. Everything compiles fine, but everytime a certain variable is defined, it starts with a seemingly random integer. Here's the code:

#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;
}

And here's an output log (ignore the "tripoloski" and "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 . . .

second output log...:

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 . . .

As I said earlier, I just started with learning C++ (about a few days ago), so the answer might be very obvious to you. I would appreciate it if you won't bash (badumm tss) me for it and give me an insightful explanation :)

-ThomThom

  • 1
    If you don't give local variables a value they just contain random nonsense. – Galik Nov 29 '16 at 19:17
  • Handy tool that will save you a lot of grief: [Software debugger.](https://en.wikipedia.org/wiki/Debugger) It allows you to control the execution of your program, stopping every line if you have to, and see the contents of all of your variables. One comes with just about every development environment . If yours does not have a debugger, strongly consider getting a different environment. The ability to watch your program makes most bugs easy to find and fix. – user4581301 Nov 29 '16 at 19:22
  • Yeah, Erik made me realise that.. *dumb me* – ThomThom Nov 29 '16 at 19:22
  • I probably should use Code::Blocks instead of Notepad++... – ThomThom Nov 29 '16 at 19:23
  • Code::Blocks integrates the gdb debugger. Personally I prefer the heavyweight monster that is Eclipse (also integrates gdb) because of it's richer editing environment, and you have to work hard to find an easier to use debugger than the one that comes with Visual Studio. – user4581301 Nov 29 '16 at 20:41

2 Answers2

4

When you write result = result + first + second;, you have not initialized the variable result before this. This is undefined behavior, and the variable could contain anything at this point. Initializing the variable to 0 before you use it here will solve the problem.

Community
  • 1
  • 1
Erik Godard
  • 5,930
  • 6
  • 30
  • 33
2

The result variable is never initialized. Try changing its declaration to int result = 0

Andrew Stein
  • 12,880
  • 5
  • 35
  • 43