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

# conversion

 P: 2 how to convert a string to integer in c++ and vice versa Suhaas Nov 5 '06 #1
Share this Question
4 Replies

 Expert 100+ P: 1,510 how to convert a string to integer in c++ and vice versa if your strings are in char[] you can use the strstream class, e.g. Expand|Select|Wrap|Line Numbers // parse a decimal number using strstream #include  #include  using namespace std;    int main() {     // convert this string to floats     char instring[20]=" 3.1415926   2.718";     float fnum=0;     // attempt to read a float from data[]     istrstream strin(instring,20);     // read first float from strin and print it     strin >> fnum;     if(strin.fail())  { cout << "input fail\n"; exit(1); }      cout << "float fnum = " << fnum << endl;     // read second float from strin and print it     strin >> fnum;     if(strin.fail())  { cout << "input fail\n"; exit(1); }      cout << "float fnum = " << fnum << endl;       // convert fnum back to a string and put it in outstring     char outstring[20]={};     ostrstream strout(outstring, 20);     strout << fnum;     cout << "outstring = " << outstring << endl; }   Nov 5 '06 #2

 Expert 100+ P: 1,510 if you are using strings use sstream, e.g. Expand|Select|Wrap|Line Numbers // parse a decimal number using stringstream #include  #include  using namespace std;    int main() {     // convert this string to floats     string instring=" 3.1415926   2.718";     float fnum=0;     // attempt to read a float from data[]     istringstream strin(instring);     // read first float from strin and print it     strin >> fnum;     if(strin.fail())  { cout << "input fail\n"; exit(1); }      cout << "float fnum = " << fnum << endl;     // read second float from strin and print it     strin >> fnum;     if(strin.fail())  { cout << "input fail\n"; exit(1); }      cout << "float fnum = " << fnum << endl;       // convert fnum back to a string and print it     ostringstream strout;     strout << fnum;     cout << "outstring = " << strout.str() << endl; }     Nov 5 '06 #3

 P: 2 can i use the atoi() function for converting from string to integer and sprintf() for viceversa? if you are using strings use sstream, e.g. Expand|Select|Wrap|Line Numbers // parse a decimal number using stringstream #include  #include  using namespace std;    int main() {     // convert this string to floats     string instring=" 3.1415926   2.718";     float fnum=0;     // attempt to read a float from data[]     istringstream strin(instring);     // read first float from strin and print it     strin >> fnum;     if(strin.fail())  { cout << "input fail\n"; exit(1); }      cout << "float fnum = " << fnum << endl;     // read second float from strin and print it     strin >> fnum;     if(strin.fail())  { cout << "input fail\n"; exit(1); }      cout << "float fnum = " << fnum << endl;       // convert fnum back to a string and print it     ostringstream strout;     strout << fnum;     cout << "outstring = " << strout.str() << endl; }     Nov 6 '06 #4

 Expert 100+ P: 1,510 can i use the atoi() function for converting from string to integer and sprintf() for viceversa? if you only have one number in the string atof() is OK, if you have more than one, e.g. char instring[20]=" 3.1415926 2.718"; string streams allow you to read a sequence of values (which can be different types) together with all the other conversion controls. try this program Expand|Select|Wrap|Line Numbers // parse a decimal number using strstream #include  #include  using namespace std;    int main() {     // convert this string to floats     char instring[20]=" 3.1415926   2.718";     float fnum=0;     // read first float from strin and print it     fnum=atof(instring);     cout << "float fnum = " << fnum << endl;     // read second float from strin and print it     fnum=atof(instring);     cout << "float fnum = " << fnum << endl;       // convert fnum back to a string and put it in outstring     char outstring[20]={};     ostrstream strout(outstring, 20);     strout << fnum;     cout << "outstring = " << outstring << endl;     cin.get();     cin.get(); }     how can you read the second number in the string using atof()? Nov 6 '06 #5

### Post your reply

Sign in to post your reply or Sign up for a free account.