Jump to content

cannot find my mistake (c++)

Go to solution Solved by MisterWhite,

Solved: i deleted my allocated memory before even using it.

Next mistake is here: i get the same mistake code:

for(int i=0; i<counter; i++)    {        int x=startX[i], y=startY[i];            for(int j=startX[i]; j<endX[i]; j++)// from startX until endX            {                for(int k=startY[i]; k<endY[i]; k++)// from startY to endY                {                    if(mode[i]=="turn on")                    beatNeighbour[x][y]=1;                    else if (mode[i]=="turn off")                        beatNeighbour[x][y]=0;                    else if(mode[i]=="toggle")                    {                        if(beatNeighbour[x][y]==0)                           beatNeighbour[x][y]=1;                        else if( beatNeighbour [x][y]==1)                            beatNeighbour[x][y]=0;                    }                    y++;                }                x++;            }    }

So  i have this prob for like 2 days now. i've narrowed it down to my function "duomenys" (i read text file there). when i comment everything out no error. when i leave the while loop and comment the for error hapens, when for is commented but while is not also error. error (black window) looks like this: http://support.eset.com/Platform/Publishing/images/Authoring/Image%20Files/ESET/KB%20Team%20Only/SOLN2716/SOLN2716Fig1-7.png

error code when closing "main.exe stopped working": Process returned -1073741819 (0xC0000005)

 

ADDED A COMMENT

so here's the full code:

 #include <iostream>#include <fstream>#include <string>#include <sstream>using namespace std;void duomenys(string mode1[], string mode2[], int startX[], int startY[], int endX[], int endY[],int **beatNeighbour, int &counter);void turnoffon(string mode[], string mode1[], string mode2[], int counter);int main(){    cout<<" a";    string mode[1000], mode1[1000], mode2[1000];    int startX[1000],startY[1000], endX[1000],endY[1000];    int **beatNeighbour= new int*[1000];    int counter=0;    for(size_t i=0; i<1000; i++)        beatNeighbour[i]=new int [1000];    duomenys(mode1, mode2,startX,startY,endX,endY,beatNeighbour,counter);    turnoffon(mode,mode1,mode2,counter);    //************************    // real solution starts here    // 0 - lights off ; 1 - lights on    //************************    for(int i=0; i<counter; i++)    {        int x=startX[i], y=startY[i];        for(int j=startX[i]; j<=endX[i]; j++)// from startX until endX            {                for(int k=startY[i]; k<=endY[i]; k++)// from startY to endY                {                    if(mode[i]=="on")                    beatNeighbour[x][y]=1;                    else if (mode[i]=="off")                        beatNeighbour[x][y]=0;                    else if(mode[i]=="toggle")                    {                        if(beatNeighbour[x][y]==0)                           beatNeighbour[x][y]=1;                        else if( beatNeighbour [x][y]==1)                            beatNeighbour[x][y]=0;                    }                    y++;                }                x++;            }    }    int count=0;    for(int i=0; i<1000; i++)    {        for(int j=0; j<1000; j++)        {            if(beatNeighbour[i][j]==1)                count++;        }    }    cout<<count;    for(size_t i=1000; i>0; --i)    {        delete[] beatNeighbour[i];    }    delete[] beatNeighbour;    return 0;}void duomenys(string mode1[], string mode2[], int startX[], int startY[], int endX[], int endY[], int **beatNeighbour,int &counter){    ifstream fd("duomenys.txt");     while (!fd.eof())    {        string through, numbers;        char temp;        fd>>mode1[counter];        if(mode1[counter]=="turn")        {            fd>>mode2[counter];        }        fd>>numbers;        stringstream ss(numbers);// geting the start coordinates        ss>>startX[counter]>>temp>>startY[counter];        fd>>through>>numbers;        stringstream sss(numbers);// getting end coordinates        sss>>endX[counter]>>temp>>endY[counter];        counter++;    }    fd.close();    for(int i=0; i<1000; i++)    {        for(int j=0; j<1000; j++)            beatNeighbour[i][j]=0;    }}void turnoffon(string mode[], string mode1[], string mode2[], int counter){    //getting turn + off or on    char temp[8];    for(int i=0; i<counter; i++)    {        if(mode1[i]=="turn")        {           /* for(int j=0; j<8; j++)// cleaning temp                temp[j]=' ';            temp[0]='t';            temp[1]='u';            temp[2]='r';            temp[3]='n';            temp[4]=' ';            string tmp=mode2[i];            for(int j=0; j<tmp.length();j++)// adding on or off                temp[5+j]=tmp[j];            */            mode[i]=mode2[i];        }        else if(mode1[i]=="toggle")            mode[i]=mode1[i];    }}

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/504575-cannot-find-my-mistake-c/
Share on other sites

Link to post
Share on other sites

Solved: i deleted my allocated memory before even using it.

Next mistake is here: i get the same mistake code:

for(int i=0; i<counter; i++)    {        int x=startX[i], y=startY[i];            for(int j=startX[i]; j<endX[i]; j++)// from startX until endX            {                for(int k=startY[i]; k<endY[i]; k++)// from startY to endY                {                    if(mode[i]=="turn on")                    beatNeighbour[x][y]=1;                    else if (mode[i]=="turn off")                        beatNeighbour[x][y]=0;                    else if(mode[i]=="toggle")                    {                        if(beatNeighbour[x][y]==0)                           beatNeighbour[x][y]=1;                        else if( beatNeighbour [x][y]==1)                            beatNeighbour[x][y]=0;                    }                    y++;                }                x++;            }    }

i5-4690k, R9 380 4gb, 8gb-1600MHz ram, corsair vs 550w, astrock h97m anniversary.

 

Link to comment
https://linustechtips.com/topic/504575-cannot-find-my-mistake-c/#findComment-6735874
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×