| | |
Why does vector get out of range?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Hi!
I have this function (well, it's pretty good explained in the code-tag, but okay.) which is supposed to see if a string is found first in a line in a text file. Anyway, this code gives the message (while running in debug mode in MSVC++):
"Debug assertion failed. Expression: Vector subscript out of range"
This is my function. I'm sorry, I know it's a long code, but I don't know whats wrong, so I wouldn't cut to much off it.
I have also added comments, so it would be easyer to read.
Code:
I'm new to this language, so please, don't make a new code for me, just point out the things ive done wrong, and I will correct it.

By the way, here is a link to pastebin:
http://pastebin.com/m36979d03
Arne Kristoffer
I have this function (well, it's pretty good explained in the code-tag, but okay.) which is supposed to see if a string is found first in a line in a text file. Anyway, this code gives the message (while running in debug mode in MSVC++):
"Debug assertion failed. Expression: Vector subscript out of range"
This is my function. I'm sorry, I know it's a long code, but I don't know whats wrong, so I wouldn't cut to much off it.
I have also added comments, so it would be easyer to read.
Code:
c++ Syntax (Toggle Plain Text)
//getInfo function. Returns an empty vec if an (account name) is not found. If an is found, the function will return the rest of the contents of the line starting with an in the text file. //An example of a line in the text file could be: "(2344)(Arne Kristoffer)(Abc 3)". In this example, if we called getInfo("2344"), it would return a vector consisting of three elements, //"2344", "Arne Kristoffer" and "Abc 3" vector<string> getInfo(const string &an) { string str; ifstream MyFile("data.txt"); vector<string> vec; vec.push_back(""); vec.push_back(""); vec.push_back(""); vec.push_back(""); int position = 0; vector<string> vectorOfInformation; string line; // Reads all info from file into an vector while(getline(MyFile, line)) vectorOfInformation.push_back(line); //loops through the "file" for(int iter = 0; iter != vectorOfInformation.size(); iter++) { //Clears the temporary storage vector for new contents. vec.clear(); vec.push_back(""); vec.push_back(""); vec.push_back(""); vec.push_back(""); vec.push_back(""); //Loops through vectorOfInformation[i], and store the contents of the string (or is it a substring, but anyway...) in the vector<string> vec. for (size_t i=0; i < vectorOfInformation[iter].length(); i++) { if (vectorOfInformation[iter][i] != '(' || vectorOfInformation[iter][i] != ')') { vec[position] +=vectorOfInformation[iter][i]; if (str[i] == ')') position++; } } // We now have an vector (vec) consisting of an unknown number of elements. First, we have to split away the "(" and ")": int iteriter = 0; for (int x = 0; x != vec.size(); x++) { vec[iteriter].erase(vec[0].length()-1); vec[iteriter].erase(0,1); iteriter++; } //We now have the clean vec, with no "(" and ")", and have to decide wether to return vec (which means that vec[0] == an) or not. if (vec[0] == an) { return vec; } } //We now have looped through the entire text file, but found nothing. We have to return an empty vector. vector<string> empty; return empty; }
I'm new to this language, so please, don't make a new code for me, just point out the things ive done wrong, and I will correct it.

By the way, here is a link to pastebin:
http://pastebin.com/m36979d03
Arne Kristoffer
lines 10-13: not needed. If you want a vector to start with a specific size then just call its resize() method
line 42: str is an empty string, so attempting to access anything beyone str[0] is an error.
line 42: str is an empty string, so attempting to access anything beyone str[0] is an error.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
I think you need to learn how to use your compiler's debugger so that you don't have to ask us to do your debugging for you. If you used your compiler's debugger you would quickly find that the problem now is at line 52.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Okay, I've changed the erasing part to this:
And it compiles.
But if I write this in main():
cout << getInfo("5135")[0];
It says "Vector out of range."
c++ Syntax (Toggle Plain Text)
for (int x = 0; x != vec.size(); x++) { vec[x].erase(vec[x].length()-1); vec[x].erase(0,1); }
And it compiles.
But if I write this in main():
cout << getInfo("5135")[0];
It says "Vector out of range."
Not even this would work!:
Yes, I tried to use my debugger, and it sets a breakpoint at the line "cout << *ptr;"
I think we're close to a solution!
c++ Syntax (Toggle Plain Text)
int main() { string *ptr = &getInfo("5656")[0]; cout << *ptr; return 0; }
Yes, I tried to use my debugger, and it sets a breakpoint at the line "cout << *ptr;"
I think we're close to a solution!
Okay, this is what I've done for now:
http://pastebin.com/m2aa328fb
1: It somehow works, but not as expected.
2: the info-vector (defined in main()) info[0] will allways be "empty".
Anyone?
http://pastebin.com/m2aa328fb
1: It somehow works, but not as expected.
2: the info-vector (defined in main()) info[0] will allways be "empty".
Anyone?
I compiled your program with VC++ 2008 Express and it crashes bigtime. I don't know what compiler you are using but I'd suggest you get a different compiler. The file I used contain just one line that you had in comments at the top of your program --
It crashes here:
If you can't take the time to use a decent compiler with a great debugger then I don't have the time to do your debugging for you any more.
(2344)(Arne Kristoffer)(Abc 3) It crashes here:
C++ Syntax (Toggle Plain Text)
for (int x = 0; x != vec.size(); x++) { vec[x].erase(vec[x].length()-1); vec[x].erase(0,1); }
If you can't take the time to use a decent compiler with a great debugger then I don't have the time to do your debugging for you any more.
Last edited by Ancient Dragon; Apr 15th, 2008 at 8:53 pm.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
Well, I used VC++ 2008 Express, and it compiles fine, and it don't crash.
What's strange, is that, if I add "cout << an;" in the start of the function, it prints an. But if I type it inside the for(int iter = 0; iter != vectorOfInformation.size(); iter++), it don't print.
Do you know about any easy to understand guides for VC++ debugging?
EDIT: I've tried to learn some debugging, and it appears that vectorOfInformation don't get filled with any information! It's empty after the while(getline())-loop, which should append all the lines in the textfile to it.
What's strange, is that, if I add "cout << an;" in the start of the function, it prints an. But if I type it inside the for(int iter = 0; iter != vectorOfInformation.size(); iter++), it don't print.
Do you know about any easy to understand guides for VC++ debugging?
EDIT: I've tried to learn some debugging, and it appears that vectorOfInformation don't get filled with any information! It's empty after the while(getline())-loop, which should append all the lines in the textfile to it.
Last edited by Arne Kristoffer; Apr 16th, 2008 at 4:18 am.
I use the same compiler, and that's why the program doesn't crash for you. Put a check after the open statement to see if the file was actually opened. I had that problem once and had to change the location of the file.
Don't PM me with questions -- you might get a nasty PM in response. If you have a question then post it in one of the forums.
![]() |
Other Threads in the C++ Forum
- Previous Thread: The Visual Studio Common IDE Package
- Next Thread: Strange concurrent array overwrite
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class classes code coding compile compiler console conversion convert count data database delete deploy developer dll download dynamiccharacterarray email encryption error file forms fstream function functions game generator getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






