I'm getting an error where Stack around the variable 'dicerolls' was corrupted... Can anyone offer an insight as to why this is happening? Program executes perfectly until then
bool in_run= false;for (int i=0;i<=19;i++)
{
if (in_run) // i think this thing would never executed till the end of our universe
{
if (dice[i]!= dice[i-1])
{
cout<<")";
in_run=false;
}
}
Thank you for your input AbstractAnon and chipp...
I will try to fix the issues you've pointed out to me and chipp, I don't quite understand what you mean... line=4 if (in_run) // That can run because if the conditions in line 27 are met to start a "run" then that would be necessary to get a ) around the end of a run
void run_die(int dice[], int size)
{
//Sets up a bool to detect wether or not a run is present
bool in_run= false;
//For loop runs to check each pairing of numbers/rolls
for (int i=0;i<=size;i++)
{
//checks to see if a run is currently present
if (in_run)
{
//Keeps function in bounds
if (i>1)
{
//this statement checks to see if the current run ends
if (dice[i]!= dice[i-1])
{
//If the run in fact ends it outputs a ) and changes the bool to reflect current run status
cout<<")";
in_run=false;
}
}
}
//Checks to see if no run is currently present
if (!in_run)
{
//Keeps the fucntion within the index of the array
if(i<19)
{
//If there is a run starting and there is space in the array left... Then it outputs a ( and changes the run status to true
if (dice[i]==dice[i+1])
{
cout<<"(";
in_run=true;
}
}
}
//After each "check" for runs it prints the next roll
cout<<dice[i]<<" ";
}
//Checks if the last roll was also the end of a run
if (in_run)
{
cout<<") ";
}
}
//Create main function
int main()
{
//Creates a "seed" for random generator
srand(time(0));
//Initializing the array to hold all the random "rolls" to be 20 integers long "0-19"
int dicerolls[19];
//Size parameter for the function call of printing parentheses around detected runs
int size=19;
//Performs the 20 "rolls" and stores them into the dicerolls array to later be utlizied in the run_die function call
for (int i=0;i<=20;i++)
{
//Starting at i-1 because the first run through i=1 however the first roll is in dicerolls[0]
dicerolls[i-1]=rand()%6+1;
}
//Call the run detection function with the array and size of the array as parameters
run_die(dicerolls,size);
cout<<endl;
system("pause");
}
I added comments to hopefully try and convey what I was doing...
Also, I am still getting the same error but I thought I entered if statements to make sure I wouldn't make those out of bounds references... And I still can't find the issue :/
Okay, I finally figured it out and it runs with no errors! I will post the code that way you all can see that I actually got it! Thank you for the pointers, just what I needed! Thanks!!!!!
#include <cstdlib>
#include <ctime>
#include <iostream>
usingnamespace std;
void run_die(int dice[], int size)
{
//Sets up a bool to detect wether or not a run is present
bool in_run= false;
//For loop runs to check each pairing of numbers/rolls
for (int i=0;i<size;i++)
{
//checks to see if a run is currently present
if (in_run)
{
//Keeps function in bounds
if (i>1)
{
//this statement checks to see if the current run ends
if (dice[i]!= dice[i-1])
{
//If the run in fact ends it outputs a ) and changes the bool to reflect current run status
cout<<")";
in_run=false;
}
}
}
//Checks to see if no run is currently present
if (!in_run)
{
//Keeps the fucntion within the index of the array
if(i<19)
{
//If there is a run starting and there is space in the array left... Then it outputs a ( and changes the run status to true
if (dice[i]==dice[i+1])
{
cout<<"(";
in_run=true;
}
}
}
//After each "check" for runs it prints the next roll
cout<<dice[i]<<" ";
}
//Checks if the last roll was also the end of a run
if (in_run)
{
cout<<") ";
}
}
//Create main function
int main()
{
//Creates a "seed" for random generator
srand(time(0));
//Size parameter for the function call of printing parentheses around detected runs
int size=20;
//Initializing the array to hold all the random "rolls" to be 20 integers long "0-19"
int dicerolls[20];
//Performs the 20 "rolls" and stores them into the dicerolls array to later be utlizied in the run_die function call
for (int i=1;i<=size;i++)
{
//Starting at i-1 because the first run through i=1 however the first roll is in dicerolls[0]
dicerolls[i-1]=rand()%6+1;
}
//Call the run detection function with the array and size of the array as parameters
run_die(dicerolls,size);
cout<<endl;
system("pause");
}