943,542 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 4481
  • C++ RSS
You are currently viewing page 1 of this multi-page discussion thread
Apr 15th, 2008
0

Why does vector get out of range?

Expand Post »
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:
c++ Syntax (Toggle Plain Text)
  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
Reputation Points: 10
Solved Threads: 1
Light Poster
Arne Kristoffer is offline Offline
26 posts
since Apr 2008
Apr 15th, 2008
0

Re: Why does vector get out of range?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Apr 15th, 2008
0

Re: Why does vector get out of range?

Thanks, but after changing:

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

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

...it still doesn't work.
Reputation Points: 10
Solved Threads: 1
Light Poster
Arne Kristoffer is offline Offline
26 posts
since Apr 2008
Apr 15th, 2008
0

Re: Why does vector get out of range?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Apr 15th, 2008
0

Re: Why does vector get out of range?

Okay, I've changed the erasing part to this:
c++ Syntax (Toggle Plain Text)
  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."
Reputation Points: 10
Solved Threads: 1
Light Poster
Arne Kristoffer is offline Offline
26 posts
since Apr 2008
Apr 15th, 2008
0

Re: Why does vector get out of range?

Not even this would work!:

c++ Syntax (Toggle Plain Text)
  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!
Reputation Points: 10
Solved Threads: 1
Light Poster
Arne Kristoffer is offline Offline
26 posts
since Apr 2008
Apr 15th, 2008
0

Re: Why does vector get out of range?

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?
Reputation Points: 10
Solved Threads: 1
Light Poster
Arne Kristoffer is offline Offline
26 posts
since Apr 2008
Apr 15th, 2008
0

Re: Why does vector get out of range?

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:
C++ Syntax (Toggle Plain Text)
  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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005
Apr 16th, 2008
0

Re: Why does vector get out of range?

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.
Reputation Points: 10
Solved Threads: 1
Light Poster
Arne Kristoffer is offline Offline
26 posts
since Apr 2008
Apr 16th, 2008
0

Re: Why does vector get out of range?

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.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is online now Online
21,947 posts
since Aug 2005

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: The Visual Studio Common IDE Package
Next Thread in C++ Forum Timeline: Strange concurrent array overwrite





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC