I'm new to c / c BUT not new to programing, I know php vary well. Well, on to my question. I'm writing a physics calc, the user enters a Vi and Angle and it gives them the rest of the info, my php version. I got my c one to work BUT it cuts everything past the decimal off... If some one could show me how to get it to not cut them off if it is I dint know what I would do.

case IDC_GO:
{
	float y, x, h, t, j, l;
	float pi = 3.14159265359;
				
	float vi = GetDlgItemInt(hwnd, IDC_VITEXT, NULL, FALSE);
	float a = GetDlgItemInt(hwnd, IDC_ANGLETEXT, NULL, FALSE);

	a = ( a / 1 ) * ( pi / 180 );

	y = vi * sin(a);
        x = vi * cos(a);
			
	h = -y / -9.81;
	t = h * 2;
			
	j = ( y * h ) + ( -4.905 * pow(h,2) );
	l = x * t;
	
	a = ( a ) * ( 180 / pi );

	SetDlgItemInt(hwnd, IDC_VITEXT, vi, TRUE);
	SetDlgItemInt(hwnd, IDC_ANGLETEXT, a, TRUE);
	SetDlgItemInt(hwnd, IDC_VXITEXT, x, TRUE);
	SetDlgItemInt(hwnd, IDC_VYITEXT, y, TRUE);
	SetDlgItemInt(hwnd, IDC_TIMETEXT, h, TRUE);
	SetDlgItemInt(hwnd, IDC_FULLTIMETEXT, t, TRUE);
	SetDlgItemInt(hwnd, IDC_HEIGHTTEXT, j, TRUE);
	SetDlgItemInt(hwnd, IDC_LENGTHTEXT, l, TRUE);
}
break;

a link the the .exe

Dani AI

Generated

Short version: GetDlgItemInt and SetDlgItemInt only work with integers, so any fractional part is discarded. ’s tip about iomanip and setprecision is correct for stream output (cout/ostringstream), but it doesn’t change how the Win32 dialog helpers convert values. That is why saw decimals vanish.

A practical pattern that keeps decimals and gives you control over formatting:

  • Read the control text as a string (not with GetDlgItemInt).
  • Convert that string to a floating value (strtod/_ttof/std::stod or equivalent).
  • Do your calculations using float/double.
  • Format the result into a string with the precision you want (snprintf/_stprintf_s or an ostringstream with fixed + setprecision).
  • Put the formatted string back into the control with SetDlgItemText.

Troubleshooting tips and gotchas: if GetDlgItemInt always returns zero (as reports), check that you’re using the correct dialog HWND and control ID, that the control exists at the time you call it, and that the control text is actually a valid integer (decimal points or non-digits will cause conversion to fail). When using GetDlgItemInt, pass a BOOL* for the translated flag and check it — it tells you whether the conversion succeeded. Also be mindful of Unicode vs ANSI builds (use the TCHAR/_t... family or wchar/string APIs so your code works in both).

A few small recommendations: prefer double for visibility of small decimals, use locale-aware parsing if your app will run in locales that use commas as decimal separators, and use safe formatting functions (_s variants or ostringstream) to avoid buffer overruns. ’s _gcvt + SetDlgItemText fix works; the pattern above is just a clearer, more portable way to handle fractional input and formatted output.

I would say that you just need to set the precision and/or the fixed notation. The setprecision and fixed manipulators can be set and all floating point output will use this format until you choose different formats. So, if you have the following:

double Pi = 3.1429
cout << setprecision(2) << fixed;
cout << Pi << endl;
cout << Pi << endl;
cout << setprecision(4);
cout << Pi << endl;

The output should look like this:
3.14
3.14
3.1429

Other options to fixed notation include scientific, etc. To use these format manipulators, you will need to include <iomanip>. Older compilers may not recognize cout << fixed even with this file included. If this happens, use
cout << setiosflags(ios::fixed);
setprecision will not affect integers so, watch for integer division. I cannot get to your exe code, but the lack of decimal digits could be because an output for an int / int will be an int. A solution for this is to multiply the result by 1.0 before outputting. Hope this helps.

well... I dont think that useing cout is going to work when im inclueding windows.h and math.h right now. I think what needs to be done is find a way a can get the SetDlgItemInt to "setprecision(10)" or somthing like that.

I got it to work!

Fix:

#define BUFFER 14

TCHAR tchBuffer[BUFFER];

SetDlgItemText(hwnd, IDC_HEIGHTTEXT, _gcvt(j, 13, tchBuffer));
SetDlgItemText(hwnd, IDC_LENGTHTEXT, _gcvt(l, 13, tchBuffer));

Can you tell me clearly about GetDlgItemInt funtion in Window program with Visual studio 2005. I did like above, but returned value is always zero.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.