Thread: strtod not working

  1. #1
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92

    strtod not working

    I want to convert string to double. I don't know. So I googling. I found this link:
    http://www.gidforums.com/t-1131.html

    It does not work.
    here's my code:
    Code:
     #include <iostream>
          #include <cmath>
          using namespace std;
         
         
          int main() {
         
              string aa = "1234.333";
              double akbar = strtod(aa.c_str());
              cout << akbar << endl;
          }
    But I got this error:
    /usr/include/stdlib.h: In function `int main()':
    /usr/include/stdlib.h:160: error: too few arguments to function `double
    strtod(const char*, char**)'
    tes.cpp:9: error: at this point in file

    os : linux slackware 9.1
    compiler: : g++ 3.3.3

    What is additional argument????

  2. #2
    Registered User deadpoet's Avatar
    Join Date
    Jan 2004
    Posts
    50
    Look at the following webpage, it may help.
    http://www.thinkage.ca/english/gcos/...ib/strtod.html

    STRTOD - convert string to double precision number.
    (ANSI Standard)
    Usage:
    #include <stdlib.h>
    dx = strtod( s, p );

    Where:
    const char *s;
    is the string to be converted into a double precision number. This string may consist of any number of blanks and/or tabs, possibly followed by a sign, followed by a string of digits that may contain a decimal point, then an "e" or "E" possibly followed by a sign, followed by an integer. It may also be in the hexadecimal format used by the "%a" format of "printf".
    char **p;
    points to a pointer that will be set to the character immediately following the double precision number in the string "s". For example, with
    s = "1.23y";
    dx = strtod(s,p);

    a pointer to the character 'y' would be placed in "*p". If no floating point number can be formed from "s", "*p" points to the first character of "s". If "p" is the NULL pointer, this sort of action does not take place.
    double dx;
    is the double precision number obtained from "s". If no conversion could be performed (because the string didn't start with a valid number), 0.0 is returned. If there was an overflow, "strtod" sets "errno" to ERANGE and returns positive or negative HUGE_VAL (where HUGE_VAL is a large positive double value defined in <math.h>). It there was an underflow, "strtod" returns 0.0.
    Description:
    "strtod" converts the string "s" into a double precision number. Conversion stops with the first character that cannot be part of such a number.
    Code:
    #include <iostream.h>
    #include <string>
    #include <stdlib.h>
    #include <cmath>
    int main() {
    
      string aa = "1234.333";
      double akbar = strtod(aa.c_str(), NULL);
      cout << akbar << endl;
      return ( EXIT_SUCCESS );
    }
    Returns: 1234.33


    DeadPoet
    Last edited by deadpoet; 03-10-2004 at 09:19 AM.

  3. #3
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Better to use stringstreams. They may not be as concise, but I feel they're more flexible:
    Code:
    #include <iostream>
    #include <string>
    #include <sstream>
    
    using namespace std;
    
    double stod ( string& s )
    {
      double d;
      istringstream conv ( s );
    
      conv>> d;
    
      return d;
    }
    
    int main() {
    
      string aa = "1234.333";
      double akbar = stod ( aa );
      cout<< akbar <<endl;
    }
    My best code is written with the delete key.

  4. #4
    Registered User gandalf_bar's Avatar
    Join Date
    Oct 2003
    Posts
    92
    Thanx guys!!! I keep that in my mind when I need to convert string to double. By the way, I have used atof function in my current program and I am too lazy to change it.

    Enjoy.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Function not working
    By sloopy in forum C Programming
    Replies: 31
    Last Post: 11-12-2005, 08:08 PM
  2. Program Not working Right
    By raven420smoke in forum C++ Programming
    Replies: 2
    Last Post: 09-16-2005, 03:21 AM
  3. Trying to eject D drive using code, but not working... :(
    By snowfrog in forum C++ Programming
    Replies: 3
    Last Post: 05-07-2005, 07:47 PM
  4. x on upper right corner not working
    By caduardo21 in forum Windows Programming
    Replies: 1
    Last Post: 02-20-2005, 08:35 PM
  5. cygwin -> unix , my code not working properly ;(
    By CyC|OpS in forum C Programming
    Replies: 4
    Last Post: 05-18-2002, 04:08 AM