Question: I was writing a small snippet to get a Fibonacci number sequence depending on the user input. If the user supplies 4 as an input, it should return him the first N members of the Fibonacci sequence.

```
#include <iostream>
using namespace std;

int main (){

    int a = 0;
    int b = 1;
    int c;
    int n = 3;
    n -= 2;

    if (n == 1){
        cout << a << endl;
    } else {
        cout << a << b << endl;

        for (int i=0;i<n;i++){
            c = b + a;
            cout << c << endl;
            a = b;
            b = c;
        }
    }

}

```

However, I end up getting a 0 as an output for whatever number I supply. I have this working in PHP and I kinda miss where I've blundered. I guess I don't actually render input and output properly.

Answer: 
```
int a =0;    
int n = 3;
n -= 2;

if (n == 1){
    cout << a << endl;
}

```

You have `n` equal to 3, you subtract 2, thus `n` equal to 1, so, you enter the if body and output `a`, which is zero.

[EDIT]

You don't seem to get any input -as stated in a comment- in your program (you could use `std::cin` or `std::getline()` for this), but you probably mean that you have the input hard-coded, by changing the value of `n` by hand.

You may want to check how the Fibonacci series program is expected to work:

1. [Fib. at Rosseta page.](http://rosettacode.org/wiki/Fibonacci_sequence)
2. [Fib. with recursion](https://stackoverflow.com/questions/21781247/printing-fibonacci-series-using-recursion-in-c?rq=1)
3. [Non-recursive Fib.](http://www.dreamincode.net/forums/topic/91499-non-recursive-fibonacci/)

After reading the links I provided above, you should be able to see that your code should be changed to this:

```
#include <iostream>
using namespace std;

int main (){

    int a = 1;
    int b = 0;
    int c;
    int n = 10; // "input" is 10

    if (n == 0 || n == 1) { // 0 and 1 case
        cout << n << endl;
    } else {
        for (int i = 2; i <= n; ++i) { // here you want to reach n
            c = a + b;
            b = a;
            a = c;
        }
        cout << c << endl;
    }

    return 0;
}

```

However, the code above outputs only the result. You should slightly modify it to get the terms of the sequence, but I'll leave you have some fun too.

In order to really let the user input the number, change:

`int n = 10;`

to

```
int n;
std::cout << "Please, input.\n";
std::cin >> n;

```

However, letting user inputting must be followed by validation of the input. You see users can, by accident or not, provide input in your program, that can cause undefined behaviour.
![enter image description here](https://i.stack.imgur.com/QNy08.jpg)
Try this which would give you the list you needed.

```
#include <iostream>
using namespace std;

int fib(int num){
    int ans;
    if (num >2) {
       ans = fib(num-1) + fib(num-2);
    }
    else
       ans = 1;
    return ans;
}

int main()
{
    int num, x=1;
    cin >> num;
    while (num >= x) {
       cout << fib(x) <<" ";
       x++;
    }
    return 0;
}

```