-
array errors
Basically this program just reads in a seating chart... Problem is I keep getting the error below... Im not sure what it means... My code seems correct?.. any help?
getting error
Code:
main.cxx:8: error: variable-size type declared outside of any function
main.cxx:8: error: variable-size type declared outside of any function
main.cxx:10: error: variable-size type declared outside of any function
main.cxx:11: error: variable-size type declared outside of any function
main.cxx:33: error: variable-size type declared outside of any function
main.cxx:40: error: variable-size type declared outside of any function
Main.cxx
Code:
#include <iostream>
#include <fstream>
using namespace std;
int SIZE = 50;
int num_mat,occupied, row, col;
char m1[SIZE][SIZE];
void init_array(char array[][SIZE]);
void display_array(char array[][SIZE]);
int main() {
init_array(m1);
cin >> num_mat;
cin >> occupied;
while( !cin.eof() ) {
cin >> row >> col;
if(col=='\n') {
occupied = row;
display_array(m1);
init_array(m1);
} else
m1[row][col] = 'X';
}
return 0;
}
void init_array(char array[][SIZE]) {
for(int i=0;i<SIZE;i++) {
for(int j=0;j<SIZE;j++)
array[i][j] = 'E';
}
}
void display_array(char array[][SIZE]) {
for(int i=0;i<SIZE;i++) {
for(int j=0;j<SIZE;j++)
cout << array[i][j] << endl;
}
}
Thanks for any assistance guys.
-
Hello,
Array declarations use constant expressions for subscripts. For example:
Code:
D1[constant-expression]
If the constant-expression is present, it must have integral type, and value greater than 0.
A constant expression is an expression that involves only constants. Such expressions may be evaluated during compilation rather than run-time, and accordingly may be used in any place that a constant can occur. For example:
Code:
#define SIZE 50
char line[SIZE+1];
An array's subscript must always be a constant expression. In your example, size is defined during run-time rather than a constant.
- Stack Overflow
-
You need to make your array dimensions constant. Right now it is
So just change that to:
Code:
const int SIZE = 50;
and it should compile fine.
-
const int SIZE did it..
thank you gentlemen :-P.