-
Converting string to int value
I am stuck trying to extract a value from a string and convert it to an int.
The following code is what I have been working with:
char* p = "";
c_ePrts.GetWindowText(m_sPrts);
p == m_sPrts;
szPrts = atoi(p);
I know I am doing something wrong as p ends up with a "" value or 0.
-
Re: Converting string to int value
"Brian Koritko" <res055xr@gte.net> wrote:
>
>I am stuck trying to extract a value from a string and convert it to an
int.
>The following code is what I have been working with:
>
>char* p = "";
>c_ePrts.GetWindowText(m_sPrts);
>p == m_sPrts;
>szPrts = atoi(p);
>
>I know I am doing something wrong as p ends up with a "" value or 0.
1st: "p == m_sPrts;" is NOT doing an assignment, it is testing for 'equality'.
You can use the operator(LPCTSTR) to access the contained nul-terminated
char array. Note: This is actually a 'TCHAR'. You can get bit if you don't
watch out. You should always use TCHAR.h when programming in windows.
2nd: If working with character arrays it is best to use the strtol/strtod
routines. They will return more meaningful errors, allow you to either avoid
or managed embedded non-numeric characters, and are therefore more robust.
3rd: The...
char* p = ""; // declare a pointer assign a const char nul
...
p = <whatever>; // reassign something else.
Is wasteful. Well actually useless. In this case consider waiting to declare
the 'p' until you need it...
char* p(<whatever>); || char* p = <whatever>;
4th: Is "szPrts" an 'int'? _atoi() returns an int.
-
Re: Converting string to int value
"ralph" <nt_consulting32@hotmail.com> wrote:
>
>"Brian Koritko" <res055xr@gte.net> wrote:
>>
>>I am stuck trying to extract a value from a string and convert it to an
>int.
>>The following code is what I have been working with:
>>
>>char* p = "";
>>c_ePrts.GetWindowText(m_sPrts);
>>p == m_sPrts;
>>szPrts = atoi(p);
>>
>>I know I am doing something wrong as p ends up with a "" value or 0.
>
>1st: "p == m_sPrts;" is NOT doing an assignment, it is testing for 'equality'.
>
>You can use the operator(LPCTSTR) to access the contained nul-terminated
>char array. Note: This is actually a 'TCHAR'. You can get bit if you don't
>watch out. You should always use TCHAR.h when programming in windows.
>
>2nd: If working with character arrays it is best to use the strtol/strtod
>routines. They will return more meaningful errors, allow you to either avoid
>or managed embedded non-numeric characters, and are therefore more robust.
>
>3rd: The...
> char* p = ""; // declare a pointer assign a const char nul
> ...
> p = <whatever>; // reassign something else.
>
>Is wasteful. Well actually useless. In this case consider waiting to declare
>the 'p' until you need it...
> char* p(<whatever>); || char* p = <whatever>;
>
>4th: Is "szPrts" an 'int'? _atoi() returns an int.
I have to agree with what you were saying but I was lost... I am new to programming...Thanks
Here is what works:
c_ePrts.GetWindowText(m_sPrts);
const char *p = m_sPrts;
char *remainderPtr;
szPrts = strtol( p, &remainderPtr, 0);
And it gives me the integer I was looking for.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks