-
Convert to int
hello
can anybody say the code required to convert a string to integer datatype
that is get a number as a string type and convert it integer
ex
char num[10];
cout<<"Enter a number: ";
cin>>num;
// convert num to int datatype so that arithmetic calcs is possible
-
-
A char can hold a small integer type, but captainMorgans link is useful to you
-
it puzzles me why dont you start with int from the beginning and save yourself the trouble of converting? because "cin" can take int input also...
-
Because forum members keep recommending doing it the way he does it?
-
That is simply wasteful of time and resources. Just start out with an int. If for some reson you need to use C style strings, there is a nonstandard function atoi that will convert for you.
-
I think strtol() is better...
-
> it puzzles me why dont you start with int from the beginning and save yourself the trouble of converting?
Because functions which do input AND conversion have such complicated error recovery procedures (which almost nobody follows, and which noobs certainly don't).
Reading the input into a string, then attempting the conversion allows you to separate input errors from conversion errors.