473,324 Members | 2,535 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

conversion

how to convert a string to integer in c++ and vice versa

Suhaas
Nov 5 '06 #1
4 3293
horace1
1,510 Expert 1GB
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
horace1
1,510 Expert 1GB
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
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
horace1
1,510 Expert 1GB
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

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

Similar topics

1
by: Stub | last post by:
Docs says that "The compiler does not use an explicit constructor to implement an implied conversion of types. It's purpose is reserved explicitly for construction." I put up code of three cases...
7
by: Michael Lehn | last post by:
Hi, I have a question regarding the conversion of objects. When is the conversion done by the constructor and when by the operator. My feeling tells me that the constructor is preferred. But...
16
by: TTroy | last post by:
Hello, I'm relatively new to C and have gone through more than 4 books on it. None mentioned anything about integral promotion, arithmetic conversion, value preserving and unsigned preserving. ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
11
by: Steve Gough | last post by:
Could anyone please help me to understand what is happening here? The commented line produces an error, which is what I expected given that there is no conversion defined from type double to type...
2
by: Alex Sedow | last post by:
Why explicit conversion from SomeType* to IntPtr is not ambiguous (according to standart)? Example: // System.IntPtr class IntPtr { public static explicit System.IntPtr (int); public...
3
by: Steve Richter | last post by:
here is a warning I am getting in a C++ .NET compile: c:\SrNet\jury\JuryTest.cpp(55) : warning C4927: illegal conversion; more than one user-defined conversion has been implicitly applied while...
0
by: Lou Evart | last post by:
DOCUMENT CONVERSION SERVICES Softline International (SII) operates one of the industry's largest document and data conversion service bureaus. In the past year, SII converted over a million...
0
by: dataentryoffshore | last post by:
Get a Discount up to 60% on data entry, data capture, dataentry services, large volume data processing and data conversion services through offshore facilities in India. Offshore data entry also...
21
by: REH | last post by:
It it permissible to use the constructor style cast with primitives such as "unsigned long"? One of my compilers accepts this syntax, the other does not. The failing one chokes on the fact that the...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.