Some bugs that I cant discover

I am new to programming (or perhaps computer)
And I just started learning C++ yesterday.
While I try to make up a program that generates fibonacci sequence, it gives negative value when the amount of terms generated is large.
The code is as follows:

1 #include <iostream>
2 using namespace std;
3 int x,y,z,a;
4 int main()
5 {
6 cout<<"This program is for generating fibonacci sequence;\nPlease initialize the conditions\n";
7 cin>>x>>y; // x is the 1st term while y is the 2nd term
8 cin.ignore();
9 cout<<"Please limit the amount of numbers generated\n";
10 cin>>z; //z refers to the number of terms calculated
11 cin.ignore();
12 a=1;
13 cout<<"T("<<a<<")="<<x<<"\n";
14 cout<<"T("<<(a+1)<<")="<<y<<"\n";
15 while (a<z)
16 {
17 a++;
18 x+=y;
19 cout<<"T("<<a<<")="<<x<<"\n";
20 a++;
21 if (a<=z){
22 y+=x;
23 cout<<"T("<<(a)<<")="<<y<<"\n";}
24
25 }
26 cout<<"Sequence terminates";
27 cin.get();
28
29 }

The numbers refer to the number of lines
I input 1 1 for the first input (for x and y)and then 100 for the second input (for z)
Hope that anyone can save me
Last edited on
That's because an int can only hold so much data. You'd need to use a large type like a long int. Eventually you will run out of space though. In that case you would need to use a bignum library.
Topic archived. No new replies allowed.