-
Dividing error
Hi everyone,
I have the following code which is not working (always gives an output of zero):
Code:
int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT2));
int len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT4));
char* buf;
char* buf2;
buf = (char*)GlobalAlloc(GPTR, len + 1); //Mass
buf2 = (char*)GlobalAlloc(GPTR, len2 + 1); //Radius
GetDlgItemText(hwnd, IDC_TEXT2, buf, len + 1);
GetDlgItemText(hwnd, IDC_TEXT4, buf2, len2 + 1);
Third=G*atof(buf);
Fourth=atof(buf2)*1000;
First=Third/Fourth;
Second=First/1000;
char strFirst[100];
sprintf_s(strFirst,"%0.2f", Second);
SetDlgItemText(hwnd, IDC_TEXT3, strFirst);
I have noticed that when I change "First=Third/Fourth" to "First=Third*Fourth" it produces a non-zero result. What is the problem with using the "/"?
Thanks.
-
You are printing second, which works out to mass/(radius*1000000) and that doesn't surprise me that that value is less than 0.005. (Also if you have any int variables you're in trouble there too.)
-
Ah thanks - I just realised I got my powers wrong (I am using numbers such as 5.97e24 - for some bizarre reason I forgot to include the powers!).