storing a char array into a 2D char array

Hi, I'm having trouble running my program for storing words into a 2D dictionary array and I ran in to the problem that the error says: invalid conversion from char* to char and I'm not sure how to fix it:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
void readFile(char dict[0][0], char theWord[], int &numOfWords){


ifstream inStream;             // declare an input stream
string fileText = "dictionary.txt";

inStream.open(fileText.c_str() ); // Open a file for reading
if( !inStream.is_open()) { // if file is not open
   cout << "Could not find dictionary.txt.  Exiting..." << endl;
   exit( -1);
}



while( inStream >> theWord) { // inputs the word into file
   // Do something with the word that was read, such as store it, print it, etc.

    for(unsigned int i = 0; i < strlen(theWord); i++){ // have all characters become lower case
        theWord[i] = tolower(theWord[i]);
    }


    if(strlen(theWord) >= 3){
        for(unsigned int i = 0; i < dict_size; i++){
            for(unsigned int j = 0; j < strlen(theWord); j++){
                 dict[i][j] = theWord; // store words and length into dictionary array FIXME: error with storing words
                 numOfWords++;
            }


        }
        
}


// close the file
inStream.close();
}// end readFile() 
Last edited on
its better to think of it as a 1-d array of strings.

if(strlen(theWord) >= 3)
strcpy(dict[nextword++], theWord); //put the word into the next position

you can also use transform() to do the tolower instead of your own loop if you wanted to.

is there a reason for the C-style strings? A 1-d array vector of c++ strings would be easier to deal with.
Last edited on
Hello Fredo25,


PLEASE ALWAYS USE CODE TAGS (the <> formatting button), to the right of this box, when posting code.

Along with the proper indenting it makes it easier to read your code and also easier to respond to your post.

http://www.cplusplus.com/articles/jEywvCM9/
http://www.cplusplus.com/articles/z13hAqkS/

Hint: You can edit your post, highlight your code and press the <> formatting button.

You can use the preview button at the bottom to see how it looks.

I found the second link to be the most help.



Just so I, and others, can put efforts in the right direction is this a C or C++ program? It does make a difference.

When you input from "dictionary.txt" what does it look like. Do not need the whole file just a good sample will work. Say the first ten to fifteen words, (lines), will do.

When you write dict[i][j] = theWord; it looks like you are trying to copy a std::string into the array. For C you would need to use "strcpy()" like jonnin has shown you.

Also if you include enough of the program that will compile other error messages may show up, but at least it is something that can be tested.

Andy
Thank you for your help guys and I've used this which stores the words correctly into the array:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void readFile(char dict[0][0], char theWord[], int &numOfWords){


ifstream inStream;             // declare an input stream
string fileText = "dictionary.txt";

inStream.open(fileText.c_str() ); // Open a file for reading
if( !inStream.is_open()) { // if file is not open
   cout << "Could not find dictionary.txt.  Exiting..." << endl;
   exit( -1);
}



while( inStream >> theWord) { // inputs the word into file
   // Do something with the word that was read, such as store it, print it, etc.

    for(unsigned int i = 0; i < strlen(theWord); i++){ // have all characters become lower case
        theWord[i] = tolower(theWord[i]);
    }


    if(strlen(theWord) >= 3){
         int nextWord;
        strcpy(dict[nextWord++], theWord);
        numOfWords++;
        
     }
}



// close the file
inStream.close();
}// end readFile()






I encountered another error, for my function which looks up user Input word in the dictionary and if it is then to print out the dictionary. For this program the use of vectors isn't allowed because the main focus is on the use of char arrays as acting for strings :
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
void lookUpWord( char dict[0][0], char theWord[], int &numOfWords, char inputWord){

readFile(dict, theWord, numOfWords); // storing words into dictionary

// after reading file, want to look for input word in dictionary & search for dictionary word

    for(int i = 0; i < dict_size; i++){
        if(inputWord == dict[i] ){
            cout << inputWord << " is in the dictionary." << endl;
        }
    }
}// end lookUpWord()




Hello Fredo25,

I encountered another error, for my function which looks up user Input word in the dictionary
That is nice. Post the full error message. Do not make people guess at what happened.

Looking at your function
void lookUpWord( char dict[0][0], char theWord[], int &numOfWords, char inputWord) The first thing I notice is that you are defining a (0) zero length array. This is not allowed. Each dimension must have a size of (1) or greater. You can omit the size of the first dimension, but the second dimension must match the size of the array where it was first defined.

The first thing you do is call the "readFile" function. This should have been done before you call the "lookUpWord" function. The "lookUpWord" function should do only one thing and that is look up the word entered.

"inputWord" is defined as a single character, but if I understand correctly the if statement in the for loop is trying to compare a single character to a string. In that case "strcmp()" is the function that you need to be using.

With the lack of code that can be compiled and tester an with out the input file that you are using I can only guess at what you have done to come up with a way to test the program. And I would most likely be wrong compared to what you have done.

Andy
Topic archived. No new replies allowed.