Results 1 to 6 of 6

Thread: char to const char* with atof

  1. #1
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default char to const char* with atof

    Hello, I have this problem; it gets a runtime error. Why?
    Qt Code:
    1. vector<double> vec;
    2. string s =" 123213131313131331";
    3. string::iterator it_s = s.begin();
    4. for ( ; it_s != s.end(); ++it_s) {
    5. vec.push_back( (double) atof( (const char *) (*it_s) ) ); //runtime error
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by mickey; 29th February 2008 at 00:49.
    Regards

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: char to const char* with atof

    You can't cast a char to const char * like that... atof() expects adress of a C string and outputs a float. What are you trying to achieve?

  3. #3
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: char to const char* with atof

    I edited it. maybe more clear now (convert each digit to double and psuh into vector)
    Regards

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: char to const char* with atof

    Khem.... Lot to learn you still have, young one

    Qt Code:
    1. QVector<double> vec;
    2. std::string str;
    3. for(int i=0;i<str.length();i++){
    4. int val = str[i] - '0';
    5. if(val<0 || val>9) continue;
    6. vec.push_back((double)val);
    7. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Jan 2006
    Posts
    976
    Thanks
    53
    Qt products
    Qt3
    Platforms
    Windows

    Default Re: char to const char* with atof

    thanks but isn't there a way to use that iterator on the string, please?
    EDIT: and why a cast at C style ( "(double)" I mean )
    Last edited by mickey; 29th February 2008 at 01:11.
    Regards

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: char to const char* with atof

    Use an iterator if you want - I don't mind. The heart of the solution is the conversion of a character to digit, not the way you traverse the string. The cast is "just in case" so that I'm more confident it compiles.

Similar Threads

  1. QSql*Model + QTreeView : make a tree
    By punkypogo in forum Qt Programming
    Replies: 18
    Last Post: 24th October 2008, 19:14
  2. qt 4.2.2 install in tru64 cxx
    By try to remember in forum Installation and Deployment
    Replies: 0
    Last Post: 30th March 2007, 08:43
  3. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 21:49
  4. QTableView paints too much
    By Jimmy2775 in forum Qt Programming
    Replies: 2
    Last Post: 26th July 2006, 19:42
  5. Delegates and Table
    By ankurjain in forum Qt Programming
    Replies: 8
    Last Post: 18th May 2006, 20:47

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.