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
Bytes IT Community
+ 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
Share on Google+
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
  1. // parse a decimal number using strstream
  2. #include <iostream>
  3. #include <strstream>
  4. using namespace std; 
  5.  
  6. int main()
  7. {
  8.     // convert this string to floats
  9.     char instring[20]=" 3.1415926   2.718";
  10.     float fnum=0;
  11.     // attempt to read a float from data[]
  12.     istrstream strin(instring,20);
  13.     // read first float from strin and print it
  14.     strin >> fnum;
  15.     if(strin.fail())  { cout << "input fail\n"; exit(1); } 
  16.     cout << "float fnum = " << fnum << endl;
  17.     // read second float from strin and print it
  18.     strin >> fnum;
  19.     if(strin.fail())  { cout << "input fail\n"; exit(1); } 
  20.     cout << "float fnum = " << fnum << endl;
  21.  
  22.     // convert fnum back to a string and put it in outstring
  23.     char outstring[20]={};
  24.     ostrstream strout(outstring, 20);
  25.     strout << fnum;
  26.     cout << "outstring = " << outstring << endl;
  27. }
  28.  
Nov 5 '06 #2

Expert 100+
P: 1,510
if you are using strings use sstream, e.g.
Expand|Select|Wrap|Line Numbers
  1. // parse a decimal number using stringstream
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std; 
  5.  
  6. int main()
  7. {
  8.     // convert this string to floats
  9.     string instring=" 3.1415926   2.718";
  10.     float fnum=0;
  11.     // attempt to read a float from data[]
  12.     istringstream strin(instring);
  13.     // read first float from strin and print it
  14.     strin >> fnum;
  15.     if(strin.fail())  { cout << "input fail\n"; exit(1); } 
  16.     cout << "float fnum = " << fnum << endl;
  17.     // read second float from strin and print it
  18.     strin >> fnum;
  19.     if(strin.fail())  { cout << "input fail\n"; exit(1); } 
  20.     cout << "float fnum = " << fnum << endl;
  21.  
  22.     // convert fnum back to a string and print it
  23.     ostringstream strout;
  24.     strout << fnum;
  25.     cout << "outstring = " << strout.str() << endl;
  26. }
  27.  
  28.  
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
  1. // parse a decimal number using stringstream
  2. #include <iostream>
  3. #include <sstream>
  4. using namespace std; 
  5.  
  6. int main()
  7. {
  8.     // convert this string to floats
  9.     string instring=" 3.1415926   2.718";
  10.     float fnum=0;
  11.     // attempt to read a float from data[]
  12.     istringstream strin(instring);
  13.     // read first float from strin and print it
  14.     strin >> fnum;
  15.     if(strin.fail())  { cout << "input fail\n"; exit(1); } 
  16.     cout << "float fnum = " << fnum << endl;
  17.     // read second float from strin and print it
  18.     strin >> fnum;
  19.     if(strin.fail())  { cout << "input fail\n"; exit(1); } 
  20.     cout << "float fnum = " << fnum << endl;
  21.  
  22.     // convert fnum back to a string and print it
  23.     ostringstream strout;
  24.     strout << fnum;
  25.     cout << "outstring = " << strout.str() << endl;
  26. }
  27.  
  28.  
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
  1. // parse a decimal number using strstream
  2. #include <iostream>
  3. #include <strstream>
  4. using namespace std; 
  5.  
  6. int main()
  7. {
  8.     // convert this string to floats
  9.     char instring[20]=" 3.1415926   2.718";
  10.     float fnum=0;
  11.     // read first float from strin and print it
  12.     fnum=atof(instring);
  13.     cout << "float fnum = " << fnum << endl;
  14.     // read second float from strin and print it
  15.     fnum=atof(instring);
  16.     cout << "float fnum = " << fnum << endl;
  17.  
  18.     // convert fnum back to a string and put it in outstring
  19.     char outstring[20]={};
  20.     ostrstream strout(outstring, 20);
  21.     strout << fnum;
  22.     cout << "outstring = " << outstring << endl;
  23.     cin.get();
  24.     cin.get();
  25. }
  26.  
  27.  
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.