After a couple of alterations to the program, most of it works the way I want it to except the first number being printed out (isn't correct). I'm going to change my container from an array to a vector to see if that helps, meanwhile can anyone give me some ideas?
sorry for posting the whole program
Code:
#include <iostream>
#include <iomanip>
using namespace std;
int getnum();
void Fibonacci(int length);
void init( int *, int length, int x);
void prnt( const int *, int length, int x);
int main ()
{
int length;
cout << "\n\t +======================================================+\n";
cout << "\t |======== ========|\n";
cout << "\t |===== =====|\n";
cout << "\t |=== Fibonacci Sequence Generator ===|\n";
cout << "\t |==== ====|\n";
cout << "\t |======= =======|\n";
cout << "\t +======================================================+\n\n\n\n";
length = getnum();
if(length < 2)
{
return 0;
}
Fibonacci(length);
return 0;
}
/*********************************************FUNCTIONS*************************************************/
int getnum()
{
int n = 0; // length = n in main function :)
do{
cout << "\n +-------------------------------------------------------------------+\n";
cout << " | How many numbers of the FIBONACCI SEQUENCE do you want dislayed |\n";
cout << " | |\n";
cout << " | Greater than 1 |\n";
cout << " +-------------------------------------------------------------------+\n\n\t\t\t\t :";
cin >> n;
if(n < 2)
{
cerr << "\n\t\t !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
cerr << "\t\t !!!!!! !!!!!!\n";
cerr << "\t\t !!!! Length is Out of Bounds !!!!\n";
cerr << "\t\t !!!!!! !!!!!!\n";
cerr << "\t\t !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!\n";
break;
}
}while(n < 2);
return n;
}
void Fibonacci(int length)
{
int x = 0;
const int MaxPerLine = 6;
int *Sequence = new int[length];
cout << "\n\t *******************************************************\n";
cout << "\t The first " << length << " integers of the Fibonacci series are: \n";
cout << "\t *******************************************************\n\n";
init(Sequence, length, x);
prnt(Sequence, length, x);
delete [ ] Sequence;
}
void init(int *Sequence, int length, int x)
{
x = 0; int y = 1;int sum;
for( int i = 0; i < length; ++i )
{
Sequence[i] = x;
if(i <= 1)
{
sum = i;
}
sum = x + y; //sums x and y at ever itteration
x = y; //changes variable value in preperation for the next calculation
y = sum; //changes variable value in preperation for the next calculation
}
}
void prnt( const int *Sequence, int length, int x)
{
int count = 0;
for(int i = length; i >= 0; --i)
{
cout << setw(5) << Sequence[i];
++count;
if(count % 6 == 0) //condition that inputs a new line after every 6 values
{
cout << '\n';
}
}
}