parsing problem

This is a discussion on parsing problem within the C++ Programming forums, part of the General Programming Boards category; I'm writing a console app to ask the users for the edges of a graph and compute the MST for ...

  1. #1
    Registered User
    Join Date
    Nov 2007
    Posts
    2

    parsing problem

    I'm writing a console app to ask the users for the edges of a graph and compute the MST for the give nodes but am having trouble parsing the input. The data is give in the form:

    (node1,node2) weight

    and the following code will extract the 2 nodes but I constantly get an out_of_range exception when trying to get the get the weight.

    Code:
    int main()  {
      string cmd;
      int startIdx, endIdx, length;
      int vert1, vert2, weight;
    
      // command loop, ends with EOF
      cout << ": ";
      while(true) {
        cin >> cmd;
        
        // terminate on EOF
        if(cin.eof() != 0) {
          cout << endl;
          return 0;
        }
    
        // parse input    
        length = cmd.capacity() - 1;
        startIdx = cmd.find('(', 0);
        endIdx = cmd.find(',', startIdx);
        vert1 = atoi((cmd.substr(startIdx + 1, endIdx - startIdx - 1)).c_str());
      
        startIdx = endIdx;
        endIdx = cmd.find(')', startIdx);
        vert2 = atoi((cmd.substr(startIdx + 1, endIdx - startIdx - 1)).c_str());
    
        startIdx = endIdx + 1;
        endIdx = length;
        weight = atoi((cmd.substr(startIdx + 1, endIdx - startIdx)).c_str());
    
        cin.clear();
        cin.ignore(numeric_limits<streamsize>::max(), '\n');
    
    cout << "From: " << vert1 << "  To: " << vert2 << "  Weight: " << weight << endl;
      
      
        cout << ": ";
      }
    
      return 0;
    }
    Everything I've read says I can call substr() with the start index alone and it will return a string from the index to the end of the string but that gives me the same exception. I've tried with a length of 1 when I provide a 4-digit weight (so I know I'm not running past the end of the string) and still get the same exception.

    Could someone please explain what I'm not getting about substr()?

    Thanks.

    Jasen

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,702
    >> cin >> cmd;
    That will not read past white spaces. You can replace it with the following to read an entire line at a time:
    >> getline(cin, cmd);

    You should also check the return value of find() for string::npos in order to detect bad input.

    gg

  3. #3
    Registered User
    Join Date
    Nov 2007
    Posts
    2
    Ah, great. Knew it had to be something silly on my part.

    Thanks!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem parsing 16-bit Hex to ASCII
    By black_spot1984 in forum C Programming
    Replies: 12
    Last Post: 12-05-2006, 11:13 PM
  2. Laptop Problem
    By Boomba in forum Tech Board
    Replies: 1
    Last Post: 03-07-2006, 06:24 PM
  3. Replies: 5
    Last Post: 11-07-2005, 11:34 PM
  4. pointer problem
    By R.Stiltskin in forum C Programming
    Replies: 25
    Last Post: 10-20-2005, 06:02 PM

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