Hi there guys.

Literally I have been asked this question;

"The Fibonacci sequence is 0, 1, 1, 2, 3, 5, 8, 13, … ; the first two terms are 0 and 1, and each term thereafter is the sum of the two preceding terms – i.e., Fib[n] = Fib[n – 1] + Fib[n – 2]. Using this information, write a C++ program that calculates the nth number in a Fibonacci sequence, where the user enters n in the program interactively. For example, if n = 6, the program should display the value 5."

I have created the program, but I want to give the User an option to either go back to the start of the program, or to quit it. i.e. I don't want them to have to continually click 'Run'. I know this is something to do with the 'Do While' Loop, but I am a bit of a novice and not really sure how to go about it/code it.

Any thoughts on my question and/or examples/walkthroughs would be much appreciated, just trying to get my head round it. Any suggestions to improve my code so far is also welcome. Thank you! :)

Here is my code so far.

``````#include <iostream>
#include <cmath>

using namespace std;

int main() {

int N;

cout<<"This program is designed to give the user any value of the Fibonacci Sequence that they desire, provided the number is a positive integer.";//Tell user what the program does

cout<<"\n\nThe formula of the Fibonacci Sequence is;   Fib[N] = Fib[N – 1] + Fib[N – 2]\n\n"; //Declare the Formula for the User

cout<<"Enter a value for N, then press Enter:"; //Declare Value that the User wants to see

cin>>N;//Enter the Number

if (N>1) {
cout<<"\nFor your value of N, \nFib[N]= "<<((pow(((1+(pow(5, 0.5)))), N))-(pow(((1-(pow(5, 0.5)))), N)))*(pow((pow(2.0, N)*(pow(5, 0.5))), -1));//Mathematic formula for Fibonacci Sequence
}

if (N<0) {
cout<<"\n\nThe value N must be a POSITIVE integer, i.e. N > 0"; //Confirm that N must be a positive integer. Loop.
}
if (N>100) {
cout<<"\n\nThe value for N must be less than 100, i.e. N < 100. N must be between 0 - 100.";//Confirm that N must be less than 100. Loop.
}
if (N==0) {
cout<<"\n\nFor your value of N, \nFib[0] = 0"; //Value will remain constant throughout, cannot be caculated through formula. Loop.
}
if (N==1) {
cout<<"\n\nFor your value of N. \nFib[1]=1";//Value will remain constant throughout, cannot be caculated through formula. Loop.
}

return 0;
}
``````

PS Sorry for it all being in Green, just the way it came out. Obviously not displayed like that in the actually program.

## All 12 Replies

Okay, it didn't actually all display in green, never mind that comment!

Also changed it to,

