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?

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
37
  #include <iostream>

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.

@Kizenku: Read Duoas' reply to this topic:
http://www.cplusplus.com/forum/beginner/1988/
http://www.cplusplus.com/articles/iw6AC542/
Duoas wrote:
This is typically a problem on Windows, caused by really dumb IDEs that don't know enough to keep the console open after the program finishes. However, it does strike right at one of the main philosophical problems with the way you are thinking about programming. After all, a console program should be run from the console --else once it terminates, the console should disappear
Last edited on
Topic archived. No new replies allowed.