I'm looking for any hints or tips ( because this is homework ) for correcting my program. The purpose of this program is to output the fibonacci sequence up to the 30th number if the user wants it too. But I keep getting an infinite loop that I have no clue how to fix. I'm new to c++ and a classmate recommended this site to me for programming help. Any input would be greatly appreciated.
her is the code I wrote:
#include <iostream>
using namespace std;
int main()
{
int num = 0;
int a = 0;
int b = 1;
int count = 0;
cout << "How many numbers do you want to run through? (3 min, 20 max) ";
cin >> num;
while ( num >= 3 && num <=20 )
{
b = a + b;
a = b - a;
b = a + b - a;
cout << b;
count = 10;
count++;
}
system ("pause");
return 0;
}
reading your reply made me realize that I was limiting the program to the 20th number, but I'm still stuck in an infinte loop. When I tried to use your solution, I get no output.
Because your loop is comparing int num which never changes in the loop. So basically at the end of the loop it goes back to while (num > 3 && num < 30) which will ALWAYS be true.