While Loop Terminating with break statement

Hello everyone! This is my code, and I need a second opinion. My program executes and outputs correctly, but I don't have the matching output as my book. Can someone help me out and verify that this C++ program us coded correctly?

Here is the code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main()
{
	long bound;
	cout << "Enter a positive integer: ";
	cin >> bound;
	cout << "Fibonacci numbers < " << bound << ":\n0, 1";
	long f0=0, f1=1;
	while (true)
	{
		long f2 = f0 + f1;
		if (f2 > bound) break;
		cout << ", " << f2;
		f0 =f1;
		f1 = f2;

		system("pause");
		return 0;

	}
}



Sample Output
Enter a positive integer: 1000
Fibonacci numbers < 1000:
0, 1, 1Press any key to continue . . .


Book show sample output to be this:
Enter a positive integer: 1000
Fibonacci numbers < 1000:
0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377, 610, 987
Press any key to continue . . .


Why is my output different than the book? Is the book's output a typo?
My program executes and outputs correctly, but I don't have the matching output as my book.
This doesn't make sense... Assuming you know what the Fibonacci sequence is, your output is incorrect, the books output is the proper fibonacci sequence.

As for your screwed up output, that's because you put lines 20 and 21 inside the while loop, when they should be in the body of the main function outside of any loops in this program.

Also, I'd invest in a new textbook, the code written here is pretty poor. The system commands are inefficient and unnecessary, and the code isn't written very logically.
Last edited on
Well..not all books are perfect..lol.

But it was line 20 and 21. I took them out of the loop and now outputs match. Thank you. You are very kind! =-)
Topic archived. No new replies allowed.