Hello All,

I am working on writing a code to implement dictionary in C++. I have few functions to implement for my dictionary. Can someone please help me writing the definitions for my dictionary.

Header file and cpp file is attached here.

Dict.h file is as below:

enum WordType{All, Animal, Fruit, Name};

struct Word{
    WordType type;
    char word[20];
};

struct Dict{
    int size;
    int capacity;
    Word **wordArray;
};

Dict *createDictionary(int capacity);
bool addWord(Dict *dic, const char *word, WordType type);
void deleteDictionary(Dict *dic);
void printDictionary(Dict *dic, WordType type=All);

Dict.cpp file is as below in which I need to add definitions:

#include "dict.h"
#include <cstring>
#include <iostream>
using namespace std;

Dict *createDictionary(int capacity){
    //To do, initialize a dictionary with the specified capacity

}

bool addWord(Dict *dic, const char *word, WordType type){
    //To do, add a Word to the dictionary. If the dictionary is already full, can’t add word and return false. Otherwise, add the word to the dictionary, increase dictionary size by 1, and return true.

}

void deleteDictionary(Dict *dic){
    //To do, delete all words in the dictionary and then the dictionary
}

void printDictionary(Dict *dic, WordType type){
    //To do, for default type all (specified in the header file), print words of all types. Otherwise print words of the specified types
}

Please help me asap. Thanks in advance.

What is giving you the problem? We are not here to do your homework for you.

I am not able to understand how to start. Can you please tell me what my each definition should contain. I mean, how the definition should be.

The comments in your code tell you what you should do in each function. Try to code it out and if you prolems with the code then post it and we should be able to help.

Programming == Problem Solving

Please help me writing definition of function createDictionary function. I am not able to start it. Rest I can work with..Thanks

I tried writing the code below for createDictionary but its giving me error:

Dict *createDictionary(int capacity){
    //To do, initialize a dictionary with the specified capacity
    Dict *dic = new Dict;
    dic->size = 0;
    (*dic->wordArray)->type = All;
    strcpy((*dic->wordArray)->word,0);
    dic->capacity=capacity;
    return dic;
}

Error is below:

First-chance exception at 0x012b1a96 in Dictionary.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.
Unhandled exception at 0x012b1a96 in Dictionary.exe: 0xC0000005: Access violation reading location 0xcdcdcdcd.

Your problem is in the way your trying to do a 2D array. This article should help.

I wrote this code for my project but still getting access violation error:

#include "dict.h"
#include <cstring>
#include <iostream>
using namespace std;

Dict *createDictionary(int capacity){
    //To do, initialize a dictionary with the specified capacity
    Dict *d = new Dict;
    d->capacity = capacity;
    d->size = 0;

    //how to initialize WordArray?


    for(int i=0; i<d->size; i++)
    {
        d->wordArray[i] = NULL;
    }
    //cout<<"Returning"<<endl;
    return d;
}

bool addWord(Dict *dic, const char *word, WordType type){
    //To do, add a Word to the dictionary. If the dictionary is already full, cannot add word and return false. Otherwise, add the word to the dictionary, increase dictionary size by 1, and return true.
        //cout<<"Enter func add"<<endl;
        //cout<<"copy word:"<<endl;
    if(type!=0)
    {
            strcpy(dic->wordArray[dic->size]->word, word);
            //cout<<"copy type: "<<endl;
            dic->wordArray[dic->size]->type = type;
            //cout<<dic->wordArray[dic->size]->word<<endl;
            (dic->size)++;
            return 1;
    }

    else
        return 0;



}

void deleteDictionary(Dict *dic){
    //To do, delete all words in the dictionary and then the dictionary
    for(int i=0; i<dic->size; i++)
    {
        dic->wordArray[i] = NULL;
    }
    delete [] dic->wordArray;
    delete dic;
    dic = NULL;
}


void printDictionary(Dict *dic, WordType type){
    //To do, for default type all (specified in the header file), print words of all types. Otherwise print words of the specified types
    if (type!=0)
    {
        cout<<"Entered"<<endl;
        for(int i=0; i<dic->size; i++)
        {
            if(dic->wordArray[i]->type == type)
                cout<<dic->wordArray[i]->word<<endl;
        }
    }

    else
    {
        for(int j=0; j<dic->size; j++)
        {
            cout<<dic->wordArray[j]->word<<endl;
        }
    }
}

Can somebody please help me improving my code to remove the error? I am not able to figure it out
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.