This forum is in archive mode. You will not be able to post new content.

Author Topic: array bound is not an integer constant  (Read 3379 times)

0 Members and 2 Guests are viewing this topic.

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
array bound is not an integer constant
« on: October 29, 2012, 06:10:46 PM »
ive got a issue with a  problem im working on

its basically a minesweeper game where you first must read the mine locations from a file and print the uncovered results into another file.

this is an example input file
Code: [Select]
44
*...
....
.*..
....
35
**...
.....
.*...
00      <------sentinel value


Here the source.
Code: (cpp) [Select]

#include <iostream>
#include <fstream>
#include <conio.h>


using namespace std;


int n;
int m;


int getField(char mines[][m] ,char mineBoard[][m],int i , int d);
void getMines(char mines[][m] ,char mineBoard[][m], int i , int d);
void calMines(char mines[][m] ,char mineBoard[][m], int i , int d);
void calFunc(char mines[][m] ,char mineBoard[][m], int i , int d);
void corners(char mines[][m] ,char mineBoard[][m], int i , int d);
void sides(char mines[][m] ,char mineBoard[][m], int i , int d);
void grid(char mines[][m] ,char mineBoard[][m], int i , int d);
void outField(char mineBoard[][m], int count, int i ,int d);






int main()
{
    int i = 0;
    int d = 0;
    char mineBoard[n][m];
    char mines[n][m];


    for(int count = 1; n != 0 && m != 0; count++)




        {


            getField(mines,mineBoard, i , d);


            calFunc(mines,mineBoard, i , d);


            outField(mines,mineBoard, i , d)
        }








    getch();
    return 0;
}




int getField(char mines[][m] ,char mineBoard[][m],int i , int d)
{
        ifstream inFile;
        inFile.open("inputdata.dat");
        inFile >> n;
        inFile >> m;




        getMines(mines,mineBoard, i , d);
        inFile.close();
       
}


void getMines(char mines[][m] ,char mineBoard[][m], int i , int d)
{
        for(i = 0; i < n; i++)
    {
        for(d = 0; d < m; d++)
            {
                  inFile >> mineBoard[i][d];
                  if(mineBoard[i][d] == '*')
                    {
                        mineBoard[i][d] = mines[i][d];


                    }
                   else if(mineBoard[i][d] == '.')
                    {
                        mineBoard[i][d] = 0;
                    }
            }


    }


}




void calMines(char mines[][m] ,char mineBoard[][m], int i, int d)
{
   
            if(mines[i][d] == '*')
                {
                    if(mineBoard[i][d] = 0)
                        {
                            mineBoard[i][d] = 1;


                        }
                    else if(mineBoard[i][d] > 0 )
                        {
                            mineBoard[i][d]++;


                        }
                    else
                        {
                            continue;
                        }
                }
       


}


void calFunc(char mines[][m] ,char mineBoard[][m], int i , int d)
{
    for(i, d ; i < n && d < m; i++, d++)
    {
        if(i = 1  && d = 1 || i = 1 && d = m || i = n && d = 1 ||i = n && d = m)
             {
                corners(mines,mineBoard, i , d);
             }
        else if (i = 1 || d = 1 || i = n || d = m)
             {
                sides(mines,mineBoard, i , d);
             }
        else
             {
                 grid(mines,mineBoard, i , d);
             }


    }
     
}


void corners(char mines[][m] ,char mineBoard[][m], int i , int d)
{
        calMines(mines,mineBoard, i+1 , d);
        calMines(mines,mineBoard, i+1 , d+1);
        calMines(mines,mineBoard, i , d+1);
}




void sides(char mines[][m] ,char mineBoard[][m], int i , int d)
{
        calMines(mines,mineBoard, i , d-1);
        calMines(mines,mineBoard, i+1 , d-1);
        calMines(mines,mineBoard, i-1 , d);
        calMines(mines,mineBoard, i+1 , d+1);
        calMines(mines,mineBoard, i , d+1);
}


