| Ace70 (13) | |
|
I'm trying to get this program to display the fibonacci sequence to the any digit between 3 & 30. It is stuck on an infinite loop. here 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; } | |
|
|
|
| b1gb0y2013 (39) | |
|
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; if(num >= 3 && num <= 20) { while (num > 0) { b = a + b; a = b - a; b = a + b - a; cout << b; count = 10; count++; num--; } } system ("pause"); return 0; } | |
|
|
|
| Ace70 (13) | |
| I never thought of using a loop. But thanks a ton. It works perfectly. I'm new to C++ but how did you know to use the If ? | |
|
Last edited on
|
|
| TheNoobie (199) | |||
I'm so confused about this bit...
Your assigning 10 to count every time it goes through the loop, then adding 1 so that it is 11. So every time this loop goes through, count is the exact same.. I don't understand the point? I also don't see where count is even used, but I imagine this isn't your full code (even though it looks like it.) | |||
|
Last edited on
|
|||
| b1gb0y2013 (39) | |
| As TheNoobie said, Im not so sure about that either. Especially that you have int count = 0 outside the loop not used, and then make it 10 without even using it. | |
|
|
|
| Ace70 (13) | |
| I really don't get it either. But it works. Maybe the int count = 0 is ignored because I changed it to 10 inside the loop. | |
|
|
|
| ascii (892) | |||
|
generally in programming its not good to say "i dont understand it but it works" because its hard to get better that way :o anyhow heres my code.
| |||
|
|
|||