C++ Fibonacci recursion stack overflow error

-3

I'm trying to write a program which uses recursion to print out a Fibonacci number based on the input in my text file, but I always seem to get a stack overflow error (0xc00000FD) and nothing gets printed out in my output file. Can you guys help me find the problem? Here is the code:

#include <iostream>
#include <fstream>
using namespace std;
int F(int n){
  if (n <= 0) return 0;
  if (n == 1 || n == 2) return 1;
  return F(n-1)+F(n-2);
}
int main () {
  int n;
  ifstream ifs("11.in.txt");
  ifs >> n;
  ofstream ofs("11.out.txt");
  ofs << F(n);
  return 0;
}
c++
asked on Stack Overflow Dec 6, 2018 by Zalgan

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0