Connecting Tech Pros Worldwide Forums | Help | Site Map

how to convert a string to number

Member
 
Join Date: Oct 2006
Posts: 60
#1: Mar 12 '07
hi. I want to get a string as input.
If the input is an integer i want to save it to an int variable.
I know to convert a number to int. but how to convert a string to number(integer). in C++

Expert
 
Join Date: Nov 2006
Location: UK
Posts: 1,320
#2: Mar 12 '07

re: how to convert a string to number


Quote:

Originally Posted by palani12kumar

hi. I want to get a string as input.
If the input is an integer i want to save it to an int variable.
I know to convert a number to int. but how to convert a string to number(integer). in C++

In C++ you can use stringstream for this, see
http://www.cppreference.com/cppsstream/all.html
Newbie
 
Join Date: Mar 2007
Posts: 1
#3: Mar 12 '07

re: how to convert a string to number


Quote:

Originally Posted by palani12kumar

hi. I want to get a string as input.
If the input is an integer i want to save it to an int variable.
I know to convert a number to int. but how to convert a string to number(integer). in C++

you can apply atoi () or atof () from the c language.

string s = "10";
int i = atoi(s.c_str());
Member
 
Join Date: Oct 2006
Posts: 60
#4: Mar 14 '07

re: how to convert a string to number


Thank you for the kind Help
Reply