Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting int to CString

Status
Not open for further replies.

rdg2cig

Programmer
Sep 16, 1999
22
US
I would like to convert a variable declared as an int to a CString for printing to the screen using a device context. In the code below, f, i, and s are all CStrings and m_windSpan is an int. I would like to print the int to the screen preceeded by the text. Casting the int to a CString does not generate a compiler error but does not produce the proper output. This code is from the OnDraw() function<br>
<br>
void CGuy2View::OnDraw(CDC* pDC)<br>
{<br>
CGuy2Doc* pDoc = GetDocument();<br>
ASSERT_VALID(pDoc);<br>
CString s, f, i;<br>
<br>
f = &quot;Wind Span: &quot;;<br>
i = (CString)m_windSpan;<br>
s = f + i;<br>
pDC-&gt;TextOut(250, 120, s);<br>
}<br>
<br>
I would appreciate help as I have not found anything useful in the Visual Studio documentation.<br>
Thank you.
 
Use the CString Format() member function e.g.<br>
<br>
<br>
f.Format("Wind Span: %d", m_windSpan);<br>
<br>
cpm<br>
<br>

 
I tried the suggestion but it didn't compile. m_windSpan is declared as an integer and Format would not convert it. Would it be better to declare the variable as a UINT? Is there another way to convert the integer to a CString or is there a way to use the device context to print the integer directly?
 
It did work as suggested in SharedPain's reply. I did not initially have the statement structured properly when I tried it. Now I'm off and running again. Thank you for your help SharedPain.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top