Wednesday, 2 October 2013

Need help cleaning up fibonacci sequence using C++ please

Need help cleaning up fibonacci sequence using C++ please

I'm still very new to C++ still and decided to make a fibonacci sequence.
It worked (Woo!) but it doesn't work as well as I would like it to.
what I mean by that is say for example I told my program to count the
first 10 terms of the sequence I will get
"0, 1, 1" and then I have to press enter for each additional number until
it hits ten in which case the program returns 0 and ends.
How do I get the program to display all the numbers I want to without
hitting enter for each additional one?
Here is my script:
#include <iostream>
using namespace std;
int main()
{
int FibNum;
cout << "How many numbers of the Fibonacci Sequence would you like to
see? \n\n";
cin>> FibNum;
cin.ignore();
int a = 0;
int b = 1;
int c = 2;
cout << "Fibonacci Sequence up to " << FibNum << " terms.\n\n";
cout << a << "\n" << b << "\n";
for (int c = 2; c < FibNum; c++) {
int d = a + b;
cout << d;
cin.ignore();
a = b;
b = d;
}
}
Thanks in advance for any help!
P.s. Also if you notice anything terrible I'm doing please feel free to
correct me, I'm very aware I'm probably doing a lot wrong, I'm just trying
to learn. :]

No comments:

Post a Comment