Page 1 of 1

## MFC Fibonacci Number Tutorial: A basic MFC application which calculates the nth number in the Fibonac Rate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=57445&amp;s=b8b74edc63369bca51011ac084453d14&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

### #1 captainhampton

• Jawsome++;

Reputation: 14
• Posts: 548
• Joined: 17-October 07

Posted 13 July 2008 - 04:11 PM

If you’ve been programming even for a small amount of time, odds are you’ve calculated or at least should be familiar with the Fibonacci Sequence. Rightfully so, this is one of the most important sequences of numbers pertaining to the field of computer science. So maybe you’ve got a console application of something basic to find the sequence and you want to really impress your professor and get some extra credit. Or maybe you just want to further your knowledge on programming and wish to take me up on a GUI driven program to find the nth value in the Fibonacci Sequence. For whatever the reason may be, I will step you through a basic MFC application to do that very thing:

First, start up Microsoft Visual Studio and go to File->New->Project. Make sure to highlight MFC application and enter an appropriate title for this project. I just called it Fibonacci Sequence:

Click OK when you’re done, and a pop window will show now, click next and on the upper left hand corner you should see some radio buttons. Make sure that you have the “Dialog Based” radio button selected and proceed to click “Finish”.

OK now you are presented with a dialog box with some buttons and text. Just delete everything on here so that you have a fresh and clean dialog box like so:

Alright we’re now ready to rock, on the right hand side you will see a “Toolbox” menu with a bunch of little gadgets. For our example we will make use of the “Edit Control”, “Static Text”, “Group Box, and “Button” items. Simply click and drag them over to the dialog box and set them as you like. Here is my example:

Ok, doesn’t have to be perfect just trying to get some type of GUI interface to fuel all of the code behind it. Speaking of which, let’s get down to the actual coding aspect of it all. First things first, we must create variables of which to utilize. Right click on the “Edit Control” box underneath the “nth” static text. Once the menu comes up, click on “Add Variable to get a window like this:

Now to make a few minor adjustments, underneath the “Category:” select “Value”. Under variable type select “double” and finally to name this variable under the “Variable name” field. Let’s call it “nth”. Easy enough right? Hit finish and the variable is created.

Now just do the same thing for the variable under the “Number in Sequence” field, however make sure to give it a unique variable name! I will call it “fib”.

So now we have our variables, but this application still does not do much of anything. Let’s make those lazy buttons at the bottom do something! Double click on the “Clear” button. Once you double click you will be sent to the actual code fueling this. For now it is an empty function definition which we will change. The “Clear” button must do what else but clear, or set the variables to “0”. That is done with the following code.

Fibonacci Sequence Clear Button:
```//Fibonacci Sequence Clear
void CCalculatorDlg::OnBnClickedButton64()
{
UpdateData(true);
nth = 0;
fib = 0;
UpdateData(false);
}

```

Be sure to keep the code surrounded by the “UpdateData(true); and UpdateData(false);

Now we need to add the code for the “Calculate” button, which is done as such. This code simply calculate the nth value in the Fibonacci Sequence and sets the appropriate variable we created in the “Edit Box” to display its result. Here is the code:

Fibonacci Sequence Calculate Button:
```//Fibonacci Sequence Calculate
void CCalculatorDlg::OnBnClickedButton65()
{
UpdateData(true);
if ( nth != 0 && fib == 0 ){
unsigned int i = 1, j = 0, k, t;
for (k = 1; k <= nth; k++){
t = i + j;
i = j;
j = t;
}//end for
fib = j;
}//end if
UpdateData(false);
}

```

Alright, so that is basically it. We have created a program to not only calculate the nth value in the Fibonacci Sequence, but to do it in style, so go ahead and try picking up some girls at the bar with your new program and impress them with your new found MFC skills!

Is This A Good Question/Topic? 0

## Replies To: MFC Fibonacci Number Tutorial:

### #2 gabehabe

• GabehabeSwamp

Reputation: 1431
• Posts: 11,001
• Joined: 06-February 08

Posted 13 July 2008 - 04:20 PM

Not content with a whole bunch of sort tutorials, now you're on to MFC?

Great tutorials though, keep up the good work