Page 1 of 1

Form problem...

Posted: Sat Feb 25, 2006 10:26 pm
by rocco.g
Hi!
I'm italian, so i'm sorry for my English.

I did a program that add the string written in the first form at the second one and show the result in the third form when the button is pressed.

But there is a little problem, to do this i used this function:


Code: Select all

    WxButton1 = new wxButton(this, LboxTest_Add, wxT("Somma"), wxPoint(170,124), wxSize(75,25), 0, wxDefaultValidator);
    
    WxEdit2 = new wxTextCtrl(this, LboxTest_AddText, wxT("Num 1"), wxPoint(20,30), wxSize(121,21), 0, wxDefaultValidator );
    
    WxEdit1 = new wxTextCtrl(this, ID_WXEDIT1, wxT(""), wxPoint(20,80), wxSize(121,21), 0, wxDefaultValidator );

    WxEdit3 = new wxTextCtrl(this, ID_WXEDIT3, wxT("Num 2"), wxPoint(20,55), wxSize(121,21), 0, wxDefaultValidator );

}

void Project1Frm::Project1FrmClose(wxCloseEvent& event)
{
    Destroy();
}
 
 
void Project1Frm::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
{
    wxString s = WxEdit2->GetValue();
    
   
    
    wxString a = WxEdit3->GetValue();
  

    WxEdit1->AppendText(s+a);
}
but i would like to sum the integer typed in the form 1 with the integer typed in the form 2 and to show the result in the third form and not to use the strings... what i have to do in order to sum or do some other mathematical operations instead to use string characters?

Can u help me?
i searched on google and on the ufficial wx reference, but i didn't find anything usefull...

please help me...

thanks and sorry for my English...

Posted: Sun Feb 26, 2006 2:10 am
by Sof_T
Have you looked at using wxString::ToDouble or ::ToLong? To convert the strings into floating point or integer numbers. Then multiply them and put the final value back into the editbox?

Sof.T

Posted: Sun Feb 26, 2006 12:03 pm
by rocco.g
hi! thanks for ur answer!

i already tried to use this function:
wxString::ToLong bool
ToLong(long *val, int base = 10) const


but AppendText works with the strings and not with the integer, so i have always the string and not the integer...

i tried to convert the first string s, by doing this:


Code: Select all

wxString s = WxEdit2->GetValue();
    
    bool ToLong(long *s, int base = 10);
   
    
   // wxString a = WxEdit3->GetValue(); disabled for this test
  

   // WxEdit1->AppendText(s+a);  disabled for this test
    
    WxEdit1->AppendText(s);
but it displays always only the string... and do not do the sum if i convert also the second string... it do only the sum of the two strings...

:cry:

Re: Integer and Maths

Posted: Sun Feb 26, 2006 12:43 pm
by elmo

Code: Select all

 
void Project1Frm::OnButtonAdd(wxCommandEvent& WXUNUSED(event))
{
    long b,c;

    wxString s = WxEdit2->GetValue();
    wxString a = WxEdit3->GetValue();
  
    s->ToLong(&b);
    a->ToLong(&c);

    WxEdit1->AppendText( wxString::Format(wxT("%ld"), b+c) );
}

Posted: Sun Feb 26, 2006 12:54 pm
by Sof_T
Just posting a reply when I noticed Elmo got there first :lol:

Can't better his solution, it is very neat :wink:

Sof.T

Posted: Sun Feb 26, 2006 12:55 pm
by rocco.g
thanks!!!

now it works!

but i had to use this:

Code: Select all

s.ToLong(&b); 
a.ToLong(&c); 
it works correctly... but it is correct?

Posted: Sun Feb 26, 2006 1:44 pm
by Sof_T
s.ToLong(&b);
a.ToLong(&c);
Yes it is correct. I think it was a typing error on elmo's part.

It is possible to leave out the assignment to temporary strings like so:

Code: Select all

    long b,c;

    WxEdit2->GetValue().ToLong(&b);
    WxEdit3->GetValue().ToLong(&c);

    WxEdit1->AppendText( wxString::Format(wxT("%ld"), b+c) );
Sof.T

Posted: Sun Feb 26, 2006 2:03 pm
by rocco.g
ok!!!

thanks a lot to all!!!


really... thanks!!!

:wink:

Posted: Wed Mar 01, 2006 5:50 pm
by elmo
Sof_T wrote:Yes it is correct. I think it was a typing error on elmo's part.
Yes, my mistake. I almost always create new instances with new operator and work on pointers.

Posted: Thu Mar 23, 2006 2:01 am
by MACKRACKIT
I am lerning also. I think I understand with the exception of how do you get 1.2 + 1.2 = 2.4 type of output?

I played around with double and float instead of long. Double gives strange answers and I can not get float to compile.

Also, I do not understand

(wxT("%ld")

I have read about wxT in the help files, but can not find "%ld"

Thanks

Posted: Thu Mar 23, 2006 10:53 am
by tbreina
Also, I do not understand

(wxT("%ld")
The "d" means integer (aka type int). The "l" modifies it to a long integer (aka type long). By long integer, I mean that if a normal integer has a maximum value of 2^31 -1 (32 bits), then a long integer has a maximum value of 2^63 -1 (64 bits).

For double and float to work, you need to change the output string to

wxT("%f") for float (32 bits) and wxT("%lf") for double (which is a long or double-sized float -- usually 64 bits).


This is standard C/C++ format and should be taught the first day of any intro to programming course (or any intro to C/C++ book on the market).

Do a google search for "printf C" and you'll see the %d, %f, %s format definitions.

-Tony

Posted: Thu Mar 23, 2006 10:47 pm
by MACKRACKIT
I thank you for your answer.

I did not know where to look as I have not had a course in C or C++. I do have two books and one of them does not mention "printf".

I am a forty something electrical engineer trying to learn C/C++. Have been using different flavors of basic for years though. Most likely will not go back to school.

Do you recomend a good book or two??

Thank you and every one else for your help.

Posted: Fri Mar 24, 2006 5:39 am
by tbreina
http://www.cplusplus.com/ref/cstdio/printf.html

Here's a good resource for formatting strings.

-Tony

Posted: Fri Mar 24, 2006 9:51 pm
by MACKRACKIT
Thank you,
Dave

Posted: Fri Mar 24, 2006 11:54 pm
by emarti
For string to integer, you may use wxAtoi.