By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,778 Members | 1,997 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,778 IT Pros & Developers. It's quick & easy.

string to int

P: n/a
Hi to all,
Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321

Dec 10 '05 #1
Share this Question
Share on Google+
8 Replies


P: n/a
"shan" <sr**********@gmail.com> writes:
Hi to all,
Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321

int i = atoi("321");

--
Chris.
Dec 10 '05 #2

P: n/a
Chris McDonald wrote:
"shan" <sr**********@gmail.com> writes:

Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321


int i = atoi("321");


strtol() would be better, atoi() has poor error handling
--
Nick Keighley

Dec 10 '05 #3

P: n/a
Chris McDonald wrote:

"shan" <sr**********@gmail.com> writes:
Hi to all,
Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321

#include <stdlib.h>
int i = atoi("321");


--
pete
Dec 10 '05 #4

P: n/a

shan wrote:
Hi to all,
Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321


#include <stdlib.h>
#include <ctype.h>
....
char *chk;
int val = (int) strtol("321", &chk, 10);
if (!isspace((int) *chk) && *chk != 0)
{
/* input string contains invalid character */
}

Dec 10 '05 #5

P: n/a
John Bode wrote:
shan wrote:
Hi to all,
Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321


#include <stdlib.h>
#include <ctype.h>
...
char *chk;
int val = (int) strtol("321", &chk, 10);


The cast produces implementation-defined behavior if the return value
of strtol exceeds the representable range of int. Jack Klein has
written a very robust example of how to do this correctly at
http://home.att.net/~jackklein/c/code/strtol.html.

Robert Gamble

Dec 10 '05 #6

P: n/a
shan wrote:
Hi to all,
Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321


atoi(), or strtod(), or even sscanf().
Dec 11 '05 #7

P: n/a
Stan Milam wrote:
shan wrote:
Hi to all,
Is there any way to convert string to integer.string will
contain only integers.I am using turbo c++
example :
"321" to 321


atoi(), or strtod(), or even sscanf().


That would be strtol(), surely.
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Dec 12 '05 #8

P: n/a
"shan" <sr**********@gmail.com> wrote:
# Hi to all,
# Is there any way to convert string to integer.string will
# contain only integers.I am using turbo c++
# example :
# "321" to 321

strtol and related routines, or sscanf.

--
SM Ryan http://www.rawbw.com/~wyrmwif/
The little stoner's got a point.
Dec 12 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.