Basic doubt

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Basic doubt

 
1
  #1
Sep 15th, 2006
I'm doing a dialog application in C++ to calculate something, but I want the result to be with 2 decimal number, how to do this?

here is the function:

  1. void CCalculatorDlg::Oncalc()
  2. {
  3. // TODO: Add your control notification handler code here
  4.  
  5. UpdateData(TRUE);
  6.  
  7. if ((m_amostras >= 1) && (m_elementos >= 1) && (m_click == true))
  8. {
  9. if (m_interno)
  10. m_total = (19.8522+(3.99038*(m_elementos-1))+((1.74579+((m_elementos-1)*0.1496))*(m_amostras-1))) + (2.5*m_preparacao);
  11. else if (m_externo)
  12. {
  13. if (m_amostras < 11)
  14. m_total = (19.9519+(19.9519*(m_elementos-1))+((19.9519+((m_elementos-1)*2.494))*(m_amostras-1)))+(5*m_preparacao);
  15.  
  16. if (m_amostras > 10)
  17. m_total = (19.9519+(19.9519*(m_elementos-1))+(((19.9519+((m_elementos-1)*2.494))*(9))+((9.98+((m_elementos-1)*2.494))*(m_amostras-10))))+(5*m_preparacao);
  18. }
  19. }
  20. else
  21. {
  22. if ((m_amostras < 1) || (m_elementos < 1))
  23. MessageBox("Tem que especificar pelo menos uma amostra e um elemento!");
  24.  
  25. if (!m_click)
  26. MessageBox("Tem que especificar o tipo de cliente!");
  27. }
  28.  
  29. UpdateData(FALSE);
  30.  
  31. }

I want the variable m_total to show with 2 decimal numbers, like, p.e. x,yz.

Thanks
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 147
Reputation: Grunt has a spectacular aura about Grunt has a spectacular aura about 
Solved Threads: 12
Grunt's Avatar
Grunt Grunt is offline Offline
Junior Poster

Re: Basic doubt

 
1
  #2
Sep 15th, 2006
The key to eliminating bugs from your code is learning from your mistakes.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: Basic doubt

 
0
  #3
Sep 15th, 2006
Originally Posted by Grunt View Post
Look in setprecision manipulator
But I'm not using cout... The variable m_total is shown at the dialog box in an edit box...
Last edited by gampalu; Sep 15th, 2006 at 8:13 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Basic doubt

 
0
  #4
Sep 15th, 2006
you will have to format the string before it is displayed in the dialog box. If you are using MFC, then use CString's Format() method, for exaple
  1. CString text;
  2. float number = 12.123456;
  3. text.Format("%.2f", number);
  4. GetDialogItem(IDC_EDI1)->SetWindowText(text);


If you are not using MFC, then use standard C sprintf() function
  1. char text[20];
  2. float number = 12.123456;
  3. sprintf(text,"%.2f", number);
  4. GetDialogItem(IDC_EDI1)->SetWindowText(text);
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: Basic doubt

 
0
  #5
Sep 15th, 2006
I'm using MFC and I tried your exmeple but I get this:

  1. --------------------Configuration: calculator - Win32 Release--------------------
  2. Compiling...
  3. calculatorDlg.cpp
  4. D:\vários\mail\calculator\calculatorDlg.cpp(201) : error C2065: 'GetDialogItem' : undeclared identifier
  5. D:\vários\mail\calculator\calculatorDlg.cpp(201) : error C2227: left of '->SetWindowTextA' must point to class/struct/union
  6. Error executing cl.exe.
  7.  
  8. calculator.exe - 2 error(s), 0 warning(s)

Can you figure out why?
thanks
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,358
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1463
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Basic doubt

 
0
  #6
Sep 15th, 2006
Sorry about the mistake. Should have been GetDlgItem

Alternatively, if you used ClassWizard to assign a CString variable to the edit control, then you don't need to call GetDlgItem() but use that variable instead, then call UpdateData(FALSE) to update the edit control.

If you are new to MFC and you are using VC++ 6.0 compiler you might want to read the Microsoft Scribble tutorial, which will show you how to do all that neat stuff.
Reply With Quote Quick reply to this message  
Join Date: Mar 2006
Posts: 78
Reputation: gampalu is an unknown quantity at this point 
Solved Threads: 0
gampalu gampalu is offline Offline
Junior Poster in Training

Re: Basic doubt

 
0
  #7
Sep 15th, 2006
Originally Posted by Ancient Dragon View Post
Sorry about the mistake. Should have been GetDlgItem

Alternatively, if you used ClassWizard to assign a CString variable to the edit control, then you don't need to call GetDlgItem() but use that variable instead, then call UpdateData(FALSE) to update the edit control.
I've used the alternative you gave me and it works
thanks for your attention.

If you are new to MFC and you are using VC++ 6.0 compiler you might want to read the Microsoft Scribble tutorial, which will show you how to do all that neat stuff.
This link it's in my bookmarks :cheesy:
thanks again
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC