Click here to Skip to main content
       

Managed C++/CLI

 
You must Sign In to use this message board.
Search this forum  
    Spacing  Noise  Layout  Per page   
QuestionConvert string to integer only such that if it is alphanumeric or float it shouldnot return anythingmemberAbinash Mohanty17-Jul-12 3:13 
Hi All,
 
I want to covert a string to integer.Consider the string as char str[10]; and integer as int num; My case is as following:-
1) Suppose str = "10"
num should be = 10
2) Suppose str = "10.88"
num should be 0 or any invalid number so that we can identify that its not an integer but any other value.
3) Suppose str = "10ab"
num should be 0 or any invalid number so that we can identify that its not an integer but any other value.
 
Is there any direct API by which we can identify the above cases. I have tried atoi() but it returns the remaining integer value after removing the float or alphanumeric values.
Or
I have to try it by creating my own method.
GeneralRe: Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything [modified] PinmemberJohn Schroedl17-Jul-12 3:29 
Since this is the C++/CLI forum, I'll give you a Managed reply -- use the TryParse static method on Int32 and/or Double classes.
 
int val;
String^ s = "10.22";
if (System::Int32::TryParse(s,val)) 
    // parsed as Int32.
else
    // try something else...
 
John

modified 17-Jul-12 10:24am.

GeneralRe: Convert string to integer only such that if it is alphanumeric or float it shouldnot return anything PinmemberAbinash Mohanty17-Jul-12 18:13 
Thanks John for your reply. I am sorry I posted this thread in C++/CLI forum. Actually I was looking for a solution in C++/MFC.
 
Sorry for the inconvinience.
 
Cheers,
Abinash

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   


Advertise | Privacy | Mobile
Web04 | 2.6.130617.1 | Last Updated 18 Jun 2013
Copyright © CodeProject, 1999-2013
All Rights Reserved. Terms of Use
Layout: fixed | fluid