out of bounds

This is a discussion on out of bounds within the C++ Programming forums, part of the General Programming Boards category; I am trying to pass a string to a function and return the string to the main function. I keep ...

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    10

    out of bounds

    I am trying to pass a string to a function and return the string to the main function.
    I keep getting the following error:

    terminate called after throwing an instance of 'std:ut_of_range'
    what(): basic_string::substr

    I don't know what this means. Can anyone help me?
    If I put the same code inside main, the program runs fine. I try to put the code into it's own function and then I get the above error message.

  2. #2
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    You need to post some of the code you have, specifically the parts that are causing the error. It's difficult to tell when we can't see what's going on.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  3. #3
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Code:
    using namespace std;
    
    string parseColor(string);
    
    int main ()
    {
     string s;
     string info;
     string info2;
    
     cout << "Welcome to the Color Description Parser!" << endl;
     cout << endl;
     cout << "Enter your color description (on the next line): " << endl;
     getline(cin,s);
    
     info2= parseColor(s); 
    
      cout << "In standard format, your color is:  " << info << endl;
      cout << "Goodbye!" << endl;
    
    return 0;
    }
    
    string parseColor(string)
    {
    ....
    ...
    //I have a bunch of logic stuff in here that works fine if I put it in main
    
    
       //starting after the word "blue", find the first thing that isn't a space
       index_B2=s1.find_first_not_of(space,index7+4);
       index_B3=s1.find_first_of(junk,index_B2); //look for either a space or a comma
       colorBlue=s1.substr(index_B2,index_B3-index_B2);
       }
       
     info=color_name + " " + colorRed + " " + colorGreen + " " + colorBlue;
    
     }
    return info;
    }
    please help.

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    OOPs

    the line should have read:

    Code:
    info=parseColor(s);

  5. #5
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Check the variable you are returning in parseColor. If it is local to the function then you can have troubles, as it is no longer valid outside of the function.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  6. #6
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    I changed the variable to be a global variable but i am still getting the same error message.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,328
    Check index_B2 against string::npos. Do the same with index_B3. If either of those are equal to string::npos then that's the problem. That indicates the call to find_xxx failed to find a match.

    BTW, I'd switch back to a local variable instead of a global, I don't see anything wrong with your use of local vs global variables here.

  8. #8
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    Quote Originally Posted by Daved View Post
    I don't see anything wrong with your use of local vs global variables here.
    I wasn't trying to imply not using a local variable, but wanted to see about using it correctly.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  9. #9
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    Code:
    string parseColor(string)
    {
     int index1, index2, index3;
     unsigned int index4;
     int index5, index6,index7;
     int index_Gr1,index_Gr2;
     int index_R1,index_R2,index_R3,index_R4;
     int index_G1,index_G2,index_G3,index_G4;
     int index_B1,index_B2,index_B3,index_B4;
     int index_A,index_B;
     int comma1,comma2;
     string space (" ");
     string junk=(" "",");
     string color_name;
     int s_length;
     string color_num1;
     string colorRed,colorGreen,colorBlue;
     string s,info;
     
     index1=s.find("color");
     index2=s.find_first_not_of(space,index1+5);
     index3=s.find_first_of(space,index2+1);
     color_name=s.substr(index2,index3-index2);
     s_length=s.length();
    
     index4=s.find("gray",index3+1);
    //if color_name is gray do this:
     if(index4!=string::npos){
     index_Gr1=s.find_first_not_of(space,index4+4);
     index_Gr2=s.find(space,index_Gr1);
     color_num1=s.substr(index_Gr1,index_Gr2-index_Gr1);
     
     info=color_name+" "+color_num1+" "+color_num1+" "+color_num1;
    }
     
    //color_name is not gray
     else{
     index_A=s.find_first_not_of(space,index3+1);
     index_B=s.find_first_not_of(space,index_A+2);
     comma1=s.find(",",index_B+1);
     comma2=s.find(",",comma1+1);
    I keep getting the same error message:
    terminate called after throwing an instance of 'std:ut_of_range'
    what(): basic_string::substr

    I don't understand why if I put the exact code in main it works fine.
    Please help.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Code:
    string parseColor(string s)
    {
    Surely you want to use the parameter that gets passed in? And not the uninitialized s that you create there at the bottom?

  11. #11
    Registered User
    Join Date
    Feb 2009
    Posts
    10
    YES!!
    that's it.
    I am eternally grateful.
    Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Going beyond bounds problem
    By swgh in forum C++ Programming
    Replies: 4
    Last Post: 01-03-2009, 09:41 AM
  2. Array bounds checking
    By rohan_ak1 in forum C Programming
    Replies: 2
    Last Post: 08-26-2008, 10:16 PM
  3. Replies: 4
    Last Post: 01-16-2006, 04:58 PM
  4. out of bounds problem
    By chris285 in forum Game Programming
    Replies: 1
    Last Post: 04-26-2005, 09:00 AM
  5. Game matrix bounds checking
    By Panopticon in forum Game Programming
    Replies: 2
    Last Post: 02-04-2003, 09:30 PM

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