void grid(char mines[][m] ,char mineBoard[][m], int i , int d)
{
         calMines(mines,mineBoard, i+1 , d);
         calMines(mines,mineBoard, i-1 , d);
         calMines(mines,mineBoard, i , d+1);
         calMines(mines,mineBoard, i , d-1);
         calMines(mines,mineBoard, i+1 , d+1);
         calMines(mines,mineBoard, i-1 , d+1);
         calMines(mines,mineBoard, i+1 , d-1);
}


void outField(char mineBoard[][m], int count, int i ,int d)
{
         ofstream outFile;
         outFile.open("outputdata.dat");


         outFile << "Field #" << count;
         for (i = 0; i < n; i++)
                {
                    for(d = 0; d < m; d++ )
                    {
                        outFile << mineBoard[i][d];
                    }
                 outFile << endl;
                }
         outFile.close();
}

the main problem i have is that it keeps giving the error "array bound is not an integer constant."and other errors in the prototypes

what does that mean?

also if there is any other logical error in it please let me know.
« Last Edit: November 01, 2012, 07:05:07 AM by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: array bound is not an integer constant
« Reply #1 on: October 29, 2012, 08:31:37 PM »
It means that you should not be declaring your prototypes as receiving a variable-sized array.  The compilation process will need to know what size that is in order to appropriately set things up to transfer information during the call.  Use pointers instead.  You should almost never declare argument arrays as 'var[][]'.  Use 'char **mineBoard' or 'char *mineBoard[]' instead.  If you don't already understand at least the basics of pointers then take the time to learn about it now.
-Xires

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: array bound is not an integer constant
« Reply #2 on: October 29, 2012, 08:53:10 PM »
It means that you should not be declaring your prototypes as receiving a variable-sized array.  The compilation process will need to know what size that is in order to appropriately set things up to transfer information during the call.  Use pointers instead.  You should almost never declare argument arrays as 'var[][]'.  Use 'char **mineBoard' or 'char *mineBoard[]' instead.  If you don't already understand at least the basics of pointers then take the time to learn about it now.


i do i tried before i posted but i got this mess of an error afterwards :o

Code: [Select]
Line 40.   error: cannot convert 'char (*)[(((unsigned int)(((int)m) + -0x000000001)) + 1)]' to 'char**' for argument '1' to 'void outField(char**, int, int, int)'|


EDIT:
heres a revised version
Code: (cpp) [Select]
#include <iostream>
#include <fstream>
#include <conio.h>

using namespace std;



void getField(int *n, int *m,int count, int x , int y);
void getMines(char *mines[] ,char *mineBoard[],ifstream &inFile, int n, int m,int x , int y);
void calMines(char *mines[] ,char *mineBoard[],int x , int y);
void calFunc(char *mines[] ,char *mineBoard[],int n, int m,int x , int y);
void corners(char *mines[] ,char *mineBoard[],int x , int y, int n, int m);
void sides(char *mines[] ,char *mineBoard[],int x , int y);
void grid(char *mines[] ,char *mineBoard[],int x , int y);
void outField(char *mines[] ,char *mineBoard[],int n, int m,int x , int y);



int main()
{
    int n;
    int m;
    int x = 0;
    int y = 0;


    for(int count = 1; n != 0 && m != 0; count++)


        {

            getField( &n , &m ,count, x , y);



        }




    getch();
    return 0;
}


void getField(int n, int m,int count, int x , int y)
{
        ifstream inFile;
        inFile.open("inputdata.dat");
        inFile >> n;
        inFile >> m;

        char mineBoard[n][m];
        char mines[n][m];

        getMines(mines,mineBoard,inFile, n , m, x , y);
        inFile.close();

        calFunc(mines, mineBoard, n , m , x , y);
        outField(mines,mineBoard,count, x , y);
}

void getMines(char *mines[] ,char *mineBoard[],ifstream &inFile, int n, int m,int x , int y)
{
        for(x = 0; x < n; x++)
            {
                for(y = 0; y < m; y++)
                    {
                          inFile >> mineBoard[x][y];
                          if(mineBoard[x][y] == '*')
                            {
                                mineBoard[x][y] = mines[x][y];

                            }
                           else if(mineBoard[x][y] == '.')
                            {
                                mines[x][y] = 0;
                                mineBoard[x][y] = 0;
                            }
                    }

            }

}



void calFunc(char *mines[] ,char *mineBoard[],int n, int m,int x , int y)
{
    for(x ; x < n && y < m; x++ )
    {
        for(y; y > m ;y++)
        if((x == 1  && y == 1) || (x == 1 && y == m) || (x == n && y == 1) ||(x == n && y == m))

                corners(mines,mineBoard, x , y, n, m);

        else if (x == 1 || y == 1 || x == n || y == m)

                sides(mines,mineBoard, x , y);

        else

                 grid(mines,mineBoard, x , y);


    }

}

void calMines(char *mines[] ,char *mineBoard[],int x , int y)
{

            if(mines[x][y] == '*')
                {
                    if(mineBoard[x][y] = 0)
                        {
                            mineBoard[x][y] = 1;

                        }
                    else if(mineBoard[x][y] > 0 )
                        {
                            mineBoard[x][y]++;

                        }
                }
}

void corners(char *mines[] ,char *mineBoard[],int x , int y, int n, int m)
{
        if(x == 1  && y == 1)
            {
                calMines(mines,mineBoard, x+1 , y);
                calMines(mines,mineBoard, x+1 , y+1);
                calMines(mines,mineBoard, x , y+1);
            }

        else if((x == 1 && y == m) || (x == n && y == 1))
            {
                calMines(mines,mineBoard, x-1 , y);
                calMines(mines,mineBoard, x+1 , y-1);
                calMines(mines,mineBoard, x , y+1);
            }

        else if(x == n && y == m)
            {
                calMines(mines,mineBoard, x-1 , y);
                calMines(mines,mineBoard, x-1 , y-1);
                calMines(mines,mineBoard, x , y-1);

            }


}


void sides(char *mines[] ,char *mineBoard[],int x , int y)
{
        calMines(mines,mineBoard, x , y-1);
        calMines(mines,mineBoard, x+1 , y-1);
        calMines(mines,mineBoard, x-1 , y);
        calMines(mines,mineBoard, x+1 , y+1);
        calMines(mines,mineBoard, x , y+1);
}

void grid(char *mines[] ,char *mineBoard[],int x , int y)
{
         calMines(mines,mineBoard, x+1 , y);
         calMines(mines,mineBoard, x-1 , y);
         calMines(mines,mineBoard, x , y+1);
         calMines(mines,mineBoard, x , y-1);
         calMines(mines,mineBoard, x+1 , y+1);
         calMines(mines,mineBoard, x-1 , y+1);
         calMines(mines,mineBoard, x+1 , y-1);
}

void outField(char *mineBoard[], int count,int n ,int m,int x , int y)
{
         ofstream outFile;
         outFile.open("outputdata.dat");

         outFile << "Field #" << count;
         for (x = 0; x < n; x++)
                {
                    for(y = 0; y < m; y++ )
                    {
                        outFile << mineBoard[x][y];
                    }
                 outFile << endl;
                }
         outFile.close();
}
« Last Edit: October 30, 2012, 05:05:45 PM by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: array bound is not an integer constant
« Reply #3 on: October 31, 2012, 08:05:02 PM »
You're using n & m as global variables not actually assigning a proper value to them before they're used.  You're allocating arrays of unknown size.  Go back to the drawing board, this program needs to be cleaned up and you should check when and how you use each of your variables.


Several problems are evident in just main():


Code: (cpp) [Select]

int main() {
    int i = 0;
    int d = 0;
    char mineBoard[n][m];
    char mines[n][m];


    for(int count = 1; n != 0 && m != 0; count++) {
            getField(mines,mineBoard, i , d);
            calFunc(mines,mineBoard, i , d);
            outField(mines,mineBoard, i , d)
    }


    getch();
    return 0;
}


