I need some guidance formulating a Do, For, While output using a Fibonacci sequence. After searching the foums I only saw posts that had just the sequence of numbers outputed like so:
01123453 after a set number was preset.
I need the output to display like so:
0 +1 = 1
1 +1 = 2 and so on (dependent on what number of iterations the user selects. Any suggestion would be thankful.

``````#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int choice;       // Menu choice
int number;       // Number of iterations
int fib, fib1, fib2;       // Possible equation variables

// Display the menu and get a choice.
cout << "Please enter the number of your selection ";
cin >> choice;
cout << "0. Exit Program\n";
cout << "1. Fibonacci For\n";
cout << "2. Fibonacci Do\n";
cout << "3. Fibonacci While\n\n";

// Respond to the user's menu selection.
if (choice == 1)
{
cout << "How many iterations of the number do you want to see? ";
cin >> number;
cout << fib1 << + << fib2 << = << fib << endl;

}
else if (choice == 2)
{
cout << "How many iterations of the number do you want to see? ";
cin >> number;
cout << fib1 << + << fib2 << = << fib << endl;
}
else if (choice == 3)
{
cout << "How many iterations of the number do you want to see? ";
cin >> number;
cout << fib1 << + << fib2 << = << fib << endl;

}
else if (choice == 0)
{
cout << "Program ending.\n";
}
else
{
cout << "The valid choices are 0 through 3. Run the\n";
cout << "program again and select one of those.\n";
}
return 0;
}``````
3
Contributors
15
Replies
16
Views
8 Years
Discussion Span
Last Post by jonsca

So start by asking yourself what would 1 iteration look like, then 2, then 3. Find the commonalities and use your loop to automate the whole thing. In doing these types of computations generally 2 variables are used, one that holds the prior value, one that holds the next value and then the sum of those two gets assigned to the first one for the next iteration.

thing is how to I create a formula that always starts out as
0+1=1 and then use the 1 to create the next equation 1+1=2 for a (#) of times
would this be the equation and variables? If so how do I code this to compute x amount of times in sequence.
fib1=0;
fib2=1
fib=fib1+f2;

Right and then do the transition I was talking about

``````fib = fib1 + fib2;
fib1 = fib2;  (My other statement was incorrect)
fib2 = fib;
(output fib)``````

Go through it a couple of times on paper and see that when you are going through the loops the "old" values stick around for fib1 and fib2. They serve as initial values just like 0 and 1 did.
I'm sure you've done a for loop before and the while just follows from the for loop with a different layout of the same conditions (basically) and then a do while delays testing until the end of the loop.

okay here is my if statement I been working on, sorry for the late reply power been out.
I also changed the fib vairables
fib1=0
fib2=1
fib= fib1+fib2

``````for (fib = 1; fib <= i; fib++);
{
number[fib]=number[fib-1]+number[fib-2];
cout << number[fib] <<"\n";

}``````

I cannot get it to display the number sequence like I stated above.

Did you preload number[0] and number[1] with 0 and 1 respectively? Remember by your requirements you are doing it by terms so you should start out at 2 and run to < i

If not put up the full code and I'll take a look at it.

Okay I changed the counter variable to maxValue and I now get an 3 output errors and one before for error, here is the code

``````#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int choice;       // Menu choice
int maxValue;       // Number of iterations
int fib;		//Loop counter variable
int fib1 = 0;			//Possible variable
int fib2 = 1;       // Possible variable

// Display the menu and get a choice.
cout << "Please enter the number of your selection ";
cin >> choice;
cout << "0. Exit Program\n";
cout << "1. Fibonacci For\n";
cout << "2. Fibonacci Do\n";
cout << "3. Fibonacci While\n\n";

// Respond to the user's menu selection.
if (choice == 1)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue

for (fib = 1; fib <= maxValue; fib++)
cout << fib1 << + << fib2 << = << (fib1 + fib2) << endl;

}
else if (choice == 2)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)
cout << fib1 << + << fib2 << = << (fib1 + fib2) << endl;
}
else if (choice == 3)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)
cout << fib1 << + << fib2 << = << (fib1 + fib2) << endl;

}
else if (choice == 0)
{
cout << "Program ending.\n";
}
else
{
cout << "The valid choices are 0 through 3. Run the\n";
cout << "program again and select one of those.\n";
}
system("PAUSE");
return 0;
}``````

Okay I changed the counter variable to maxValue and I now get an 3 output errors and one before for error, here is the code

You claim there are 3 errors. Do you think it might be helpful to let us know what they are? After 15 posts, you'd think you'd have posting help requests down by now.

You claim there are 3 errors. Do you think it might be helpful to let us know what they are? After 15 posts, you'd think you'd have posting help requests down by now.

Sorry Walt P I hit submit before caught what the errors were actually. Heres the code below, Program runs ( I just made all of the statements FOR just to test). After inputting the # of iterations the numbers do not gradually increase to the iteration inputed.
for example: I input 5 and it displays
0+1=1
0+1=1
0+1=1
0+1=1
0+1=1
instead of
0+1=1
1+1=2
1+2=3
2+3=4
3+5+5
I know I am missing something in the For statement but I dont know what. If you can help I would greatly appreciate the knowledge.

