Why does vector get out of range?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Apr 2008
Posts: 26
Reputation: Arne Kristoffer is an unknown quantity at this point 
Solved Threads: 1
Arne Kristoffer's Avatar
Arne Kristoffer Arne Kristoffer is offline Offline
Light Poster

Why does vector get out of range?

 
0
  #1
Apr 15th, 2008
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:
  1. //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.
  2. //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,
  3. //"2344", "Arne Kristoffer" and "Abc 3"
  4. vector<string> getInfo(const string &an)
  5. {
  6. string str;
  7. ifstream MyFile("data.txt");
  8.  
  9. vector<string> vec;
  10. vec.push_back("");
  11. vec.push_back("");
  12. vec.push_back("");
  13. vec.push_back("");
  14.  
  15. int position = 0;
  16.  
  17. vector<string> vectorOfInformation;
  18. string line;
  19.  
  20. // Reads all info from file into an vector
  21. while(getline(MyFile, line))
  22. vectorOfInformation.push_back(line);
  23.  
  24. //loops through the "file"
  25. for(int iter = 0; iter != vectorOfInformation.size(); iter++)
  26. {
  27. //Clears the temporary storage vector for new contents.
  28. vec.clear();
  29. vec.push_back("");
  30. vec.push_back("");
  31. vec.push_back("");
  32. vec.push_back("");
  33. vec.push_back("");
  34.  
  35. //Loops through vectorOfInformation[i], and store the contents of the string (or is it a substring, but anyway...) in the vector<string> vec.
  36. for (size_t i=0; i < vectorOfInformation[iter].length(); i++)
  37. {
  38.  
  39. if (vectorOfInformation[iter][i] != '(' || vectorOfInformation[iter][i] != ')')
  40. {
  41. vec[position] +=vectorOfInformation[iter][i];
  42. if (str[i] == ')')
  43. position++;
  44. }
  45. }
  46.  
  47. // We now have an vector (vec) consisting of an unknown number of elements. First, we have to split away the "(" and ")":
  48.  
  49. int iteriter = 0;
  50. for (int x = 0; x != vec.size(); x++)
  51. {
  52. vec[iteriter].erase(vec[0].length()-1);
  53. vec[iteriter].erase(0,1);
  54. iteriter++;
  55. }
  56.  
  57. //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.
  58.  
  59. if (vec[0] == an)
  60. {
  61. return vec;
  62. }
  63. }
  64.  
  65. //We now have looped through the entire text file, but found nothing. We have to return an empty vector.
  66. vector<string> empty;
  67. return empty;
  68.  
  69. }

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
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why does vector get out of range?

 
0
  #2
Apr 15th, 2008
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.
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 26
Reputation: Arne Kristoffer is an unknown quantity at this point 
Solved Threads: 1
Arne Kristoffer's Avatar
Arne Kristoffer Arne Kristoffer is offline Offline
Light Poster

Re: Why does vector get out of range?

 
0
  #3
Apr 15th, 2008
Thanks, but after changing:

str[i] to if (vectorOfInformation[iter][i] == ')')

And inserting vec.resize(5,"");

...it still doesn't work.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why does vector get out of range?

 
0
  #4
Apr 15th, 2008
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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 26
Reputation: Arne Kristoffer is an unknown quantity at this point 
Solved Threads: 1
Arne Kristoffer's Avatar
Arne Kristoffer Arne Kristoffer is offline Offline
Light Poster

Re: Why does vector get out of range?

 
0
  #5
Apr 15th, 2008
Okay, I've changed the erasing part to this:
  1. for (int x = 0; x != vec.size(); x++)
  2. {
  3. vec[x].erase(vec[x].length()-1);
  4. vec[x].erase(0,1);
  5.  
  6. }

And it compiles.

But if I write this in main():

cout << getInfo("5135")[0];

It says "Vector out of range."
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 26
Reputation: Arne Kristoffer is an unknown quantity at this point 
Solved Threads: 1
Arne Kristoffer's Avatar
Arne Kristoffer Arne Kristoffer is offline Offline
Light Poster

Re: Why does vector get out of range?

 
0
  #6
Apr 15th, 2008
Not even this would work!:

  1. int main()
  2. {
  3. string *ptr = &getInfo("5656")[0];
  4. cout << *ptr;
  5. return 0;
  6. }

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!
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 26
Reputation: Arne Kristoffer is an unknown quantity at this point 
Solved Threads: 1
Arne Kristoffer's Avatar
Arne Kristoffer Arne Kristoffer is offline Offline
Light Poster

Re: Why does vector get out of range?

 
0
  #7
Apr 15th, 2008
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?
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why does vector get out of range?

 
0
  #8
Apr 15th, 2008
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 -- (2344)(Arne Kristoffer)(Abc 3)
It crashes here:
  1. for (int x = 0; x != vec.size(); x++)
  2. {
  3. vec[x].erase(vec[x].length()-1);
  4. vec[x].erase(0,1);
  5.  
  6. }

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 26
Reputation: Arne Kristoffer is an unknown quantity at this point 
Solved Threads: 1
Arne Kristoffer's Avatar
Arne Kristoffer Arne Kristoffer is offline Offline
Light Poster

Re: Why does vector get out of range?

 
0
  #9
Apr 16th, 2008
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.
Last edited by Arne Kristoffer; Apr 16th, 2008 at 4:18 am.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,454
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1476
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Why does vector get out of range?

 
0
  #10
Apr 16th, 2008
Originally Posted by Arne Kristoffer View Post
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.
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.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC