I tryed looking up things that have to do with setting a variable that is already set causing a crash and I found nothing on-topic for this.
Rextblock is a user input number. Returns random number between numbermax and 2 if textblock is equal to 1.
if ( textblock == 1 )
{
int rand1 = rand() %(numbermax - 2) + 2;
int textbox = rand1;
}
When I input 1 for textblock it crashes, i’m pretty sure this code is causing it.
using namespace std;
int main(int argc, char *argv[])
{
srand(time(NULL));
int numbermax = 2;
while (while1 < 1)
{
cout << "input the number of the text block you wish to use, \n ";
int textblock;
cin >> textblock;
if ( textblock != 0 )
{
}
if ( textblock == 1 )
{
int rand1 = rand() %(numbermax - 2) + 2;
int textbox = rand1;
}
if (textblock == 2)
{
}
if (textblock == 2)
{
}
if( textblock == 0 )
{
}
if ( textblock == 1 )
{
cout << " \n error, try again \n";
}
I highly suspect that when you run it,
numbermaxis equal to 2. This would causenumbermax - 2to equal zero, and using modulus with a rhs of 0 invokes division by 0, which is undefined behaviour, hence the fair probability of a crash.You should be making sure that it doesn’t end up doing that via a check of
if (numbermax != 2).