-4

I'm trying to compare elements of the "coordinateList" vector with elements of "Buttons" vector and if four different if-statements are okay to pass them, increase 1 in "countList" vector. But, the problem is that whenever I input 2 numbers for "coordinateList" which can't pass the if-statement, it gets "vector subscript out of range" on the part;

if ((coordinateList[j][0] >= Buttons[i][0]) 
    && (coordinateList[j][0] <= Buttons[i][1]) 
    && (coordinateList[j][1] >= Buttons[i][2]) 
    && (coordinateList[j][1] <= Buttons[i][3]))

I don't know how to fix it.

#include <iostream>
#include <vector>

using namespace std;

int main() {

    int numberOfButtons;
    int numberOfClicks;

    cin >> numberOfButtons;

    cin >> numberOfClicks;

    int buttonCoordinate;
    vector <vector<int> > Buttons;

    for (unsigned int i = 0; i < numberOfButtons; i++) {
        vector<int> oneButtonCoordinate;
        for (unsigned int j = 0; j < 4; j++) {
            cin >> buttonCoordinate;
            oneButtonCoordinate.push_back(buttonCoordinate);
        }
        Buttons.push_back(oneButtonCoordinate);
    }



    int XYCoordinate;
    vector <vector<int> > coordinateList;
    vector <int> clickCount;

    for (unsigned int i = 0; i < numberOfClicks; i++) {
        vector<int> oneClickCoordinate;
        for (unsigned int j = 0; j < 2; j++) {
            cin >> XYCoordinate;
            oneClickCoordinate.push_back(XYCoordinate);
        }
        coordinateList.push_back(oneClickCoordinate);
    }


    for (unsigned int i = 0; i < numberOfButtons; i++) {
        clickCount.push_back(0); 
    }

    for (unsigned int j = 0; j < numberOfClicks; j++) {
        for (unsigned int i = Buttons.size() - 1; i >= 0; i--) {
            if ((coordinateList[j][0] >= Buttons[i][0]) && (coordinateList[j][0] <= Buttons[i][1]) && (coordinateList[j][1] >= Buttons[i][2]) && (coordinateList[j][1] <= Buttons[i][3])) {
                clickCount.at(i) += 1;
                break; 
            }
        }
    }


    (skip)


    return 0;
}

Example of correct Input and Output

Input

2 5

1 5 1 5

3 8 3 8

1 1

3 3

3 5

8 8

3 10

Output

Button: #1: 1

Button: #2: 3
Scheff's Cat
  • 19,528
  • 6
  • 28
  • 56
Lua
  • 1
  • 4
    Did you try stepping through your code with a debugger, to see where, exactly, this exception, is coming from, and which index is invalid? – Algirdas Preidžius Sep 20 '19 at 14:32
  • 2
    `for (unsigned int i = Buttons.size() - 1; i >= 0; i--)`. Be careful with decrementing iterators. This loop never ends since an unsigned integer will always be `>= 0`. – Gilles-Philippe Paillé Sep 20 '19 at 14:38

1 Answers1

1

so it is the line

for (unsigned int i = Buttons.size() - 1; i >= 0; i--)

which ends up in an invalid index (0xFFFFFFFF still >= 0!).

Use instead:

for (int i = (int) Buttons.size() - 1; i >= 0; i--)
mommos
  • 189
  • 5
  • First you should avoid C-style casts. Then you should check that the value doesn't overflow when you cast from `unsigned int` to `int`. That's dangerous. – Thomas Sablik Sep 20 '19 at 19:13