Results 1 to 2 of 2

Thread: problem converting string-char-int

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

    Default problem converting string-char-int

    Hi,
    I'm trying to read from a file a char for time (that are float eg line of file: 1.1 1.0 3);
    Qt Code:
    1. vector <vector <float> > example;
    2. char ch;
    3. string s;
    4. int i=0,j=0;
    5. while (f.readChar(ch)) { //this is my function return a char on ch
    6. cout.put(ch);
    7. if (ch == '\n') lines++;
    8. else if (ch != ' ') {
    9. s += ch;
    10. } else {
    11. example[i][j] = atof(s.c_str()); //but here a runtime error uccurs
    12. s="";
    13. j++;
    14. }
    15.  
    16. i++;
    17. }
    To copy to clipboard, switch view to plain text mode 
    How to do that conversion? And then: is this the easier way to reach my aim? (I suspect it can be more simple).
    thanks
    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: problem converting string-char-int

    Quote Originally Posted by mickey View Post
    Qt Code:
    1. vector <vector <float> > example;
    2. //...
    3. example[i][j] = atof(s.c_str()); //but here a runtime error uccurs
    To copy to clipboard, switch view to plain text mode 
    Did you initialise those vectors?

    Try this:
    Qt Code:
    1. vector<vector<float> > example(10, vector<float>(10, 0.0));
    2. example[i][j] = atof(s.c_str());
    To copy to clipboard, switch view to plain text mode 

    And then: is this the easier way to reach my aim? (I suspect it can be more simple).
    Yes. Use Qt

Similar Threads

  1. unable to save QCStrings properly in a buffer
    By nass in forum Qt Programming
    Replies: 13
    Last Post: 15th November 2006, 20:49
  2. QT string Find problem
    By kingslee in forum Qt Programming
    Replies: 2
    Last Post: 16th October 2006, 20:45
  3. Converting number to string.
    By safknw in forum Newbie
    Replies: 2
    Last Post: 18th September 2006, 12:12
  4. converting string to unsigned integer
    By mgurbuz in forum Qt Programming
    Replies: 4
    Last Post: 12th May 2006, 09:46
  5. How to get size (length, width ....) of string or char
    By Krishnacins in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2006, 09:55

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.