sstream

M

Michael

Hi,

I am using Quincy99 and do not have sstream library to use. I want to
convert string "123" to integer 123. How to get the work done? Thanks!

#include <iostream>
#include <string>

using namespace std;

int main ()
{
string a ("123");

int b;
b=int(a);

cout << b;

return 0;
}


Thanks a lot!
Michael
 
D

David Harmon

On Tue, 19 Jun 2007 11:05:44 -0700 in comp.lang.c++, Michael
I am using Quincy99 and do not have sstream library to use. I want to
convert string "123" to integer 123. How to get the work done? Thanks!

Look for old C strtol()
 
V

Victor Bazarov

Bharath said:
why not this -
#include <stdlib.h>
i = atoi(s);

'atoi' is not a very good function. E.g. it doesn't distinguish between
a string that contains "0" and a string that does not contain a number.

V
 
D

Default User

Victor said:
'atoi' is not a very good function. E.g. it doesn't distinguish
between a string that contains "0" and a string that does not contain
a number.

There's a further problem (or maybe a generalization of that one) in
that a string with an initial sequence of numerals will be partly
converted to an integer, again with no way of telling what happened.



Brian
 
M

Michael

There is no way to tell whether or not atoi() fails.

Thanks a lot, Experts. How to let the following code run?

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main ()
{
string a;
getline(cin,a);

char * pEnd;

int b;
b=strtol (a,&pEnd,10);

cout << b;

return 0;
}
 
V

Victor Bazarov

Michael said:
Thanks a lot, Experts. How to let the following code run?

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main ()
{
string a;
getline(cin,a);

char * pEnd;

int b;
b=strtol (a,&pEnd,10);

Probably

b=strtol (a.c_str(),&pEnd,10);
cout << b;

return 0;
}

V
 
D

Default User

Please trim quoted material to the minimum needed for context.
Especially trim quoted signatures like the one above.
Thanks a lot, Experts. How to let the following code run?

#include <iostream>
#include <string>
#include <stdlib.h>

using namespace std;

int main ()
{
string a;
getline(cin,a);

char * pEnd;

int b;
b=strtol (a,&pEnd,10);

1. You can't pass a string to strtol(). It requires a const char*. Pass
a.c_str().

2. There's very little point in passing a pointer to pEnd to strtol()
and then not use it.

You'll need a couple of checks. One is:

if (pEnd == a.c_str())
{
// no conversion, handle it
}
else if (*pEnd != 0) // not at end of string
{
// partial conversion, handle it
}

You also should check for overflow, using errno. If errno == ERANGE,
then look at the return value. It will be either LONG_MAX or LONG_MIN,
telling you which way it would have overflowed.
cout << b;

return 0;
}




Brian
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top