468,496 Members | 1,824 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,496 developers. It's quick & easy.

how to convert a string into integer

Hi there,

I want to convert a String into integer.

I get the string from a 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
3 8818

priyanka wrote:
Hi there,

I want to convert a String into integer.

I get the string from a file using :

string argNum;
getline(inputStream,argNum);
Why not:

int x;
inputStream >x;
>
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*)'
use c_str() to get the char* insides of a std::string.
>
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 #2
Noah Roberts wrote:
priyanka wrote:
>But I got errot message :error: cannot convert `std::string' to `const
char*' for argument `1' to `int atoi(const char*)'

use c_str() to get the char* insides of a std::string.
>>
You mean to get the _const_ char* insides of a std::string.
Jul 17 '06 #3
priyanka schrieb:
Hi there,

I want to convert a String into integer.

I get the string from a 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 ?
It's a FAQ:

http://www.parashift.com/c++-faq-lite/
http://www.parashift.com/c++-faq-lit....html#faq-39.2

--
Thomas
Jul 17 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

5 posts views Thread by IamZadok | last post: by
3 posts views Thread by Convert TextBox.Text to Int32 Problem | last post: by
8 posts views Thread by Carlos | last post: by
5 posts views Thread by Mika M | last post: by
14 posts views Thread by Drew | last post: by
20 posts views Thread by Niyazi | last post: by
7 posts views Thread by elliotng.ee | last post: by
reply views Thread by NPC403 | last post: by
3 posts views Thread by gieforce | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.