Thread: pulling 2 doubles from a string?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    pulling 2 doubles from a string?

    I have been playing around with sscanf but I have recently read that it does not even support floats and doubles. true?

    Code:
    double c1,c2;
    
    string input = "guaranteed repair & support at (3.124, 391.111).";
    sscanf(input.c_str(),"%lf%lf%",&c1,&c2);
    doesnt save anything into c1, c2

    Code:
    double c1,c2;
    
    string input = "guaranteed repair & support at (3.124, 391.111).";
    sscanf(input.c_str(),"%*s%lf, %lf%).",&input, &c1,&c2);
    doesnt change anything


    anyone know a way to pull the doubles out of the string and save them in c1 and c2?
    (without explicitly iterating through the string and finding the (...) )

    THanks

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    doesnt save anything into c1, c2
    Why would it? The `%lf' modifier (for `sscanf') reads a single `double'. There is no `double' at that point in the input.

    doesnt change anything
    Why would it? The `%s' modifier (for `sscanf') reads a single "word". There is more than one "word" before the first `double' value.

    I have been playing around with sscanf but I have recently read that it does not even support floats and doubles. true?
    You need to go read the standard documentation. What other sources may say is irrelevant.

    anyone know a way to pull the doubles out of the string and save them in c1 and c2?
    You need to go read the standard documentation. Programming isn't magic. You can't wave your bits around and hope for the best.

    Soma

    Code:
    #include <stdio.h>
    
    int main()
    {
    	double c1 = 0;
    	double c2 = 0;
    	char input[] = "guaranteed repair & support at (3.124, 391.111).\n";
    	if(2 == sscanf(input, "%*[^0123456789.]%lf%*[^0123456789.]%lf%*[^\n]%*[\n]", &c1, &c2))
    	{
    		printf("%f\n%f\n", c1, c2);
    	}
    	else
    	{
    		printf("error");
    	}
    	return(0);
    }

  3. #3
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    what about negative values?

    and what this thread has to do with C++ anyway? It should be moved to C-forum
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    what about negative values?
    What about different locals that don't use '.' as the radix separator?
    What about values expressed in other notations (scientific)?
    What about values expressed in a different base (16)?
    What if the type changes to `float' or `long double'?
    What if the string uses a MBCS?

    Do you want to post some source that can handle all of these potential changes for him? No? "You need to go read the standard documentation." ^_^

    and what this thread has to do with C++ anyway? It should be moved to C-forum
    Calm yourself. Unless you have the authority to move it, you'll have to wait.

    Soma

  5. #5
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    it should be in the C-programming forum, and it can be done by asking the
    mods. but lets not bother them for now...

  6. #6
    Registered User UltraKing227's Avatar
    Join Date
    Jan 2010
    Location
    USA, New york
    Posts
    123
    by the way, rodrigorules, its not a good example of a C++ programmer
    using both C and C++ in a program. it can be used, but i dont think thats
    a good way of programming.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Replies: 4
    Last Post: 03-03-2006, 02:11 AM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM