Thread: substr problems

  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
    28,413
    Try printing line to see what it contains.
    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
    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
    28,413
    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".
    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
    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, 05: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, 09:19 PM
  5. DJGPP problems
    By stormswift in forum C Programming
    Replies: 2
    Last Post: 02-26-2002, 04:35 PM