Fibonacci Generator Won't Compile

I figured I'd try writing a fibonacci generator, but had no luck. I have no idea what's wrong with it. The error, according to the compiler, was on line 7 (the FOR loop).

Run on Ubuntu 11.04 (might be 11.00) using Code::Blocks.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
using namespace std;

int main()
{
    int y = 0;
        for (x = 1; x <= 100; x = x + y;)
    {
        cout << x << ", ";
        y = x;
        x = x + y;
    }
    cout << " End.";
    cin.get();
    return 0;
}


The way I understand it, y is initialized, x is initialized and set to one, the condition is set (while x is equal to or less than one hundred), and the increase is set (add y to x every loop)> It should show a "1, 2, 3, 5, 8, 13, 21, 34, 55, 89, End." or whatever. (I may have screwed up the math.) What am I missing? Is it that 'x = x + y' isn't allowed?
Last edited on
In general compilers tell you what was the error.
By instance gcc gives:
7:14: error; ‘x’ was not declared in this scope

(line:column)

So you should declare it
for( int x = 1; //...

You've got a logic error, but I suppose that you will handle it.
Last edited on
Topic archived. No new replies allowed.