Solved

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

Posted on 2011-03-02
17
439 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?
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

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
  • 8
  • 7
  • 2
17 Comments
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018012
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.  
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35018052
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
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018091
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?
0
Technology Partners: 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!

 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018106
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.
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018117
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.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35018247
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
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018340
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.
0
 
LVL 31

Accepted Solution

by:
Zoppo earned 500 total points
ID: 35018488
No need for a new question - using namespace IMO makes sense to ensure function/variable/class names are unique - i.e. using a function name 'add' may be a problem since it's a common name for lot of functionalities - anyhow, here the same sample without namespace and more descriptive function name:

// function.h
#pragma once

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

Open in new window


// 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



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
}

Open in new window



BTW: I would anyhow suggest to either use namespace or, if it's more common for you to implement the functions as static members of a class since it's generally a good idea to keep related functions 'bundles' somehow.

ZOPPO
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018899
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?
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018924
I tried #include <afx.h> in Function.cpp but that didn't work.
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35018926
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
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35018936
No need to include <afx.h> anew - it's already included via 'stdafx.h' ...
0
 
LVL 33

Expert Comment

by:it_saige
ID: 35018954
#include <string>

-saige-
0
 
LVL 1

Author Comment

by:shaolinfunk
ID: 35018973
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!
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35019031
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 ...
0
 
LVL 33

Expert Comment

by:it_saige
ID: 35019072
@Zoppo - I had noticed that after I had posted.  :(

-saige-
0
 
LVL 31

Expert Comment

by:Zoppo
ID: 35019076
:o)

no problem ...
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Dynamically allocate memory 9 70
Change local server setting in php 6 105
Eclipse Neon and jdk 1.8.0 11 326
Autosar OS Multicore Share Resources confusion ? 2 108
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

733 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