Fibonacci Lab

In my class we were asked to create a program that outputs a whatever fibonacci number the user wanted. Then once you have a program that gives the user a correct Fibonacci number, write a while loop around the code that uses a bool conditional and an if statement that will determine whether or not the user would like to find multiple Fibonacci numbers... I'm having a problem creating this loop...

#include<iostream>
using namespace std;
int main()
{
int firstfib = 1;
int secondfib = 2;
int fibnext;
int n=0;

cout << "The first Fibonacci number is 1" << endl;
cout << "The second Fibonacci number is 2" << endl;
cout << "What Fibonacci number would like that is greater than 2?" << endl;
cin >> n;
while (n > -1) {
for (int i=0; i < n-2; i++) {
fibnext = firstfib + secondfib;
firstfib = secondfib;
secondfib = fibnext;}
cout << "The number you requested is " << fibnext << endl;
cout << "What other Fibonacci number would you like? Enter -1 to exit." << endl;
cin >> n;
}
}
Boolean is true or false...

So you can make a bool variable and define it either true or false...

bool variable=true;

Before the part you need to loop, set a condition to test bool. Once such method could be...

if (variable== !false){

run your code

one the last run of your loop change the value of variable and your loop is broken.

Last edited on
Topic archived. No new replies allowed.