The variables 'n' & 'm' have been given no value.  The 'n' & 'm' variables are also global; they shouldn't be.  The 'count' variable is not used for the conditional, effectively making the loop infinite.  If 'n' & 'm' had been initialized to 0 then the loop would not even execute.  The 'i' & 'd' variables are never incremented.  I doubt this is intentional.


I don't mean to be insulting but it seems that this program is far too advanced for your current skills.  I would strongly suggest that you go back to the basics.  Lots of people think that, just because they're smarter than everyone else in their class or whatever, they can skip learning basics and immediately handle the harder stuff. Thoughts like these just breed idiocy.  Start from the basics.

« Last Edit: October 31, 2012, 08:23:42 PM by Xires »
-Xires

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: array bound is not an integer constant
« Reply #4 on: October 31, 2012, 11:55:55 PM »

I don't mean to be insulting but it seems that this program is far too advanced for your current skills.  I would strongly suggest that you go back to the basics.  Lots of people think that, just because they're smarter than everyone else in their class or whatever, they can skip learning basics and immediately handle the harder stuff. Thoughts like these just breed idiocy.  Start from the basics.


-____-


dude this was just a dry run, unfinished code, i would never even consider this to be good. i knew it had plenty of other problems in it.  i just asked about a certain problem i was having so i could know what to do later on if i came across this and i did feel a bit insulted even with the disclamer. this was homework for my class.this isnt a choice i had to do this
« Last Edit: November 01, 2012, 12:08:31 AM by Super_mario666 »
The Bigger they are...The more likely you'll get your ass kicked

Offline Xires

  • Noob Eater
  • Administrator
  • Knight
  • *
  • Posts: 379
  • Cookies: 149
    • View Profile
    • Feed The Trolls - Xires
Re: array bound is not an integer constant
« Reply #5 on: November 01, 2012, 02:43:34 AM »
I do apologize.  As I said, I didn't mean to be insulting.  I do feel it best to mention, however, that when I looked at the code I got the distinct impression that there was little to no planning involved.  It's very common for people to hate the 'by the book' methods of writing out plans and creating flowcharts & what-not.  Most people just want to dive right into the code.  But the planning process is absolutely essential.  In fact, it is more important than the actual code itself.  During the planning process you should be able to catch things like required variable initialization, loop conditions, etc.

Good luck.
-Xires

Offline Daemon

  • VIP
  • Baron
  • *
  • Posts: 845
  • Cookies: 153
  • A wise man fears a gentle mans anger
    • View Profile
Re: array bound is not an integer constant
« Reply #6 on: November 01, 2012, 02:57:45 AM »
I do apologize.  As I said, I didn't mean to be insulting.  I do feel it best to mention, however, that when I looked at the code I got the distinct impression that there was little to no planning involved.  It's very common for people to hate the 'by the book' methods of writing out plans and creating flowcharts & what-not.  Most people just want to dive right into the code.  But the planning process is absolutely essential.  In fact, it is more important than the actual code itself.  During the planning process you should be able to catch things like required variable initialization, loop conditions, etc.

Good luck.

TBH with all programmers and programmers-in-training out there, the process of writing down the steps to your program is the #1 most important thing I took away from my C++ class last semester. It has translated to EVERYTHING i do with programming now, and even though I'm in an HLA class I still start every program with a pen and piece of paper on which i write down the logical order of my program and what each step is going to have. Then after that flowchart is up to my standards, then I'll start to build the program step-by-step.

It may seem like a pain in the ass, but after you get used to it and in the habit you'll know when you don't need to use it and when you should. Sometimes even simple sounding programs have to be broken down into a flowchart before your mind can get them working, and if I ever find myself stuck on how a portion of a program is working then i pull out another piece of paper and pen and try to figure it out haha.

TL;DR
Flowcharts are boss

Good luck OP and all you other programmers out there
This lifestyle is strictly DIY or GTFO - lucid

Because sexploits are for h0edays - noncetonic


Xires burns the souls of HF skids as a power supply

