943,739 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 2294
  • C++ RSS
Nov 30th, 2008
0

CString to Integer / Double

Expand Post »
Hi All,


i have a problem in converting CString to Int/Double,
function i am using is _wtoi , _wtof.....
Here if the CSting value has say "12345"
this works perfectly. But if any other value is say ( Over flow )
" 11111111111111111111111111111" which exceeds
the max size of int then it returns INT_MAX, where as i need the
exact string as int ie 11111111111111111111111111111 in the integer variable. same thing happens in double.

if there is any junk value entered then the ret value is 0,
Please help me in converting the CString to exact int value
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
koushal.vv is offline Offline
35 posts
since Oct 2008
Nov 30th, 2008
0

Re: CString to Integer / Double

There isn't much you can do about overflows, except just discard the bad CString value. It returns INT_MAX because its impossible to put that many digits into an int variable. I'd like to stuff my 100 inch waste into size 36 slacks too, but that's not possible either
Last edited by Ancient Dragon; Nov 30th, 2008 at 1:29 pm.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,950 posts
since Aug 2005
Nov 30th, 2008
0

Re: CString to Integer / Double

Finally i found a way...
When ever any over flow occurs a global variable errno get's set to ERANGE so in the code i have made a check like this

C++ Syntax (Toggle Plain Text)
  1. int ConvertToInt( CString intVal )
  2. {
  3. int ret = _wtoi(intVla);
  4. if((ret == 0) || (errno == ERANGE))
  5. {
  6. // Throw Exception
  7. // I dont know why i have to do this
  8. errno = 0;
  9. // If i dont reset the "errno" then the value will be set to
  10. //ERANGE even if the next string is valid
  11. //I dont know resetting the system variable is correct or not.
  12. // Functionality works for me :)
  13. }
  14. }

Resetting would give any side effects ?
Reputation Points: 10
Solved Threads: 1
Light Poster
koushal.vv is offline Offline
35 posts
since Oct 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: stl/map and string question
Next Thread in C++ Forum Timeline: Memory allocation (checking for success)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC