changing a string to a 'double' data type

This is a discussion on changing a string to a 'double' data type within the C++ Programming forums, part of the General Programming Boards category; I'm trying to figure out a way to read in two values- one is a char and one is a ...

  1. #1
    Registered User
    Join Date
    Oct 2009
    Posts
    29

    changing a string to a 'double' data type

    I'm trying to figure out a way to read in two values- one is a char and one is a string. I would like to pull the real number from the string if it is a real number.

    basically if the string was a real number it would output as double type.

    here's an example of the basic input structure:

    Code:
    #include <iostream>
    #include <string>
    using namespace std;
    
    char a;
    string b;
    
    cin >> a >> b;
    and then somehow would use a function of "strtod" or something similar to extract the real number if there is one.

    STRTOD - convert string to double precision number.


    here is an example of one that works but w/o the cin value. I have tried using this one below by adding a cin value, but can't get it to work.

    Code:
    #include <iostream>
    #include <stdlib.h>                                                             
    #include <stdio.h>  
    #include <string>
    using namespace std;
    
    
    /* CELEBS3                                      
       This example converts a string to a double value.                            
       It prints out the converted value and the substring that                     
       stopped the conversion.                                                                                                                                  
     */ 
                                                                                    
    int main()                                                                  
    {                                                                              
    	
    char *string, *stopstring;                                                   
       double x;                                                                                      
    
       string = "-5667.45&%$";                                                          
       x = strtod(string, &stopstring);            
    
    
       printf("string = \"%s\"\n", string);                                         
       printf("   strtod = %f\n", x);                                               
       printf("   Stopped scan at \"%s\"\n\n", stopstring);            
    
       cout << x << endl;
       cout << stopstring << endl;
    
    }

    and the output is:

    string = "-5667.45&%$"
    strtod = -5667.450000
    Stopped scan at "&%$"

    -5667.45
    &%$
    Press any key to continue . . .


    part of that reason is I don't know how to make that strtod function work with a cin command line is I don't know what the asterisks are for on this line: char *string, *stopstring;
    I thought if the data type was char, it would be just one letter long. according to the line:
    string = "-5667.45&%$";" can have more than one letter...

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    People always suggest lexical_cast (if you have Boost), so that's one option. The "standard" method is to use a stringstream.

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Posts
    22,900
    Quote Originally Posted by tabstop View Post
    People always suggest lexical_cast (if you have Boost), so that's one option. The "standard" method is to use a stringstream.
    Yes, it is a shame that this method does not exist in the standard. It is so convenient and so easy for newbies. One for all, and all for one. So to speak.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    29
    thank you, found some good info on stringstreams and does exactly what I needed.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  2. Please Help - Problem with Compilers
    By toonlover in forum C++ Programming
    Replies: 5
    Last Post: 07-23-2005, 11:03 AM
  3. ras.h errors
    By Trent_Easton in forum Windows Programming
    Replies: 8
    Last Post: 07-15-2005, 11:52 PM
  4. Errors
    By Rhidian in forum C Programming
    Replies: 10
    Last Post: 04-04-2005, 01:22 PM
  5. HUGE fps jump
    By DavidP in forum Game Programming
    Replies: 23
    Last Post: 07-01-2004, 11:36 AM

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