Offline Super_mario666

  • Knight
  • **
  • Posts: 160
  • Cookies: 7
  • Professional Badass
    • View Profile
Re: array bound is not an integer constant
« Reply #7 on: November 01, 2012, 07:16:20 AM »
I do apologize.  As I said, I didn't mean to be insulting.  I do feel it best to mention, however, that when I looked at the code I got the distinct impression that there was little to no planning involved.  It's very common for people to hate the 'by the book' methods of writing out plans and creating flowcharts & what-not.  Most people just want to dive right into the code.  But the planning process is absolutely essential.  In fact, it is more important than the actual code itself.  During the planning process you should be able to catch things like required variable initialization, loop conditions, etc.

Good luck.


yea it alright. as far planning goes i do plan ahead and write some of the general logic on paper
but i dont really get in to making pseudocode and flowcharts. i guess as my programs get larger i might end up having to do that. either way i solved the problem and the code works fine although it may be a bit messy.


Thanks for the tip ;)


Code: (cpp) [Select]

#include <iostream>
#include <fstream>
#include <cstdlib>


using namespace std;




void getField(char (*mineBoard)[100], int n, int m, int x , int y);
void getMines(char (*mineBoard)[100],ifstream &inFile, int n, int m,int x , int y);
void calMines(char (*mineBoard)[100],int i, int d,int x , int y);
void calFunc(char (*mineBoard)[100],int n, int m,int x , int y);
void corners(char (*mineBoard)[100],int x , int y, int n, int m);
void sides(char (*mineBoard)[100],int x , int y, int n, int m);
void grid(char (*mineBoard)[100],int x , int y);
void outField(char (*mineBoard)[100],ofstream &outFile, int count,int n ,int m,int x , int y);






int main()
{
    int n = 0;
    int m = 0;
    int x = 0;
    int y = 0;
    char mineBoard[100][100];
    getField(mineBoard, n , m , x , y); // open and read contents from file to mineboard


    return 0;
}




void getField(char (*mineBoard)[100],int n, int m, int x , int y) //in 'main'
{
   
ifstream inFile; //opening file
        inFile.open("inputdata.dat");
if (!inFile)
{
cerr << "File will not open. " << endl;
exit(1);
}
ofstream outFile;
        outFile.open("outputdata.dat");
int c[100]; // array for max x and y axis
for(int a = 0, count = 1; a < 100; a++, count++)
{

inFile >> c[a];

inFile >> c[a+1];

if( c[a] == 0 && c[a+1] == 0)
break;
else
n = c[a];

m = c[a+1];


getMines(mineBoard,inFile, n , m, x , y);
calFunc(mineBoard, n , m , x , y);//determines number of mines next to surrounding squares
outField(mineBoard,outFile,count,n, m, x , y);  //outputs results to file
}
inFile.close();
outFile.close();
}


void getMines(char (*mineBoard)[100],ifstream &inFile, int n, int m,int x , int y)// in 'getField'
{

        for(x = 0; x < n; x++)
            {
                for(y = 0; y < m; y++)
                    {
                          inFile >> mineBoard[x][y]; 
  if(mineBoard[x][y] == '.')
                            {
                                mineBoard[x][y] = '0';
                            }
 
                    }

            }




}






void calFunc(char (*mineBoard)[100],int n, int m,int x , int y) // in 'main'
{
   
for(x = 0; x < n; x++ )
    {
        for(y = 0; y < m ;y++)

{


 
            if((x == 0  && y == 0) || (x == 0 && y == m-1) || (x == n-1 && y == 0) ||(x == n-1 && y == m-1))
 
    corners(mineBoard, x , y, n-1, m-1);
 
else if (x == 0 || y == 0 || x == n-1 || y == m-1)
 
sides(mineBoard, x , y, n-1, m-1);
 
else
 
grid(mineBoard, x , y);
}
 
    }
 
 
    }








