substr problems

This is a discussion on substr problems within the C++ Programming forums, part of the General Programming Boards category; I am having problems splitting 1 string into sub strings. the section of code is as follows getline ( file_1, ...

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

    Question substr problems

    I am having problems splitting 1 string into sub strings.
    the section of code is as follows


    getline ( file_1, line );
    index = line.find(",",1);
    time = line.substr(0,index);
    prob = line.substr(index,4); //works when rem'd

    line, time and prob are strings
    index is size_t

    The file i am ready has the data in the following format

    10,0.00,0,0,,,,,

    When the program runs I get a abnormal program termination. with the prob line removed the prog runs fine with no errors.

    I have tried casting index to int before the prob line and changed the substr format to line.substr(index); but nothing works

    Can anyone point out where I am going wrong

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    Try printing line to see what it contains.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    line contains what it should, 10,0.00,0,0,,,,

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    line contains what it should, 10,0.00,0,0,,,,
    hmm... this seems to work for me, so I cannot really duplicate your problem:
    Code:
    #include <string>
    #include <iostream>
    
    int main()
    {
        std::string line("10,0.00,0,0,,,,");
        std::string::size_type index = line.find(",",1);
        std::string time = line.substr(0,index);
        std::string prob = line.substr(index,4);
    
        std::cout << time << "\n" << prob << "\n";
    }
    Of course, you have a slight off by one mistake in that prob should be line.substr(index + 1, 4), but I certainly do not get an "abnormal program termination".
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2007
    Posts
    12
    thanks - i got it sorted now
    the prog failed due to reading past the end of a file, but why rem'ing that line allowed it work i don't know.

    i'll try leaving things for an hour before posting problems in future.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. No clue how to make a code to solve problems!
    By ctnzn in forum C Programming
    Replies: 8
    Last Post: 10-16-2008, 02:59 AM
  2. String Manipulation problems -_-
    By Astra in forum C Programming
    Replies: 5
    Last Post: 12-13-2006, 04:48 PM
  3. Rendering problems (DirectX?)
    By OnionKnight in forum Tech Board
    Replies: 0
    Last Post: 08-17-2006, 12:17 PM
  4. contest problems on my site
    By DavidP in forum Contests Board
    Replies: 4
    Last Post: 01-10-2004, 08:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 03:35 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21