0

I was given the task of displaying Fibonacci numbers, but while asking the user how many number he/she would like to compute at a given time.

There was an example in the book they told me to refer. I figured a few lines of change in the code would produce the answer to my problem, but I'm having trouble understanding where I went wrong with this code.

int main()
{ 
    int NumsToCal = 5;

    cout << "How many numbers would you like to calculate?" << endl;
    cin >> NumsToCal;

    cout << " This program will calculate " << NumsToCal << " Fibonacci Numbers at a time" <<endl;

    int Num1 = 0, Num2 = 1;
    char WantMore = '\0';
    cout << Num1 << " " << Num2 << " " ;

    do 
    {
        for( int Index = 0; Index < NumsToCal; ++Index)
        {
            cout << Num1 + Num2 << " ";

            int Num2Temp = Num2;
            Num2 = Num1 + Num2;
            Num1 = Num2Temp;
        }
        cout <<  "Do you want more numbers (y/n)? " << endl;
        cin >> WantMore;

    } while (WantMore == 'y');

        cout << "Goodbye!" << endl;

    return 0;
}
1
  • 1
    you welcome! :), if that is the answer please check as correct my answer @Ham Commented Apr 7, 2015 at 11:53

2 Answers 2

1

Xsami is absolutely right. You only need to include one more line like:

cin>>NumstoCal;

Though it won't be bad to change the way you output stuff for a bit more clarity.

Here is my code: https://ideone.com/BXREP9

Sign up to request clarification or add additional context in comments.

Comments

0

The only thing that you have to do is read NumsToCal again, and you have to do something like this after cin >> WantMore;

if ( WantMore == 'y' ) 
{
    Num1 = 0;
    Num2 = 1;
    cout << "How many numbers would you like to calculate?" << endl;
    cin >> NumsToCal;
    cout << Num1 << " " << Num2 << " " ;
}

This is my code: http://ideone.com/a8um5Z

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.