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

converting string into integer

P: n/a
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

Jul 17 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
priyanka wrote:
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.]

--
Peter

Jul 17 '06 #2

P: n/a

priyanka wrote:
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

Jul 17 '06 #3

P: n/a
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.

Jul 18 '06 #4

P: n/a
"Andy" <an*****@gmail.comwrites:
>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.
The original question was posted by "priyanka" <pr**********@gmail.com>.
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.

--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Jul 18 '06 #5

P: n/a
On 17 Jul 2006 17:48:03 -0700, "Andy" <an*****@gmail.comwrote in
comp.lang.c:

Please don't trim attribution lines. I've put it back:
On 17 Jul 2006 15:22:23 -0700, "priyanka" <pr**********@gmail.comwrote in comp.lang.c:
....that's better!
>
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.
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.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://c-faq.com/
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 18 '06 #6

P: n/a

Jack Klein wrote:
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.
But elswhere, Keith writes:
>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...

Jul 18 '06 #7

P: n/a
sp****@gmail.com wrote:
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.

Jul 18 '06 #8

P: n/a
Bill Pursell wrote:
Jack Klein wrote:

>>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.


But elswhere, Keith writes:
>>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...
I thought the third door was the moderated group...

--
Ian Collins.
Jul 18 '06 #9

P: n/a
On 17 Jul 2006 21:44:58 -0700, in comp.lang.c , "Bill Pursell"
<bi**********@gmail.comwrote:
>>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.

But elswhere, Keith writes:
>>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!
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
Jul 18 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.