Coming soon - Get a detailed view of why an account is flagged as spam!
view details
1
Out of range exception which shoud not be thrown
Post Flair (click to view more posts with a particular flair)
Post Body

Hey, so this is the current method:

void move(int &movesMade, std::pair<int,int> &currentPos, vector<std::pair<int,int>> &visitedFields, std::vector<int> &lastUsedRule, std::vector< std::pair<int,int> > &longestRoute, rulebook &rules ) {
     if(lastUsedRule.size() >= 1) {
        if (currentPos.first == 0 && currentPos.second == 0 && lastUsedRule.at(0) == (rules.getPossibleMoves() - 1) ) {
            return;
        }
    }

When I try to run the whole program, it throws an out of bounds exception. The thing is: I know that the exception occurs because of "lastUsedRule.at(0)" inside of the if-condition, because std::vector throws it, if the given parameter for the at-function is greater or equal to the size of the vector. The vector is empty, so its size is 0 and so it produces an exception. To avoid exactely this situation the programm checks whether the size is greater than 0 - Still it throws the exception. I don't understand it, to be honest.

What can I change to get this function working? Thanks in advance.

EDIT: Forgot the rest of the code (Here is the task I'm working on https://www.reddit.com/r/dailyprogrammer/comments/6dgiig/20170526_challenge_316_hard_longest_uncrossed/ ):

#include <cstdlib>
#include <iostream>
#include <vector>
#include <array>
#include <utility>
#include <algorithm>

using namespace std;


struct rulebook {
    std::vector< std::pair<int,int> > moves {
        { std::pair<int,int>{1,2} }, 
        { std::pair<int,int>{1,-2} },
        { std::pair<int,int>{-1,2} },
        { std::pair<int,int>{-1,-2} },
        { std::pair<int,int>{2,1} },
        { std::pair<int,int>{2,-1} },
        { std::pair<int,int>{-2,1} },
        { std::pair<int,int>{-2,-1} }
    };
    const int n;

    rulebook(int range) : n(range) {};
    int getN() { return n; };
    int getPossibleMoves() {
        return moves.size();
    }
};

bool outOfBounds(std::pair<int,int> currentPos, std::pair<int,int> destination, int n) {

}

bool illegalMove (std::pair<int,int> currentPos, std::pair<int,int> destination, vector<std::pair<int,int>> visitedFields, rulebook rules) {

}

    void move(int &movesMade, std::pair<int,int> &currentPos, vector<std::pair<int,int>> &visitedFields, std::vector<int> &lastUsedRule, std::vector< std::pair<int,int> > &longestRoute, rulebook &rules ) {
         if( !lastUsedRule.empty() ) {
            if (currentPos.first == 0 && currentPos.second == 0 && lastUsedRule.at(0) == (rules.getPossibleMoves() - 1) ) {
                return;
            }
        }

    /*if (movesMade > 0 && !lastUsedRule.empty()) {
        if ( lastUsedRule.at(movesMade) == rules.getPossibleMoves() ) {
            movesMade--;
            currentPos.first = visitedFields.at(movesMade).first;
            currentPos.second = visitedFields.at(movesMade).second;
            visitedFields.pop_back();
            lastUsedRule.pop_back();
            return;
        }
    }
    if ( movesMade == lastUsedRule.size() ) {
        int temp = 0;
        lastUsedRule.push_back(temp);
    }*/

}

int main(int argc, char** argv) {
    int n;
    int movesMade = 0;
    pair<int, int> position = {0,0};
    std::vector< std::pair<int,int> > visitedFields;
    std::vector<int> lastUsedRule;
    std::vector< std::pair<int,int> > longestRoute;

    visitedFields.push_back(position);
    cout << "Enter the board length:" << endl;
    cin >> n;
    rulebook rules(n);

    move(movesMade, position, visitedFields, lastUsedRule, longestRoute, rules);

    return 0;
}

EDIT2: I know that there are also errors in the rest of the given method, probably because of the same reasons. I commented everything out but the snippet above.

Author
Account Strength
100%
Account Age
9 years
Verified Email
Yes
Verified Flair
No
Total Karma
50,418
Link Karma
37,137
Comment Karma
11,040
Profile updated: 6 days ago
Posts updated: 9 months ago

Subreddit

Post Details

We try to extract some basic information from the post title. This is not always successful or accurate, please use your best judgement and compare these values to the post title and body for confirmation.
Posted
7 years ago