main function for a dictionary program

Hi.
I'm having trouble writing a mainfunction to a dicitionary program.

This is the code I have:

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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// The file dictionary.h



class Entry {

public:

  Entry() : o(0), t(0) {}

  ~Entry() {delete[] o; delete[] t;}

  void change(const char *word, const char *txt);

  const char *reference_word() {return o;}

  const char *text() {return t;}

private:

  char *o, *t;

};



class Dictionary {

public:

  Dictionary() : number(0) {}

  void add_entry(const char *word, const char *txt);

  const char *operator[] (const char *word);

private:

  int number;

  enum {max=100};

  Entry u[max];

};



// The file dictionary.cpp

#include "dictionary.h"

#include <cstring>

#include <iostream>

using namespace std;



void Entry::change(const char *word, const char *txt)

{

  delete[] o;

  delete[] t;

  o = new char[strlen(word)+1];

  strcpy(o,word);

  t = new char[strlen(txt)+1];

  strcpy(t,txt);

}



const char *Dictionary::operator[] (const char *word)

{

  for (int i=0; i<number; i++)

    if (strcmp(word, u[i].reference_word()) == 0)

      return u[i].text();

  return 0;

}



void Dictionary::add_entry(const char *word, const char *txt)

{

  if ((*this)[word] == 0) // a new word

    if (number<max)

      u[number++].change(word, txt);

    else

      cout << "The dictionary is full!" << endl;

  else

    cout << "The entry " << word << " already exists!" << endl;

}


If someone could point me in the right direction on how to add an entry I would appreciate that!

Thanks in advance!
All you need to do it write a main that will create a dictionary object, then pass the add_entry function two pointers (I'm not sure what the txt pointer is supposed to do), one of which is a pointer to the word you want added (a C string with the word in it would work for that). How you get the word into the program I'll leave for you, but for starters you can just load it straight into the string.
Topic archived. No new replies allowed.