Dividing error

This is a discussion on Dividing error within the C++ Programming forums, part of the General Programming Boards category; Hi everyone, I have the following code which is not working (always gives an output of zero): Code: int len ...

  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    128

    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.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    128
    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!).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Winsock problem
    By Wolf` in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2010, 05:55 PM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 09:18 PM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Connecting to a mysql server and querying problem
    By Diod in forum C++ Programming
    Replies: 8
    Last Post: 02-13-2006, 10:33 AM
  5. Couple C questions :)
    By Divx in forum C Programming
    Replies: 5
    Last Post: 01-28-2003, 01:10 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21