converting a string to an int or double..

This is a discussion on converting a string to an int or double.. within the C++ Programming forums, part of the General Programming Boards category; how would i go about converting a string to a double?...

  1. #1
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69

    converting a string to an int or double..

    how would i go about converting a string to a double?

  2. #2
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    1. Use the atof function:
    Code:
    #include <iostream>
    #include <string>
    #include <cstdlib>
    
    using namespace std;
    
    int main()
    {
        string str("19.874");
        double d;
    
        // Use atof to convert string to double
        d = atof(str.c_str());
    
        // Output double value
        cout << d << endl;
    
        return 0;
    }
    2. Use stringstreams:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    int main()
    {
        string str("19.874");
        stringstream sstr(str);
        double d;
    
        // Extract double value from stringstream
        sstr >> d;
    
        // Output double value
        cout << d << endl;
    
        return 0;
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    http://www.cppreference.com/stdstring/atof.html

    this is how you can do it in C not C++ hope it helps

    [edit1]
    took me a little while to find the correct link.
    if you saw this with the old link, sorry to confuse you.

    here is the index to the site with good details
    http://www.cppreference.com/index.html

    if all else fails google it.
    [/edit1]
    Last edited by xviddivxoggmp3; 03-27-2005 at 01:14 PM.
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

  5. #5
    Waxy-Dock
    Join Date
    Mar 2005
    Posts
    69
    thanks for that...

    also how do you convert a double to a string?

  6. #6
    Registered User
    Join Date
    Nov 2004
    Posts
    86
    using <sstream>:

    Code:
    #include <sstream>
     
    using namespace std;
     
    string makeString(double d)
    {
    	ostringstream ss;
    	ss<<d<<flush;
    	return ss.str(); 
    }
    Last edited by sean; 03-27-2005 at 05:30 PM. Reason: Improper Code Tag Use

  7. #7
    essence of digital xddxogm3's Avatar
    Join Date
    Sep 2003
    Posts
    589
    here is another link i found that might be of assistance to you.
    all i did again was run a search on google to find it.
    it uses stringstream to convert from and to the formats your looking for.
    http://pegasus.rutgers.edu/~elflord/...ingstream.html
    "Hence to fight and conquer in all your battles is not supreme excellence;
    supreme excellence consists in breaking the enemy's resistance without fighting."
    Art of War Sun Tzu

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. NEED HELP READING FILE and PRINTING
    By geoffr0 in forum C Programming
    Replies: 4
    Last Post: 04-16-2009, 05:26 PM
  2. Testing some code, lots of errors...
    By Sparrowhawk in forum C Programming
    Replies: 48
    Last Post: 12-15-2008, 03:09 AM
  3. need some help with last part of arrays
    By Lince in forum C Programming
    Replies: 3
    Last Post: 11-18-2006, 08:13 AM
  4. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  5. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 11:58 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21