Fibonacci -- suggestions please
Hello all. I am still pretty new to C++ and I made this Fibonacci sequence thing.
What would you recommend to prevent a segmentation fault if the first to vector elements are not populated, or rather, how would you ensure the first two elements were populated?
Code:
#include <iostream>
#include <vector>
#include <iomanip>
using namespace std;
int main(){
vector<double> fib;
double stop = 200;
double i = 1;
fib.push_back(1);
fib.push_back(1);
while(i++ <= stop)
fib.push_back(fib[i-1] + fib[i-2]);
for(i = 0; i < fib.size(); i++)
cout << setprecision(50) << fib[i] << endl;
cout << endl;
return 0;
}
TIA!