Link to home
Start Free TrialLog in
Avatar of shaolinfunk
shaolinfunkFlag for United States of America

asked on

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

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?
void CFirstAppDlg::OnBnClickedButton1()
{
 UpdateData( TRUE ); // get text from edit control into 'm_strEdit1'
 // modify 'm_strEdit1' here just for testing
 m_strEdit1 = "2";
 UpdateData( FALSE ); // put the text from 'm_strEdit1' to the edit control
}

Open in new window

Avatar of shaolinfunk
shaolinfunk
Flag of United States of America image

ASKER

I realize it will take multiple steps and questions to help me out with my ultimate goal..so I will post 1 question at a time in what is likely to be a string of questions.  
Avatar of Zoppo
Hi shaolinfunk,

I think how to continue may be dependant on what's your goal. Writing a parser for even simple mathematical expressions can be difficult and lot of effort.

If it's an option for you I would suggest you try to use a math-parser library you can download from the internet - I just found three within some minutes with google, I guess there can be found more:

http://warp.povusers.org/FunctionParser/
http://www.bestcode.com/html/math_parser_for_cpp.html
http://www.codeproject.com/KB/recipes/MathieuMathParser.aspx

A problem is they may be some kind of overkill for your needs, but you have to decide this for yourself ...

ZOPPO
Hmm...what is the parser for?  I just literally want to do the calculation below into Functions.cpp...and have it spit out only the result.

int nFirst;
int nSecond;
int nSum;
nFirst = 1;
nSecond = 1;
nSum  = nFirst + nSecond;

How do I display nSum onto the Edit Control that we discussed in the last question?
My project is one of those do-it-yourself, teach yourself, beginner projects...where everything is really, really simple.  I think what you're suggesting above with the parser is beyond the scope of what I'm trying to do.
Put another way, the program doesn't take ANY input from the user, except the pushing of the button.  I just want to see the connection between how to output a value to the dialog, and how to get the calculation done in a .cpp file that is different from the ones created when I originally created the project.
ok, here's as simple example which might help you get started a function which adds two integers and another one which creates a string from an integer and a sample how you call it from your button event handler - I don't now if you would like to have it in a class or something; I just put it in its own namespace since a class up to now IMO makes no sense.

// function.h
#pragma once

namespace function
{
 int      add( int x, int y );
 CString  toString( int x );
}

Open in new window


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

namespace function
{
 int add( int x, int y )
 {
  return x + y;
 }

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

Open in new window


void CFirstAppDlg::OnBnClickedButton1()
{
 using namespace function;
 UpdateData( TRUE ); // get text from edit control into 'm_strEdit1'
 m_strEdit1 = toString( add( 1, 1 ) );
 UpdateData( FALSE ); // put the text from 'm_strEdit1' to the edit control
}

Open in new window



This is just one very simple sample - there are at least a dozen of ways to implement this, and it's hard for me to distinguish which one might be exactly what you need/like ...

ZOPPO
OK Is there a way to do this without using "namespace function"?  I am not familiar with this concept at all.  I have finished the chapter in my book on classes so I am familiar with variables, functions, and classes...but not namespace.

I just read: http://www.cprogramming.com/tutorial/namespaces.html
to get a handle on namespaces, but would like to stick the with the traditional way of doing this in a class so that I can build on it and later add more and more functions in the Functions.cpp.  

Shall I create a new question post for that?  Just let me know.
ASKER CERTIFIED SOLUTION
Avatar of Zoppo
Zoppo
Flag of Germany image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Ok, thanks.  I spent some time adding some lines...I included "Function.h" in FirstAppDlg...i'm down to 1 last error...

For this line:

 CString strText;
 strText.Format( "%d", x );

I get this error:

1>c:\documents and settings\administrator\desktop\firstapp\firstapp\function.cpp(12) : error C2664: 'void ATL::CStringT<BaseType,StringTraits>::Format(const wchar_t *,...)' : cannot convert parameter 1 from 'const char [3]' to 'const wchar_t *'

What is the include line that I need to type to get CString to work?
I tried #include <afx.h> in Function.cpp but that didn't work.
ok, that's because your project is built using UNICODE - sorry, I didn't take care about this - just change the line to be like this:

strText.Format( _T( "%d" ), x );

Open in new window


ZOPPO
No need to include <afx.h> anew - it's already included via 'stdafx.h' ...
#include <string>

-saige-
Ah yes UNICODE.  I just left your code as is...and went to Project properties, Config properties, General, choose 'Multibyte Character Set" instead of "UNICODE"...and then your code worked!  And I see what you did...Thanks so much!
Well, yes, with UNICODE this is one thing to take care of - but you can keep UNICODE and use _T( ... ) for each string literal, then it's possible to compile the same code with and without UNICODE.

@it_saige: sorry, but your comment is unrelated to this problem since the code doesn't use std::string - it's MFC with CString/LPSTR ...
@Zoppo - I had noticed that after I had posted.  :(

-saige-
:o)

no problem ...