std::out_of_range vector error

Hi there :)

I've been struggling with some code that was working fine until recently when I must have changed something and screwed it all up.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <string>
#include <vector>
#include <fstream>
#include <stdlib.h>
#include <sstream>
#include <iostream>
using namespace std;

int location[2];
string findesc;
void descOMeter();


int main(){
    location[0]=1000;
    location[1]=800;
    descOMeter();

return 0;
}

void descOMeter(){
    bool inareaX = false;
    bool inareaY = false;
    struct point{
    int x;
    int y;
    };
    size_t pos = 0;
    string delimiter = "~";
    point TL;
    point BR;
    vector<string> locstr(8);
    int n = 0;
    string myline;
    ifstream myfile ("Desc.info");
    string desc;
int linecheck = 0;
  if (myfile.is_open()){
      while(!myfile.eof()){
       getline(myfile,myline);
                while ((pos = myline.find(delimiter)) != string::npos) {

                        locstr.at(n) = myline.substr(0, pos);
                        myline.erase(0, pos + delimiter.length());
                        n++;
                        pos++;
                                if(n==8){
                                   TL.x  = atoi(locstr.at(0).c_str());
                                   TL.y  = atoi(locstr.at(1).c_str());
                                   BR.x  = atoi(locstr.at(2).c_str());
                                   BR.y  = atoi(locstr.at(3).c_str());
                                   desc = locstr.at(4);
                                   bool iswater = istringstream(locstr.at(5));
                                   string watersource = locstr.at(6);
                                   bool ismonsters = istringstream(locstr.at(7));

//cout << "At TLBR list " << TL.x << " " << TL.y << " " << BR.x << " " << BR.y << " " << desc << " " << iswater << " " << watersource << " " << ismonsters << " " << endl;

                                   inareaX == false;
                                   inareaY == false;
                                   if(location[0]<TL.x || location[0]>BR.x){
                                    inareaX = false;
                                    //cout << "Outside area, fails on X" << endl;
                                    }
                                    else{
                                    inareaX = true;
                                    //cout << "In areaX!" << endl;
                                    }
                                    if(location[1]<TL.y || location[1]>BR.y){
                                    inareaY = false;
                                    //cout << "Outside area, fails on Y" << endl;
                                    }
                                    else{
                                    inareaY = true;
                                    //cout << "In areaY!" << endl;
                                    }
                                    if(inareaX == true && inareaY == true){
                                       //cout << "inareaX and inareaY are true" << endl;
                                       findesc = desc;
                                       break;
                                    }
                                    n=0;
                                }

                            }

                }
        }
        else{
                cout << "File Desc.info could not be opened" << endl;
        }

 //cout <<  findesc << endl;
 //findesc = "You can't really see anything interesting";

myfile.close();

}


Somehow at some point n=8. I don't understand how and that is what is causing the out_of_range error I believe as that ends up trying to access locstr.at(8) which doesn't exist.

An example of Desc.info (the file I'm loading) is:

1
2
3
4
5
6
7
TLx(int)~TLy(int)~BRx(int)~BRy(int)~Description(string)~isWater(bool)~waterSource/block(string)~isMonsters(bool)~
820~620~1320~1000~The great Trindan Sea goes on for miles. Waves crash and water swirls around you. You think you might be able to see some far off fishing boats.~true~sea~true~
1420~660~1840~760~The great Trindan Sea goes on for miles. Waves crash and water swirls around you. You think you might be able to see some far off fishing boats.~true~sea~true~
1840~800~2120~980~The great Trindan Sea goes on for miles. Waves crash and water swirls around you. You think you might be able to see some far off fishing boats.~true~sea~true~
1840~800~2120~980~The great Trindan Sea goes on for miles. Waves crash and water swirls around you. You think you might be able to see some far off fishing boats.~true~sea~true~
820~420~840~460~You are in the bustling city of Imbataar. Traders and soldiers are everywhere.~true~well~false~
1680~460~1700~480~The spires of Umbat rise around you; blotting out the sky. Traders bustle around you and you can hear shouting and bartering.~true~well~false~



Thanks for any help in advance :)
Last edited on
At least put something that compiles

http://www.cplusplus.com/forum/general/112111/
Last edited on
Sorry, didn't copy the top bit of my file! 2 seconds and I'll edit the other bit in!

EDIT: Done! Thanks for pointing it out (Y)
Last edited on
foo.cpp: In function ‘void descOMeter()’:
foo.cpp:60:14: warning: statement has no effect [-Wunused-value]
foo.cpp:61:14: warning: statement has no effect [-Wunused-value]
foo.cpp:54:11: warning: unused variable ‘iswater’ [-Wunused-variable]
foo.cpp:56:11: warning: unused variable ‘ismonsters’ [-Wunused-variable]
foo.cpp:38:6: warning: unused variable ‘linecheck’ [-Wunused-variable]


1
2
3
4
5
6
7
//line 60, 61
inareaX == false; //== is comparison
inareaY == false;

//maybe you intended to write
inareaX = false; //= is assignment 
inareaY = false;



1
2
      while(!myfile.eof()){
       getline(myfile,myline);
don't loop on eof, you'll reach it too late.
instead, loop on the reading operation while( getline(myfile, myline) )

78
79
80
81
82
83
if(inareaX == true && inareaY == true){
    //cout << "inareaX and inareaY are true" << endl;
    findesc = desc;
    break;
}
n=0;
if you `break' in line 81, line 83 would not execute so `n' would not reset.
Ah, that makes sense :). Cheers, been racking my brains all day over this.
Topic archived. No new replies allowed.