Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How do I make my app calculate 1+1 in Function.cpp and then display the result using classes?

Posted on 2011-03-03
2
Medium Priority
?
532 Views
Last Modified: 2013-12-14
I would ultimately like to create a simple dialog-based app that does a simple calculation, like adding 1+1, and  displays the result in a text box.  The caveat is, that I would like the calculation performed in a separate .cpp file (Functions.cpp) so that I can isolate the calculation part of the program from everything else.  So, I did the following:

1.   Created an MFC dialog-based application in VS2008 called "FirstApp".  The files FirstApp.cpp and FirstAppDlg.cpp were created (along with their respective header files).  

2.  Using the Resource View tab, I inserted a button dialog called IDC_BUTTON1.  This is the button to be pushed.

3.  Then I Inserted an edit control dialog called IDC_EDIT1. This is where the results of 1+1 is to be displayed.  Added a variable (value) called m_strEdit1 of type String to the control.

4. I created Functions.cpp and Functions.h (no code in there yet)

5. Double clicked on the button dialog and inserted the code below

Question: How do make it so that I when I click on Button1 the program goes to Function.cpp to calculate 1+1..and then displays the result "2" in the Edit Control?  I plan on adding more complex calculation functions and using more variables soon...so I would like to use classes in the solution.  
// FirstAppDlg.cpp
void CFirstAppDlg::OnBnClickedButton1()
{
 UpdateData( TRUE ); // get text from edit control into 'm_strEdit1'
 m_strEdit1 = intToString( addInts( 1, 1 ) );
 UpdateData( FALSE ); // put the text from 'm_strEdit1' to the edit control
}


// function.h
#pragma once

int      addInts( int x, int y );
CString  intToString( int x );


// function.cpp
#include "stdafx.h"
#include "function.h"

int addInts( int x, int y )
{
 return x + y;
}

CString intToString( int x )
{
 CString strText;
 strText.Format( "%d", x );
 return strText;
}

Open in new window

0
Comment
Question by:shaolinfunk
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
2 Comments
 
LVL 35

Accepted Solution

by:
sarabande earned 2000 total points
ID: 35026182
if you want classes, you may add

class Calculator
{
public:
     int add(int operand1, int operand2);

};

to functions.h.

implement Calculator::add in functions.cpp

then you would use it like

void OnBtnClicked()
{
    // get both values to add and convert to integer
   ...

   Calculator calc;
   int r = calc.add(operand1, operand2);

   // convert r to string and show it on screen
   ...

Sara
}
0
 
LVL 1

Author Closing Comment

by:shaolinfunk
ID: 35026262
Many, many thanks!  I see what you did there....and I got my program to compile on the 1st try.  Thanks for helping me create my first working class!
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What is C++ STL?: STL stands for Standard Template Library and is a part of standard C++ libraries. It contains many useful data structures (containers) and algorithms, which can spare you a lot of the time. Today we will look at the STL Vector. …
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question