how to convert a string into integer

P

priyanka

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
 
N

Noah Roberts

priyanka said:
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.
 
T

Thomas J. Gritzan

priyanka said:
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-lite/misc-technical-issues.html#faq-39.2
 

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

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top