when i pass an array with a single dimension it goes thus:
1 2 3 4 5 6
void fun(char[]);
int main{
...
fun(a)
...}
void fun(char a[]){...}
and it works but when i apply the same concept with a 2 dimensional array i get an error is there a special way to do this i can't find any literature on it in my text books ><
that is what i thought but i get an error that the 2nd open bracket is not allowed... >< I'm reading up on it its saying that i need to include the second size identifier so like...char a[][c]where c is the global constant number of columns... I get an error both ways... :s
Ah, that's right, apologies. When passing a two-dimensional array like that you must specify an dimensions after the first. If you want, however, you could also just accept a char** a instead. You should pass the size of each dimension as additional parameters.
//creates a 10 by 10 word search puzzle
//words determined by a specified filed
//word locations then specified by user
//program creates random letters to fill in the puzzle
//programmer : Tyler Sievers
#include <iostream>
#include <fstream>
#include <string>
usingnamespace std;
constint k=5; // 5 is the most words allowed in file. constant for the first array
constint r=10; //10 rows high
constint c=10; //10 collomns wide
void getfile(ifstream&);
void populatearray(ifstream&, string[]);
void testfcn1(string[]); //comment when done!!!!
void displaygrid(constchar[][c]);
void colomncounter();
void horizontalborders();
void main(){
//more or less does nothing :]
ifstream infile;
string words[k];
char thegrid[r][c]; //tron reference intended... if only i were as good a programmer...
cout<<"welcome to the Word Finder Generator\n\n";
getfile(infile);
populatearray(infile, words);
//testfcn1(words); // COMMENT WHEN DONE!!!
system("CLS");
return;
}
void getfile(ifstream& words){
//gets the file name from the user, opens it, and returns to the main
string filename;
do{
cout<<"Please enter the name of the file with the word list: ";
cin >> filename;
words.open(filename.c_str());
if (words.fail())
cout << "I'm sorry, that file could not be found. Please try again.\n";
}while(words.fail());
return;
}
void displaygrid(constchar thegrid[][c]){
//manages all the sub modules that display and change the grid
colomncounter();
horizontalborders();
}
displaygrid is where i come into trouble... i have thegrid[r][c] declared in the main... i get an error saying thegrid is missing a subscript... i just don't understand what i am doing wrong
+ i don't think i should have to pass the function additional sizes because these are all global constants
kk never mind what i have up there is compiling now... don't know why i was getting errors... but here is another question... my book says i should declare these arrays as const. the reasoning is vague... will i be able to change this array declared in this fashion?
No, not if it is const. You should declare them const if you are not going to modify them, which a display function probably shouldn't need to do anyway.