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.

Dani AI

Generated

Quick diagnosis: the crash comes from dereferencing an uninitialized pointer. In MSVC debug builds the pattern 0xCDCDCDCD indicates uninitialized heap memory — your wordArray was never allocated or initialized, then code like indexing or dereferencing dic->wordArray[...] touches garbage. That explains the access violation seen by .

What to do (step‑by‑step)

  • In createDictionary allocate the array of pointers and initialize every slot (use NULL/nullptr). Do not loop to size (size starts at 0) — you must loop to capacity.
  • In addWord check dic and word for NULL, check size < capacity, allocate a Word for the new slot, copy the string safely (limit to 19 chars for char word[20]), set type, then store the pointer and increment size. If allocation fails or capacity is reached, avoid changing size and free any temporary allocation.
  • In deleteDictionary delete each allocated Word (only those you actually created), then delete[] the wordArray, then delete the Dict.
  • In printDictionary test if (type == All) to print everything — All is the first enum value (0), so type != 0 is the wrong test.

Minimal safe patterns (illustrative)

Dict *createDictionary(int capacity) { /* allocate wordArray = new Word*[capacity]; init slots to NULL; set size=0 */ }
bool addWord(Dict *dic, const char *word, WordType type) { /* check capacity, new Word, strncpy(...), set type, dic->wordArray[dic->size++]=w */ }

Notes and tips

  • Use strncpy (and null-terminate) or, better, change word to std::string to avoid fixed-buffer issues.
  • Consider std::vector and smart pointers to remove manual new/delete entirely.
  • was right to point at the 2D/allocation issue; is also right — implement, test, then post the failing code and error output if anything remains.

Recommended Answers

All 7 Replies

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.