-2

I am trying to use this function and I was able to debug and narrow it down to this variable called addrstore. I don't know why it is giving me this error. I have tried variables, set numbers, and such but I can't seem to get it to work without it erroring out on me. I am able to ignore the error and the program seems to work just fine but every time I run this, it gives me the error, "String subscript out of range."

Here is the code

void loading(string file){
ifstream object;
BYTE var;
ADDRESS addr;
string input;

object.open(file);
if (!object){
    cout << "ERROR: Could not load " << file << endl;
}
else{
    string line;
    string addrstore;
    cout << "loaded the file " << file << endl;
    while (!object.eof()){
        getline(object, line);
        addr = 0;
        if (line[0] == 'T'){
            for (int x = 1; x < 7; x++){
                addrstore[x - 1] = 0;
                //addrstore[x - 1] = line[x];
            }
            addr = numconvert(addrstore);
        }
        int i = 7;
        while (line.size() != 0 && line[0] == 'T' && i < line.size()){
            input = line[i] + line[i + 1];
            var = numconvert(input);
            PutMem(addr, &var, 0);
            i += 2;
            addr++;
        }
    }
    object.close();
}
}
Yu Hao
  • 119,891
  • 44
  • 235
  • 294
  • This is C++, not C. Please update the tags. :) – TheBigH Dec 04 '14 at 09:02
  • ...but I suspect the issue is that you are referring to line[i+1], when line actually has only elements line[0] through line[i-1]. – TheBigH Dec 04 '14 at 09:03
  • 1
    You are using `line[0]` without checking that your `line` is non-empty. Also, see http://stackoverflow.com/questions/5431941/while-feof-file-is-always-wrong – CB Bailey Dec 04 '14 at 09:05
  • 1
    The cause of the issue is the line `addrstore[x - 1] = 0;` Since `addrstore` is never initialized to anything, it has size `0` and any index is out of range. Note that @TheBigH is also correct and when you fix the `addrstore` issue, you reference an out-of-bounds index of `line`. – Bart van Nierop Dec 04 '14 at 09:09
  • You only have one line that indexes `addrstore`, it should be pretty obvious that's where the error is. – Jonathan Wakely Dec 04 '14 at 09:19
  • 2
    Have you considered the possibility that the string subscript is out of range? – Raymond Chen Dec 04 '14 at 09:45

1 Answers1

0

You never initialize the variable addrstore and assign some value to it at any place in your program. This is causing an issue.

Also without assigning value to variable anywhere else how can you access like addrstore[x - 1]

SHR
  • 7,940
  • 9
  • 38
  • 57
Sridhar DD
  • 1,972
  • 1
  • 10
  • 17