1

I've been getting "Range check error"s while working with string arrays. To my understanding, this means I am using indexes that don't exist in my array.

My class and functions in my header file:

AnsiString toBin(int n) {
    AnsiString zero = "0";
    int len;
    AnsiString result;
    while (n != 0) {
        result += (n % 2 == 0 ? "0" : "1");
        n /= 2;
    }
    len = result.Length();
    while (result.Length() < 8)
        result = zero + result;
    return result;
}
                    
int toInt(AnsiString bin) {
    int start = 1;
    int result = 0;
    for (int i = 0; i < 8; i++) {
        result += (int)bin[i] * start;
        start * 2;
    }
    return result;
}
                
class enc_dec {
private:
    AnsiString stext;
    AnsiString skey;
                
public:
    enc_dec(AnsiString t, AnsiString k) {
        stext = t;
        skey = k;
    }
                
    AnsiString XOR() {
        if (skey == "") {
            Application->MessageBox(L"Error!", L"No Key", MB_OK);
            return "";
        }
        AnsiString cry = "";
        int key = skey[1] + 0;
        AnsiString keyBin = toBin(key);
        AnsiString temp = "";

        // AnsiString output[stext.Length()];
        std::vector<AnsiString>output;
        for (int i = 1; i < stext.Length(); i++) {
            temp = toBin(stext[i]);
            for (int j = 1; j < 8; j++) {
                if (temp[j] == keyBin[j])
                    temp[j] = '0';
                else
                    temp[j] = '1';
            }
            output.push_back(temp[i]);
        }
        for (int i = 0; i < stext.Length(); i++)
            cry += char(toInt(output[i]));
        return cry;
    }
};

My .cpp file:

void __fastcall TForm1::Button1Click(TObject *Sender){
    enc_dec temp(Edit1->Text,Edit2->Text);
    temp.XOR();
}

The design:

image

I found a mistake from before updating, The "Range Error" has not been resolved.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
yahya
  • 40
  • 4

1 Answers1

2

Unlike standard C++ strings and arrays, which are 0-indexed, AnsiString is 1-indexed. Its valid character indexes are 1 <= N <= Length, not 0 <= N < Length as your for loops are written for.

Also, in XOR(), return; should not compile, since XOR() is declared to return an AnsiString. You need to either return an actual value (ie return "";) or else throw an exception instead of using Application->MessageBox().

Also, AnsiString output[stext.Length()]; is not standard C++. You should use a std::vector or a System::DynamicArray for your array.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • I can see that Index 0 causes the range error now. I made the changes you suggested, but now the program crashes every time I try to run it. – yahya May 21 '21 at 20:00
  • @yahya then you still have other mistakes that you need to fix as well. But we can't see everything you are doing. Please update your question with a [mcve]. – Remy Lebeau May 21 '21 at 20:16
  • I have updated the question, I hope this is what you meant by minimal reproducible example. I created a new VLC from that only contains the elements linked to the problem. – yahya May 21 '21 at 20:56
  • A [mcve] would include examples of the input used and what the output is *supposed* to look like. What is the *goal* of this code? In any case, when you updated your `for` loops to start at index 1, you didn't update them to also use `<=` instead of `<`, so you are skipping the last character in your strings. Also, `start * 2;` is a no-op, since the result is not being saved anywhere – Remy Lebeau May 21 '21 at 22:31