I wrote this code to produce a fibonacci seqeunce to a cretain number. However, when you get to a certain number, it starts producing negative numbers, and I don't know what to do.

#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
long lMax;
lMax = 0;
long x;
int Count;
cout << "Fibonacci Sequence Generator\nGenerate to what number?\n";
cin >> lMax;
int FibOut[lMax+1];
FibOut[1] = 1;
FibOut[2] = 1;
Count = 0;
do
{
Count++;
if (Count < 3)
{
Count = 3;
}
FibOut[Count] = FibOut[Count - 2] + FibOut[Count - 1];
}
while (Count < lMax);
Count = 0;
do
{
Count++;
cout << FibOut[Count] << "\n";
}
while (Count < lMax);
cout << "Press ENTER to end...\n";
x = cin.get();
return 0;
}

I wrote this code to produce a fibonacci seqeunce to a cretain number. However, when you get to a certain number, it starts producing negative numbers, and I don't know what to do.

Can you be slightly less vague than "a certain number"? It helps to give us all the information you have.

Also please format your code. It's very difficult to follow without proper indentation.

> it starts producing negative numbers
Yeah, somewhere between 40 and 50 IIRC, the size of F(n) becomes larger than what can be stored in a 32-bit integer and that's what you're seeing.

One fix is to use a bignum package like GMP

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.