mercredi 18 janvier 2023

C++ Error 0xC0000094: Integer division by zero [closed]

Keep getting this error for an unhandled exception in my C++ function whenever I try to generate a random integer between 0 & the size of a vector.

relevant code:

std::string Mountains::getRandomMountain()
{
    int randomValue = 0;
    string randomMountain;

    randomValue = rand() % 4;

    if (randomValue == 0) {
        randomValue = rand() % alpMountains.size();
        randomMountain = alpMountains[randomValue];
    }
    else if (randomValue == 1) {
        randomValue = rand() % pyreneesMountains.size();
        randomMountain = pyreneesMountains[randomValue];
    }
    else if (randomValue == 2) {
        randomValue = rand() % carpathiansMountains.size();
        randomMountain = carpathiansMountains[randomValue];
    }
    else if (randomValue == 3) {
        randomValue = rand() % icelandicHighlandsMountains.size();
        randomMountain = icelandicHighlandsMountains[randomValue];
    }

    return randomMountain;
}

It's main purpose is to select a random string from one of the 4 vectors and then assign it to the randomMountain variable, which is then returned. Also the error only occurs on the first else if, if that is any help?




Aucun commentaire:

Enregistrer un commentaire