having fun with C++

hello...i have the code where i wanna have fun with infinite result and was trying to make a function who is calling itself if n <1000000...
Here is the code:
#include <iostream>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
using namespace std;

double fib(double n);

int main()
{
    double n; 
    double answer;
    cout << "Enter number to fing: ";
    cin >> n;
    cin.ignore();
    
    cout << "\n\n";
    
    answer = fib(n)
    
    cout << answer << " is the " << n << "th Fibonacci number\n";
    
         return 0;
}

double fib (double n)
{
       cout << "Processing fib("<<n<<")...";
       
       if (n < 1000000)
       {
             answer = fib(n);
             return (1);
             }
        else
        {
            cout << "FATAL ERROR!!("<<n-2<<")FATAL ERROR!!("<<n-1<<")$$$$$$011000100$0111000\n";
            return(fib(n-2) + fib(n-1));
            }
}

and i get error in this line
cout << answer << " is the " << n << "th Fibonacci number\n";
Well, yeah... you're returning one. You're doing it wrong... http://www.dreamincode.net/code/snippet80.htm
should i return n?
No, you should return return(fib(n-2) + fib(n-1));

What you're doing it returning 1 until n is 1000000... but you aren't incrementing n, so n is never going to get bigger...
Topic archived. No new replies allowed.