Program crashes... cin.get() not... cin.getting

It's a simple dictionary, to work with pointer arrays a little. The program is supposed to tell the user when the word entered isnt in the database. Instead, the program crashes (dictionary.exe not responding).

When I enter words from the database(array) cin.get() doesn't do what it should... the definition appears and the program terminates withing 1/10 of second or so. Is it my code(copied directly from a textbook)? Or is it Dev-C++?

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
#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])
{
    char *dictionary[][2] = //a 2d array of pointer which points to paired strings
    {
         "pencil", "A writing instrument",
         "keyboard", "A musical instrument",
         "rifle", "A shoulder-fired firearm",
         "airplane", "A fixed-wing aircraft",
         "network", "An interconnected group",
    };
    
    char word[80];
    int i;
    
    cout << "Enter word: ";
    cin >> word;
    
    
    for(i = 0; *dictionary[i][0]; i++)
    {
      if(!strcmp(dictionary[i][0], word))
      {
        cout << dictionary[i][1] << "\n";
        break;
      }
    }
    
  if(!*dictionary[i][0])
    cout << word << " not found.\n";
   
    cin.get();
    return 0;
}


Plz thx! Sorry if this is mis-filed (should be in beginner's forum maybe?)

for(i = 0; *dictionary[i][0]; i++)

Maybe you should limit yourself to the known size of the dictionary, rather than accessing memory that you don't own.
I tried that by changing the line to
for(i = 0; *dictionary[i][10]; i++)

this caused the program to stop responding when i enter a word in the array. did i limit the size correctly?
did i limit the size correctly?


No. You didn't limit it at all.
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
#include <cstdlib>
#include <iostream>
#include <cstring>

using namespace std;

int main(int argc, char *argv[])
{
    char *dictionary[][2] = //a 2d array of pointer which points to paired strings
    {
         "pencil", "A writing instrument",
         "keyboard", "A musical instrument",
         "rifle", "A shoulder-fired firearm",
         "airplane", "A fixed-wing aircraft",
         "network", "An interconnected group",
    };
    
    const unsigned nRows = sizeof(dictionary) / sizeof(dictionary[0]) ;

    char word[80];
    
    cout << "Enter word: ";
    cin.getline(word, 80) ;
    
    unsigned i ;
    for(i = 0; i < nRows ; ++i)
    {
      if(!strcmp(dictionary[i][0], word))
      {
        cout << dictionary[i][1] << "\n";
        break;
      }
    }
    
    if ( i == nRows ) 
        cout << word << " not found.\n";   
}

Last edited on
ahh... so then limiting the size is a separate task altogether! I must not understand what the values in the brackets for the array are... please to can explain?

This did fix both errors though so, thank you for that! Why would the textbook give me such junky code?
Topic archived. No new replies allowed.