Thread: String with two words: 1)String, 2) $double

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

    String with two words: 1)String, 2) $double

    I have a string saved in memory with exactly one space in the middle and two substrings; the string is formatted like this:

    "Mike $120.00"

    The first word will be saved as a string, the second needs to be converted to a float. Please, how can I do separate this string at the space and convert the 2nd string into a float?

    Thanks in advance.
    Last edited by Roger; 11-20-2009 at 01:26 PM. Reason: "saved in memory"

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Assuming that the first word does not contain whitespace, you could use sscanf() with a format string like "%Ns $%f", where N is the maximum length of the destination string (i.e., excluding the null terminator).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Oct 2009
    Posts
    30
    Code:
    char name[50];
    float amount;
    ...
    
    sscanf(buffer, "%s $%f", name, amount);
    Code:
    warning: format â%fâ expects type âfloat *â, but argument 4 has type âdoubleâ
    I don't know why I'm getting this warning. The variable 'amount' is declared as a float, so I do not see why the compiler is making this warning.
    Last edited by Roger; 11-20-2009 at 01:51 PM. Reason: realized something

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should be taking the address of amount, so it should be:
    Code:
    sscanf(buffer, "%49s $%f", name, &amount);
    You might also want to check that sscanf returned 2.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Oct 2009
    Posts
    30
    Thank you very much for your help laserlight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Converting a string of words into correct cases
    By BigFish21 in forum C++ Programming
    Replies: 2
    Last Post: 05-26-2008, 12:53 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 05:45 PM
  4. Another overloading "<<" problem
    By alphaoide in forum C++ Programming
    Replies: 18
    Last Post: 09-30-2003, 10:32 AM
  5. lvp string...
    By Magma in forum C++ Programming
    Replies: 4
    Last Post: 02-27-2003, 12:03 AM

Tags for this Thread