void calMines(char (*mineBoard)[100],int i, int d,int x , int y)// in 'corners', 'sides' and 'grid'
{

            if(mineBoard[i][d] == '*')
                {
switch(mineBoard[x][y])
{
case '0':
                            mineBoard[x][y] = '1';
break;


case '1':
mineBoard[x][y] = '2';
break;


case '2':
mineBoard[x][y] = '3';
break;
case '3':
mineBoard[x][y] = '4';
break;
case '4':
mineBoard[x][y] = '5';
break;


case '5':
mineBoard[x][y] = '6';
break;


case '6':
mineBoard[x][y] = '7';
break;


case '7':
mineBoard[x][y] = '8';
break;

}
                }


}


void corners(char (*mineBoard)[100],int x , int y, int n, int m) // in 'calFunc'
{
int i = x;
int d = y;

if(x == 0  && y == 0)
            {
                calMines(mineBoard,i, d, x+1 , y);
                calMines(mineBoard,i, d, x+1 , y+1);
                calMines(mineBoard,i, d,x , y+1);
            }


        else if((x == 0 && y == m) || (x == n && y == 0))
            {
                calMines(mineBoard,i, d, x-1 , y);
                calMines(mineBoard,i, d, x+1 , y-1);
                calMines(mineBoard,i, d,x , y+1);
            }


        else if(x == n && y == m)
            {
                calMines(mineBoard,i, d, x-1 , y);
                calMines(mineBoard,i, d, x-1 , y-1);
                calMines(mineBoard,i, d,x , y-1);


            }




}




void sides(char (*mineBoard)[100],int x , int y, int n, int m)// in 'calFunc'
{
int i = x;
int d = y;

if(x == 0)
{
calMines(mineBoard,i ,d, x , y-1);
calMines(mineBoard,i ,d, x+1 , y-1);
calMines(mineBoard,i ,d, x+1 , y);
calMines(mineBoard,i ,d, x+1 , y+1);
calMines(mineBoard,i ,d, x , y+1);
}
else if(y == 0)
{
calMines(mineBoard,i ,d, x-1 , y);
calMines(mineBoard,i ,d, x-1 , y+1);
calMines(mineBoard,i ,d, x , y+1);
calMines(mineBoard,i ,d, x+1 , y+1);
calMines(mineBoard,i ,d, x+1 , y);
}
else if(x == n)
{
calMines(mineBoard,i ,d, x , y-1);
calMines(mineBoard,i ,d, x-1 , y-1);
calMines(mineBoard,i ,d, x-1 , 1);
calMines(mineBoard,i ,d, x-1 , y+1);
calMines(mineBoard,i ,d, x , y+1);
}
else if(y==m)
{
calMines(mineBoard,i ,d, x-1 , y);
calMines(mineBoard,i ,d, x-1 , y-1);
calMines(mineBoard,i ,d, x , y-1);
calMines(mineBoard,i ,d, x+1 , y-1);
calMines(mineBoard,i ,d, x+1 , y);
}






}


void grid(char (*mineBoard)[100],int x , int y)// in 'calFunc'
{
int i = x;
int d = y;
       calMines(mineBoard,i ,d, x-1 , y-1);
               calMines(mineBoard,i ,d, x-1 , y);
               calMines(mineBoard,i ,d, x-1 , y+1);
               calMines(mineBoard,i ,d, x , y-1);
                calMines(mineBoard,i ,d, x , y+1);
                calMines(mineBoard,i ,d, x+1 , y-1);
                 calMines(mineBoard,i ,d, x+1 , y);
        calMines(mineBoard,i ,d, x+1 , y+1);
}


void outField(char (*mineBoard)[100],ofstream &outFile,int count ,int n ,int m,int x , int y)
{


         outFile << "Field #" << count <<":" << endl;

         for (x = 0; x < n; x++)
                {
                    for(y = 0; y < m; y++ )
                    {
                         
outFile << mineBoard[x][y];



                    }
                 outFile << endl;


                }
         outFile << endl;
}

The Bigger they are...The more likely you'll get your ass kicked

 



Want to be here? Contact Ande, Factionwars or Kulverstukas on the forum or at IRC.