### Trying to create Fibonacci sequence

Maybe this should be in the beginner forum but I'm trying to make a fibonacci sequence with some user inputs. I'm using arrays for this, the user inputs are for the Nth term and the starting number (as in the number in front of 0).

My problem is that when the program runs it's an infinite loop which constantly prints the starting number. Which, I think, means that my WHILE loop isn't coming to an end and my 'count' variable isn't increasing. Sorry to bother you guys, does anyone know what I've done wrong?

 ``12345678910111213141516171819202122232425262728293031323334353637`` `````` #include using namespace std; int main() { int start; int term; cout << "Input a starting number for the sequence: "; cin >> start; cout << "\nEnter the Nth term for the sequence to reach: "; cin >> term; int seq[term]; seq[0] = 0; seq[1] = start; bool comp; int count = 1; cout << seq[count]; while (comp == false) { count++; seq[count] = seq[count--] + seq[count-2]; cout << seq[count]; if (count == term) { comp = true; } } cin >> count; }``````

On line 15, it is not allowed by the C++ standard to declare an array from a non-const number like this. SOme compilers support it, but you should get out of this habit and use an STL container or dynamic memory (whichever is allowed, preferably STL containers).

Between line 18 where you declare comp and line 23 where you use it, you never initialize comp. Can't you just do a for loop or make the while condition while count < term?

On line 25, you increment count, but on line 26 you decrement it again. Maybe you mean count-1 and not count--?
15: int seq[term];
vla are illegal in c++, you may use std::vector

18: bool comp;
You did not initialize the variable, you intent to use it later (uninitialized) in line 23

23: while (comp == false)
`comp' is a bool, you could simply while( comp )

26: seq[count] = seq[count--] + seq[count-2];
undefined behaviour.
You modify count in that line ¿Should `seq[count]' use the old or new value? (ditto for `seq[count-2]')

28: if (count == term)
You have incremented `count' (line 25), then decremented it (line 26). You did nothing
Also, you may use that as your loop condition

35: cin >> count;
¿wtf?
ne555 wrote:
35: cin >> count;
¿wtf?
I'm assuming he's trying to hold the console open.