converting string into integer

P

priyanka

Hi there,

I want to convert a String into integer.

I get the string froma file using :

string argNum;
getline(inputStream,argNum);

I now need to convert argNum into integer.

I tried to use

int a = atoi(argNum);

But I got errot message :error: cannot convert `std::string' to `const
char*' for argument `1' to `int atoi(const char*)'

And for the getline function, the argNum must be a string variable.

Can anyone please tell me how to convert argNum into integer ?

Thank you in advance,
priya
 
P

Peter Nilsson

priyanka said:
Hi there,

I want to convert a String into integer.
...
But I got errot message :error: cannot convert `std::string' to `const
char*' for argument `1' to `int atoi(const char*)'

Try comp.lang.c++ down the hall. We only deal with C.

[And just in case: No, C is not a subset of C++, and no, the C solution
is
not 'okay' for a C++ program.]
 
S

spibou

priyanka said:
Hi there,

I want to convert a String into integer.

I get the string froma file using :

string argNum;
getline(inputStream,argNum);

What is this getline function you're speaking of ?
I now need to convert argNum into integer.

I tried to use

int a = atoi(argNum);

But I got errot message :error: cannot convert `std::string' to `const
char*' for argument `1' to `int atoi(const char*)'

Since this std::string thingy is not C you were bound to get an error.
By the way , C++ questions should be asked at comp.lang.c++
And for the getline function, the argNum must be a string variable.

Can anyone please tell me how to convert argNum into integer ?

If argNum is pointer to char , then you use atoi. In C that is.

Spiros Bousbouras
 
A

Andy

int a = atoi(argNum);

But I got errot message :error: cannot convert `std::string' to `const
char*' for argument `1' to `int atoi(const char*)'

You can use argNum.c_str() to solve this problem.
I think you are a newer to C++. This is a very basic problem. You can
find the answer in many books very easily.
 
K

Keith Thompson

Andy said:
You can use argNum.c_str() to solve this problem.
I think you are a newer to C++. This is a very basic problem. You can
find the answer in many books very easily.

The original question was posted by "priyanka" <[email protected]>.
Please don't snip attribution lines.

If your answer involves C++, please don't post it here. comp.lang.c++
is down the hall, third door on the left, just past the water cooler.
We discuss C here, and we can't check the accuracy of any C++
information.
 
J

Jack Klein

On 17 Jul 2006 15:22:23 -0700, "priyanka" <[email protected]> wrote in comp.lang.c:

....that's better!
You can use argNum.c_str() to solve this problem.

No he can't, not in C.
I think you are a newer to C++. This is a very basic problem. You can
find the answer in many books very easily.

He has a much more basic problem. He either can't distinguish between
C and C++, or he can't distinguish between comp.lang.c and
comp.lang.c++. Until he gets that issue straightened out, I foresee a
world of hurt in his future.
 
B

Bill Pursell

Jack said:
He has a much more basic problem. He either can't distinguish between
C and C++, or he can't distinguish between comp.lang.c and
comp.lang.c++. Until he gets that issue straightened out, I foresee a
world of hurt in his future.

Hmmm. In this thread, we find a quote from Keith
Thompson.
If your answer involves C++, please don't post it here. comp.lang.c++
is down the hall, third door on the left, just past the water cooler.
We discuss C here, and we can't check the accuracy of any C++
information.

you're in the wrong place; comp.lang.c++ is down the
hall, just past the water cooler, first door on the left.

This leaves me completely baffled! Not only can I not
find the water cooler, but the group seems to be changing
rooms! At least I'm confident that it will be on the left side
of the hall...
 
O

Old Wolf

If argNum is pointer to char , then you use atoi. In C that is.

atoi causes undefined behaviour if the number is bigger than
the biggest possible int (or smaller than the smallest possible).

It's best to use strtol or strtoul.
 
I

Ian Collins

Bill said:
Jack Klein wrote:




Hmmm. In this thread, we find a quote from Keith
Thompson.







This leaves me completely baffled! Not only can I not
find the water cooler, but the group seems to be changing
rooms! At least I'm confident that it will be on the left side
of the hall...
I thought the third door was the moderated group...
 
M

Mark McIntyre

This leaves me completely baffled! Not only can I not
find the water cooler, but the group seems to be changing
rooms!

Its a big room. What, theatres in your town only have one way in ?
At least I'm confident that it will be on the left side
of the hall...

And on the right...
--
Mark McIntyre

"Debugging is twice as hard as writing the code in the first place.
Therefore, if you write the code as cleverly as possible, you are,
by definition, not smart enough to debug it."
--Brian Kernighan
 

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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top