Thread: Numbers in String to Double

  1. #1
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Question Numbers in String to Double

    Hi All;
    I am new to the world of programming and is stuck on a small part.I am writing a program for a machine which does some measurement.It is done successfully.But the machine returns the value as

    Code:
    READOUT "  -0.01 V,  -0.04 A"  /n
    Now I need to seperate the values -0.01 and -0.04 into two double variables like x=0.01 and y=0.04.I tried to copy this string to a character array and seperate it from there.But didnt work.

    Any ideas and help is very much appreciated.

    Have a nice day and Regards

  2. #2
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Use sscanf() -> http://www.cplusplus.com/reference/c...io/sscanf.html
    If you have any probs just ask, but it would be nice if you tried yourself first - otherwise you won't learn

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Talking Thnx

    Hi thnx for the function.Didnt know it existed.but I still cant get it into double variables.Besides its the part of a much bigger program tht is already completed.Only this remains to be finished and is so tagging.If u could give a sample would be helpful to get those in to two double variables.

    Thnx and regards

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    If it isn't %, then it stands for itself.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main ( ) {
        double x, y;
        char buff[] = "READOUT \"  -0.01 V,  -0.04 A\"  \n";
        if ( sscanf( buff, "READOUT \"  %lf V,  %lf", &x, &y ) == 2 ) {
            printf("Success: %f %f\n", x, y );
        }
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Thumbs up Thanks

    Hi Salem;
    Your program works perfectly.Thnx a lot.

    Thnx for all those who contributed too

    Have a nice day.

  6. #6
    Registered User
    Join Date
    Jun 2007
    Posts
    8
    Hi Salem;
    Thnx a lot for ur code.May I ask if it would also be possible to get the value after 0.01 and 0.04 to a character?
    I tried changing the code to character but didnt work.

    Kind regards

  7. #7
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        double x, y;
        char a, b;
        char buff[] = "READOUT \"  -0.01 V,  -0.04 A\"  \n";
    
        if(sscanf(buff, "READOUT \"  &#37;lf %c,  %lf %c", &x, &a, &y, &b) == 4)
        {
            printf("Success: %f %c %f %c\n", x, a, y, b);
        }
        return 0;
    }

  8. #8
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Thumbs up Thanks Again:)

    HI Al;
    Thnaks for your valuable help.The project finsihed successfully few minutes before

    Once again a big sincere thank you.Without your help,I would have doen it

    Have a nice time

  9. #9
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Unhappy This too is possible?

    Hi All;
    I am sorry there is a small problem with the code above.

    Sometimes it shows the ouput in this format

    Code:
    READOUT "?  -0.01 V,  -0.04 A"  /n
    And at that times the above code

    Code:
    if(sscanf(buff, "READOUT \"  %lf %c,  %lf %c", &x, &a, &y, &b) == 4)
        {
            printf("Success: %f %c %f %c\n", x, a, y, b);
        }
    doesnt run.Why is tht so?

    Regards

  10. #10
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Learn the syntax of sscanf() and friends, it'll save you time and effort.

  11. #11
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Unhappy Tried but...

    Hi;
    I tried to do tht.With a character to take in the '?'.But as u see it occures only some times.And sscanf() ignores the spaces.


    So its possible tht the value comes in 2 ways.
    Code:
    READOUT "  -0.01 V,  -0.04 A"  /n
    Code:
    READOUT "?  -0.01 V,  -0.04 A"  /n
    So I changed the format of the program to

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main(void)
    {
        double x, y;
        char a, b,c;
        char buff[] = "READOUT \"  -0.01 V,  -0.04 A\"  \n";
    
        if(sscanf(buff, "READOUT \" %c %lf %c,  %lf %c",&c, &x, &a, &y, &b) == 5)
        {
            printf("Success: %f %c %f %c\n", q,x, a, y, b);
        }
        return 0;
    }
    But so the variable q can be used to check if its a blank or "?".But it doesnt work.Could you please look through this?

    Regards

  12. #12
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    This is getting close to the limit of what a single sscanf call can achieve.
    I hope there are no more optional surprises you've yet to mention.

    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    void test ( const char *buff ) {
        double  x, y;
        char    a, b, c;
        char    q[3];
    
        printf("Line=&#37;s", buff );
        if ( sscanf(buff, "READOUT %[\"?] %c %lf %c,  %lf %c", q, &c, &x, &a, &y, &b) == 6)
        {
            printf("Success: %s %f %c %f %c\n", q, x, a, y, b);
        }
    }
    
    int main ( void ) {
        test( "READOUT \"  -0.01 V,  -0.04 A\"  \n" );
        test( "READOUT \"?  -0.01 V,  -0.04 A\"  \n" );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  13. #13
    Registered User
    Join Date
    Jun 2007
    Posts
    8

    Thumbs up Thnx

    Hi Salem;
    I didnt mean to add in "surprises" later on.Just tht the machine showed some strange things after some breakdown which wasnt aticipated.Today is the last day of the project so thee wont be anythng more

    So Thanks again to all of those who helped and have a nice day.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 8
    Last Post: 04-25-2008, 02:45 PM
  2. Inheritance Hierarchy for a Package class
    By twickre in forum C++ Programming
    Replies: 7
    Last Post: 12-08-2007, 04:13 PM
  3. Logical errors with seach function
    By Taka in forum C Programming
    Replies: 4
    Last Post: 09-18-2006, 05:20 AM
  4. Unknown Math Issues.
    By Sir Andus in forum C++ Programming
    Replies: 1
    Last Post: 03-06-2006, 06:54 PM
  5. Help with multi function progam
    By WackoWolf in forum C Programming
    Replies: 22
    Last Post: 10-13-2005, 02:56 AM