|
Hi all, I want to round a float/double number (to an integer)so that when its less than number.5 it is rounded down and when its greater than or equal to number.5 round up. At the moment I have discovered ceil and floor, but these are no good, as it is an either or solution. VB has a dedicated round function, is there one in VC++?
cheers all
Alan.
"When I left you I was but the learner, now I am the Master" - Darth Vader
|
|
|
|
|
Why don't you use sprintf? Here's two functions. One returns an int (after rounding), and the other returns a double after rounding to the nPrecision'th decimal point.
int _rounder(double n)
{
char st[16];
sprintf(st, "%.0lf", n);
return atoi(st);
}
double _rounder(double n, int nPrecision)
{
char st[16];
sprintf(st, "%.*lf", nPrecision, n);
return atof(st);
}
"...the staggering layers of obscenity in your statement make it a work of art on so many levels." - Jason Jystad, 10/26/2001
|
|
|
|
|
As far as I know, there's no "round" function, but here's a modified version of the one I use:
#include < math.h >
double round(double dValue, unsigned uDecimalPlaces)
{
dValue *= dValue;
dValue += 0.5;
dValue = floor(dValue);
return (dValue / pow(10.0, uDecimalPlaces));
}
Regards,
Alvaro
|
|
|
|
|
Thanks for the responses guys! You solved my problem! All I needed to do was add 0.5 on and then floor it because that would, in effect round it exactly how I want it.
Many, many thanks
Alan.
"When I left you I was but the learner, now I am the Master" - Darth Vader
|
|
|
|
|
Hi, everybody, i need some help for writing an operator=
:::::::::::::::::::::::::::::::
class aa
{
public:
int var;
virtual aa& operator=(const aa && c ) throw();
};
:::::::::::::::::::::::::::::::
class bb : public aa {
public:
int varbb;
bb& operator=(const bb && c )
{
varbb=c.varbb;
return *this;
};
};
int main()
{
aa * xx = new bb();
aa * yy = new bb();
*xx=*yy;
};
Problem:
Ok, so when i execute this code "*xx=*yy" the aa operator= was called, but i want to use the bb operator=.
How can i do that?
Ho, sorry for my english.
|
|
|
|
|
In that case you must declare the operator in class bb the same way than
in class aa. Did you notice that they are not compatible?
They both must be virtual. (In fact, that's the first time I saw someone
using virtual operator but hey, it's good to know. Thanks).
|
|
|
|
|
Since the bb operator is not declared exactly as the aa operator, they're not treated polymorphically. In other words, they appear as two different functions and the compiler uses the one that most closely matches.
Regards,
Alvaro
|
|
|
|
|
Ok , thank's a lot
But do you know the solution for my problem.
|
|
|
|
|
The solution is to add another assignment operator inside bb that looks exactly like the one in aa. Then you can call bb operator from there by casting the parameter to bb&:
virtual aa& operator=(const aa& c) throw()
{
return operator=((bb&)c);
}
Regards,
Alvaro
|
|
|
|
|
* reinterpret_cast<bb *>(xx) = * reinterpret_cast<bb *>(yy);
Bomb our homes and threaten our children, and, as difficult as it is, we will still love you --- Martin Luther King, Jr.
|
|
|
|
|
Thank's, but sometimes i don't know the real type of xx and yy, and i dont want to know.
I don't write this code with a switch.
Other idea ?
|
|
|
|
|
Can operators even be virtual? You certainly can't make = virtual, because = always operates on a object, not a pointer to an object, as is required for polymorphism.
--Mike--
http://home.inreach.com/mdunn/
Help! Help! I'm being repressed!!
your with and
Sonork - 100.10414 AcidHelm
|
|
|
|
|
Hello experts,
Well,I have a VC++ 6.0 developed application, supporting MBCS, which is the default for VC++ 6.0. Also, let me tell you I am using Windows 2000 platform.
Then, I have compiled it with UNICODE MFC dlls and static librarys[ UAFX*.lib and Unicode MFC dlls] and got the UNICODE version of application.
Now, my requirement is to develop one single application that can support both Unicode and MBCS[DBCS]....
Well, I dont know wehether this is possible, if Possible, please let me know how to go for.
Thanks & Regards,
Mr Prasad G.
|
|
|
|
|
While I'm not an expert, I will try to answer your question.
Use the TCHAR macro's for all your string manipulations (CString does this)
ie.
use 'TCHAR' instead of 'char' or 'WCHAR'
use '_tcscpy' instead of 'strcpy', '_mbscpy', or 'wcscpy'
HTH
---
It may be that your sole purpose in life is simply to serve as a warning to others.
|
|
|
|
|
I wrote database program,and my m_pSet(recordset) is snapshot and I can use AddNew() function;when I change m_nDefaultType to dynaset I can't use AddNew() and when I use CanAppend() it does not append!
How can I add a new record to dynaset record?
thanks
|
|
|
|
|
You only can set the type of recordset in the openrecordset. Is for this that the only way is close the recordset an open again, using a dynaset type...
Cheers!!!
Carlos Antollini.
Sonork ID 100.10529 cantollini
|
|
|
|
|
Hi !
I have an app using the Doc-View architecture.
In my View class, I have a method GetDocument().
I would like to be able to access the document from my mainApp class, not from the view. GetDocument() is using the member property m_pDocument, but I cannot use it from my mainApp class. How can I get a pointer to my CDocument class from my App class ?
Thanks for your help !
Jerome
|
|
|
|
|
|
Hello!
How do I get those nice blue/white buttons and dropdown menus that most XP applications use in my MFC app?
|
|
|
|
|
Hi, All !!!
=======
Last time I asked: How to grayscale a picture, and didn't got an answer.
Well...
If you guys wanna know how, there is your answer:
I found out that in order to have a gray color,
all Red,Green & Blue must have the same value.
So...
<code>void</code> GrayscaleIt(MyPicture & pic)
{
for (<code>int</code> y=0; y<pic.height; y++)
for (<code>int</code> x=0; x<pic.width; x++)
{
<code>COLORREF</code> col= pic.GetPixel(x,y);
<code>unsigned char</code> gs= ((GetRValue(col)+GetGValue(col)+GetBValue(col))/3);
pic.SetPixel(x,y, RGB(gs,gs,gs));
}
}
BTW:
I have a question.
After the win32 AppWizard, In order to make my window "transparent" or "hollow",
I need to change the wcex.hbrBackground to 0 ;
Like this:
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEX wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = 0;
wcex.lpfnWndProc = (WNDPROC)WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = LoadIcon(hInstance, (LPCTSTR)IDI_GRAY1);
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
<code> wcex.hbrBackground = NULL;
wcex.lpszMenuName = NULL;
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);
return RegisterClassEx(&wcex);
}
How do I do it in MFC ?????
Please, HELP!
BTW: come to #CodeProject on DalNet (mIRC)
|
|
|
|
|
Take a look at PreCreateWindow, which you can override in your CMainFrame class.
Regards,
Alvaro
|
|
|
|
|
Call AfxRegisterWndClass() and save the returned string. Use that string as your window class name.
--Mike--
http://home.inreach.com/mdunn/
Help! Help! I'm being repressed!!
your with and
Sonork - 100.10414 AcidHelm
|
|
|
|
|
(Visual C++6.0 on OS w2k)
I have the following problem using standard microsoft ActiveX MSComm32 or others:
when I create this object and I run my application in
debug mode there is always a COleException raised:
Warning:_ constructing COleException,
scode=DIPS_E_MEMBERNOTFOUND ($80020003).
I create the object as below:
CString csLicKey = "Copyright (c) 1994 ";
BSTR bstrLicKey = csLicKey.AllocSysString();
if(!m_CommCtrl.Create("Communications Control",
WS_CHILD, CRect(0,0,40,40), this, IDC_COMMCTRL, NULL,
FALSE, bstrLicKey))
I couldn't trap this exception. And I couldn't discover
why it was raised.
Someone knows something about this issue?
|
|
|
|
|
Assuming you mean DISP_E_MEMBERNOTFOUND and not DIPS_E_MEMBERNOTFOUND.
MSDN states the following,
Debugging Output You Can Safely Ignore
When you're debugging a control, you may get a series of messages in Visual C++'s debugging window. Although it's not documented anywhere, some of these messages can be safely ignored. For instance, when I debug the StopLite control in the test container using Visual C++ version 4.2, I get the following:
Loaded symbols for 'C:\WINNT35\system32\MFC42.DLL'
LDR: WARNING ! MAJOR PERFORMANCE LOSS in TSTCON32.EXE
LDR: Dll MFC42D.DLL base 5f400000 relocated due to collision with
C:\WINNT35\System32\MFC42.DLL
Loaded symbols for 'C:\WINNT35\system32\MFC42D.DLL'
Loaded symbols for 'C:\WINNT35\system32\MSVCRTD.DLL'
Loaded symbols for 'C:\WINNT35\system32\MFCO42D.DLL'
Warning: constructing COleException, scode = DISP_E_MEMBERNOTFOUND ($80020003).
First-chance exception in TSTCON32.EXE (MFCO42D.DLL): 0xE06D7363: Microsoft C++ Exception.
The first line is just notification that the debugging symbols for the release version MFC DLL were loaded when the test container, which uses the release version of the MFC DLL, was loaded.
The second and third lines (which begin with "LDR:") warn that the debugging version of the MFC DLL (used by the StopLite control) had to be relocated in my address space because it conflicted with the release version of the same DLL (which is being used by the test container). Although this means that it took a little longer to load the MFC DLL, it isn't a big deal because it only happens when you're debugging in the test container. You can ignore these messages. (Since Visual Basic apps don't use the MFC DLL at all, you won't get this message if you use a Visual Basic app as your test container.)
The next three lines are notifications that debugging symbols have been loaded.
The last two lines are notification that an exception has been thrown because the test container didn't provide a Color property to be read. This is okay, because we've provided a default value for the property. You won't get this error if you use a container that provides the property, such as a Visual Basic application.
In Visual C++ version 4.1, the only line I got was a warning about the exception for properties that couldn't be loaded from the container.
First-chance exception in TSTCON32.EXE (MFC40.DLL): 0xE06D7363: Microsoft C++ Exception.
Depending on how your container handles threads, you may also get occasional messages about threads exiting. You don't need to worry about these, either.
Michael
|
|
|
|
|
|