Categories: MSDN / DotNet / Java / Scripts / Linux / PHP Ask - La ask - La Answer

MFC CString to integer weird problems

Hi there!

I need help with conversion from CString to int.
I've already tried the _ttoi function, but it returns
characters (such as: a, d, G, ? ,( etc...). What's the problem.
I use MS VC++ 6.0 compiler. here's the bit of the code causing problems.
I'm trying to get a percent of two values.

void MyBaseClass::OnMyMessage()
{
CString str1;
CString str2;
int i1;
int 12;
int percent;
str1=("100");
str2=("1000");
i1 = _ttoi(str1);
i2 = _ttoi(str2);
percent = ((i1) / (i2) * 100);
output.SetPos(percent);
}
[611 byte] By [DragonArt] at [2007-11-11 7:44:40]
# 1 Re: MFC CString to integer weird problems
http://forums.dev-archive.com/showthread.php?t=149184

this might help
rssmps at 2007-11-11 21:02:01 >
# 2 Re: MFC CString to integer weird problems
The problem here is that the OP uses CString. I doubt that stringstream object can handle CString arguments. So you should first convert CString to a char array, and then apply the other methods explains in the previous thread. Also, make sure that i1 and i2 have the correct values after their initialization.
Danny at 2007-11-11 21:03:01 >
# 3 Re: MFC CString to integer weird problems
Sorry, I wrote it wrong. The output (ProgressBar::SetPos()) should use int for input type. When I try to get the percent value with MessageBox ( MessageBox(static_cast<CString>(percent)); ) I get blank only. When I try to get the values of the two CStrings, i get correct values. With ints the result is characters only. Someone, please modify the attached code.
DragonArt at 2007-11-11 21:04:02 >
# 4 Re: MFC CString to integer weird problems
The i1 and i2 has values of NULL after their initialization
DragonArt at 2007-11-11 21:05:01 >
# 5 Re: MFC CString to integer weird problems
First of all, static_cast can't be used like this. It can't convert a string object to int. You need to extract the char * respresentation first. That's why I prefer to use std::string and not the Microsoft proprietary hacks, whose age is really showing. So try to convert the string to a char * variable, and then convert that char * to an int using one of the techniques mentioned in the previous thread.
Danny at 2007-11-11 21:06:00 >
# 6 Re: MFC CString to integer weird problems
Don't see what's wrong with CString (I found, it's one of the best class if you work with MFC). You can always do a casting like (LPCTSTR) str1.
hprasetya at 2007-11-11 21:07:06 >
# 7 Re: MFC CString to integer weird problems
try using this DragonArt

CString String_Name_X,String_Name_XX; \\string name setup
int x = 10; \\interger setup
long double xx= 10.01;\\long double setup

String_Name_X.Format("%d",x);\\CString to int
int x_string_int = atof(String_Name);\\int now equals string

String_Name_XX.Format("%d",xx);\\CString to int
long double xx_string_double = atof(String_Name_XX);\\int now equals string

String_Name_X.Format("%d", String_Name_X);\\CString to display int
String_Name_XX.Format("%4.2f",String_Name_XX);\\CString to display int

MessageBox("The output of the int is: " + String_Name_X + "\nThe output of the long double is: " + String_Name_X);
scorpion at 2007-11-11 21:08:10 >
# 8 Re: MFC CString to integer weird problems
Microsoft OLE DB Provider for ODBC Drivers (0x80040E14) [Microsoft][ODBC SQL Server Driver][SQL Server]Line 1: Incorrect syntax near '='.

<%@LANGUAGE="VBSCRIPT"%>
<%
const action_add="add"
const action_view="view"
const action_delete="del"
const action_edit="edit"
const validate_yes="y"
const norecordselected=-9999
%>
<html>
<LINK rel=stylesheet href=menu.css type=text/css>
<script language=JavaScript src=content1.js></script>
<script language=JavaScript src=menu.js></script>

<body topmargin="0" leftmargin="0">

<object classid="clsid:D27CDB6E-AE6D-11cf-96B8-444553540000" codebase="http://download.macromedia.com/pub/shockwave/cabs/flash/swflash.cab#version=6,0,29,0" width="780" height="110">
<param name="movie" value="toplinks.swf">
<param name="quality" value="high">
<embed src="toplinks.swf" quality="high" pluginspage="http://www.macromedia.com/go/getflashplayer" type="application/x-shockwave-flash" width="780" height="110"></embed></object>
<br>
<div id="Layer1" style="position:absolute; width:313px; height:22px; z-index:1; left: 461px; top: 112px;">
<div align="right"><font color="#00FFFF" face="Comic Sans MS">Bangalore Division</font></div>
</div>
<img src="pagehead.gif" width="780" height="28"> </body>
<!--#include file="database.asp"-->
<!--#include file="adovbs.inc"-->
<%
'Main Processing loop
sub main()
dim straction
dim blnisvalidation
dim stn
%>
<body>
<h5 align="right">
<%
response.write "<a href=""list.asp""><font face='Arial' size='2' color='blue'>Back</a>"
%> -
<a href="login.asp?out=logout"><font face="Arial" size="2" color="blue">Logout</a>
</h5>
<%
tn=request("tn")
set objrs=server.CreateObject("adodb.recordset")
strsql="select * from sbcswrtds where tn=" & tn [B] --> Error comes here
objrs.open strsql,objconn,3,3
%>

<table align="center">
<tr><td height="25"><font size=3 face="Verdana, Arial, Helvetica, sans-serif" color="blue"><b>Are you sure to delete "<%=objrs("tn")%>"</b></font></td></tr>
</table>

<form name="form1" method=post action="deletetn1.asp" onSubmit="return validate();" >
<p>
<table align="center">
<tr>
<td></td>
<td height="80">
<input type="submit" name="submit1" value="Delete" ></td></tr>
</table>
</p>
</form>

<h5 align="right">
<%
response.write "<a href=""list.asp""><font face='Arial' size='2' color='blue'>Back</font></a>"
%> -
<a href="login.asp?out=logout"><font face="Arial" size="2" color="blue">Logout</font></a>
</h5>
<%
end sub
'Code begins executing here
call main
%>
</body>
</html>
thirulok at 2007-11-11 21:09:03 >