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.

Here's my code :

#include <iostream>
#include "stdafx.h"

using namespace std;

int main()
{ 

    const int NumsToCal = 5;
    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;
}

Edit

#include <iostream>
#include "stdafx.h"

using namespace std;

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;
}
Abhishek Ghosh
  • 2,593
  • 3
  • 27
  • 60
Ham
  • 1
  • 5

0 Answers0