This code returns the correct Fibonacci terms at first but then when it gets to the 47th term it starts giving negative numbers or seemingly random numbers
This is my code:
#include <iostream>
#include <string>
#include <vector>
#include <algorithm>
#include <cmath>
using namespace std;
int main() {
int x;
int t1 = 0;
int t2 = 1;
int nextTerm = 0;
cout<< "Enter the term in the sequence that you want:\n";
cin>> x;
for (int i = 1; i <= x; i++) {
if (i == 1) {
cout<< t1 << ", ";
continue;
}
if (i == 2) {
cout<< t2 << ", ";
continue;
}
nextTerm = t1 + t2;
t1 = t2;
t2 = nextTerm;
cout<< nextTerm << ", ";
}
cout<< "\n";
return 0;
}
And this is what it returns:
If anyone could explain why this is please answer, also I recently started learning C++ so please go easy on me in the comments :)