``````#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int choice;       // Menu choice
int maxValue;       // Number of iterations
int fib;		//Loop counter variable
int fib1 = 0;			//Possible variable
int fib2 = 1;       // Possible variable

// Display the menu and get a choice.
cout << "Please enter the number of your selection\n";
cout << "0. Exit Program\n";
cout << "1. Fibonacci For\n";
cout << "2. Fibonacci Do\n";
cout << "3. Fibonacci While\n\n";
cin >> choice;

// Respond to the user's menu selection.
if (choice == 1)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 2; fib <= maxValue; fib++)
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;

}
else if (choice == 2)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;
}
else if (choice == 3)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;

}
else if (choice == 0)
{
cout << "Program ending.\n";
}
else
{
cout << "The valid choices are 0 through 3. Run the\n";
cout << "program again and select one of those.\n";
}
system("PAUSE");
return 0;
}``````

This is one of those times that you have to trace the program by hand because you're missing virtually everything we talked about before (see below).
You're just outputting everything and not actually doing the operations with the variables.
When you write: `cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;` it's only printing out fib1+fib2 and not actually storing anything.
Reference this:

``````fib = fib1 + fib2;
fib1 = fib2;  (My other statement was incorrect)
fib2 = fib;
(output fib)``````

from post #4. These are the actual statements you need to use in the code.

I added the piece of code an now it calculates correctly but only shows the last iteration, instead of the number inputed.

``````#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int choice;       // Menu choice
int maxValue;       // Number of iterations
int fib;		//Loop counter variable
int fib1 = 0;			//Possible variable
int fib2 = 1;       // Possible variable

// Display the menu and get a choice.
cout << "Please enter the number of your selection\n";

cout << "0. Exit Program\n";
cout << "1. Fibonacci For\n";
cout << "2. Fibonacci Do\n";
cout << "3. Fibonacci While\n";
cin >> choice;

// Respond to the user's menu selection.
if (choice == 1)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)
{
fib = fib1 + fib2;
fib1 = fib2;
fib2 = fib;

}
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;

}
else if (choice == 2)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;
}
else if (choice == 3)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;

}
else if (choice == 0)
{
cout << "Program ending.\n";
}
else
{
cout << "The valid choices are 0 through 3. Run the\n";
cout << "program again and select one of those.\n";
}
system("PAUSE");
return 0;
}``````

So move line 38 back into the for loop then.

I got the For menu to work successfully, I know have an issue with initiating the fib counter in the Do and while menus. I have placed the counter in and out of the statements but it only prints a single line no matter where I place it.

``````#include <stdafx.h>
#include <iostream>
#include <iomanip>
using namespace std;

int main()
{
int choice;       // Menu choice
int maxValue;       // Number of iterations
int fib;		//Loop counter variable
int fib1 = 0;			//Possible variable
int fib2 = 1;       // Possible variable

// Display the menu and get a choice.
cout << "Please enter the number of your selection\n";

cout << "0. Exit Program\n";
cout << "1. Fibonacci For\n";
cout << "2. Fibonacci Do\n";
cout << "3. Fibonacci While\n";
cin >> choice;

// Respond to the user's menu selection.
if (choice == 1)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
for (fib = 1; fib <= maxValue; fib++)

{
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;
fib = fib1 + fib2;
fib1 = fib2;
fib2 = fib;
}

}
else if (choice == 2)
{
do
{   //Get interation number
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;

fib = 1; fib <= maxValue; fib++;
fib = fib1 + fib2;
fib1 = fib2;
fib2 = fib;

cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;
} while (fib++ > maxValue);
}
else if (choice == 3)
{
cout << "How many iterations of the number do you want to see? ";
cin >> maxValue;
while ( maxValue <= 0)
{
cout << "Please enter a positive number: ";
cin >>maxValue;
cout << fib1 << " + " << fib2 << " = " << (fib1 + fib2) << endl;
fib = fib1 + fib2;
fib1 = fib2;
fib2 = fib;
fib = 1; fib <= maxValue; fib++;
}

}
else if (choice == 0)
{
cout << "Program ending.\n";
}
else
{
cout << "The valid choices are 0 through 3. Run the\n";
cout << "program again and select one of those.\n";
}
system("PAUSE");
return 0;
}``````

Gee where did line 49 come from? Your middle statement there is just going to evaluate to true or false and not do anything anyway.

I think you're confusing yourself a bit because you're using iterations and max value interchangeably but they are very different. In other words "fib" should not factor into your loop condition at all. Create a counter (just an int) to use for this purpose as you go through each iteration. Hint: you have to initialize your counter to 2 so that you account for the first two terms you have found outside of the loop.

Once you get the do/while loop done the while loop will be a slam dunk.

Curiosity question : why don't you ask for the iterations right after the user selects a choice (you can do it around line 24 or so) since you are holding onto the choice value anyway. That way you don't have to duplicate the code 3 times.

so replace maxvalue with something like int num.

maxValue is still fine. See for example: the 5th number the series is 3. What does 3 have to do with the number of iterations? Nothing. Incrementing the fib value doesn't make much more sense. So create something named count and compare if it's less than maxNum.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.