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:
CODE
//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:
CODE
//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!