``````if (N>1 and N<100) {
cout<<"\nFor your value of N, \nFib[N]= "<<((pow(((1+(pow(5, 0.5)))), N))-(pow(((1-(pow(5, 0.5)))), N)))*(pow((pow(2.0, N)*(pow(5, 0.5))), -1));//Mathematic formula for Fibonacci Sequence
``````

to stop values of over 100 being entered. Cheers

You need to wrap the body of your code (lines 10-33) inside a loop. When the result is displayed, ask the user if they want to find another, get at yes/no type response, check it.

You would want to wrap lines 10-33 in a do-while loop.

``````int continue = 0;
do
{
//lines 10-33
cout << "Do you want to go agian? << endl
cout << "Enter 1 for yes 0 for no: ";
cin >> continue;
} while (continue != 0) // doing this to allow all non 0 numbers to continue
``````

This should get you started. You probably want to have some input verification so that you can have a more specific while condition.

Thanks very much I will give it a try. However I come up witht he problem that one of the conditions specified for this assignment is "Some of you have inquired if you can use a formula (available on the web) to calculate the Fibonacci series. The answer is: NO."

While I have no actually any of my code from online (I did it in maths last year), I can't see my argument holding.

So now I have a new question, can anyone possible give hints/tips/steps to calculate the answer?

I have a basic understanding, its just I don't know how to write the code to calculate the answer with the formula Fn = Fn-1 +Fn+2.

Badly worded question, but hard to say. Any help with this question/writing it in code would be much appriciated. Thanks again

Also, Nathan Oliver, I tried your method but the int continue = 0 comes up with the error 'unqualified ID'. What can I do to fix this? Here is code now.

``````#include <iostream>
#include <cmath>

using namespace std;

int main() {

int N;
int continue = 0

do {
cout<<"This program is designed to give the user any value of the Fibonacci Sequence that they desire, provided the number is a positive integer.";//Tell user what the program does

cout<<"\n\nThe formula of the Fibonacci Sequence is;   Fib[N] = Fib[N – 1] + Fib[N – 2]\n\n"; //Declare the Formula for the User

cout<<"Enter a value for N, then press Enter:"; //Declare Value that the User wants to see

cin>>N;//Enter the Number

if (N>1 and N<100) {
cout<<"\nFor your value of N, \nFib[N]= "<<((pow(((1+(pow(5, 0.5)))), N))-(pow(((1-(pow(5, 0.5)))), N)))*(pow((pow(2.0, N)*(pow(5, 0.5))), -1));//Mathematic formula for Fibonacci Sequence
}

if (N<0) {
cout<<"\n\nThe value N must be a POSITIVE integer, i.e. N > 0"; //Confirm that N must be a positive integer. Loop.
}
if (N>100) {
cout<<"\n\nThe value for N must be less than 100, i.e. N < 100. N must be between 0 - 100.";//Confirm that N must be less than 100. Loop.
}
if (N==0) {
cout<<"\n\nFor your value of N, \nFib[0] = 0"; //Value will remain constant throughout, cannot be caculated through formula. Loop.
}
if (N==1) {
cout<<"\n\nFor your value of N. \nFib[1]=1";//Value will remain constant throughout, cannot be caculated through formula. Loop.
}

{
cout<<"Do you want to enter another number?";
cout<<"Enter 1 to continue and 0 to exit";
cin>>continue;

} while (continue ! = 0);

return 0;
}

}
``````

you need a `;` at the end of line 9. Also change `continue ! = 0` on line 42 to `continue != 0`. As far as an algorithim for the fib series there are plenty of threads on this site that you can read to see how you would do it.

Thanks again Nathan. I ended up using a char function which worked. And yeah, I have looked, but the majority I have seen have been people asking if there code is perfect etc....rather not copy was just looking for few started points/steps which I could work on myself then refer back to other peoples questions if needed.

`continue` is a keyword in C++. Use a different name.

Thanks deceptikon. I cant believe I used a keyword for a varibale name. Shame on me :(

Often, the fibonacci sequence is computed using a recursive function as shown in your comments (except for 0 and 1), and fib(6) == 8, not 5. I think the teacher wants you to learn how to write a recursive function instead of plugging in a formula that you looked up on the web. The function signature would be something like this: `unsigned long fib(unsigned long n);`

rubberman, thanks for that. Not sure what you mean by "fib(6) == 8, not 5"?

So far I have this code (too add to code above);

``````long long Fibonacci(int N);

int main()
{
cout<<"Input Fibonacci Index number: ";
int Index = 0;
cin>> Index;
cout<<"Answer"<<Fibonacci(N)<<endl;
return 0;

}
long long Fibonacci(int input)
{
if (N < 2)
return N;
else
{
return Fibonacci(N - 1) + Fibonacci(N - 2);
}
}
``````

However I am not sure about the

`````` cout<<"Input Fibonacci Index number: ";
int Index = 0;
cin>> Index;
cout<<"Answer"<<Fibonacci(N)<<endl;
return 0;
``````

As it returns an error of not being able to identify N. This part of the code I got from looking at his notes. ANy help appriciated, just shooting in the dark here really, trial and error

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.