Dictionary with pointers

1

I want to write a program that adds words to a lexicon with pointers. However, I must not use strdup(), how can I do that? Below is what I tried but it gives me this error:

Exception thrown at 0x7C87EE72 (ucrtbased.dll) in (name of the file.exe): 0xC0000005: Access violation writing location 0xCDCDCDCD.

The problem is in the newStr() function.

#include <cstring>
#include <string>
#pragma warning (disable:4996)
#include <iostream>
using namespace std;

void delStr(char**&, int&, char*);
void newStr(char**&, int&, char*);
void printchar(char**, int, char);
char* searchStr(char**&, int&, char*);
void printAll(char**&, int);

enum ACTIONS { NEW, DELETE, SEARCH, PRINTLETTER, PRINTALL, EXIT };

int main() {
    char** lexicon = NULL;//pointer to pointer fo the dictionary
    int x = 0;
    int size = 0;
    char word[81];
    char ch;
    cout << "Enter 0-5:" << endl;
    cin >> x;
    while (x < 0 || x > 5) {//while loop for correct input
        cout << "ERROR" << endl;
        cin >> x;
    }
    while (x >= 0 && x <= 5) {
        switch (x) {
        case NEW:
            cout << "Enter the word:" << endl;
            ch = cin.get();
            cin.getline(word, 80);
            newStr(lexicon, size, word);
            printAll(lexicon, size);
            break;
        case DELETE:
            cout << "Enter the word to delete:" << endl;
            ch = cin.get();
            cin.getline(word, 80);
            delStr(lexicon, size, word);
            printAll(lexicon, size);
            cout << endl;
            break;
        case SEARCH:
            cout << "Enter the word to search for:" << endl;
            ch = cin.get();
            cin.getline(word, 80);
            searchStr(lexicon, size, word);
            break;
        case PRINTLETTER:
            cout << "Enter the char:" << endl;
            ch = cin.get();
            ch = cin.get();
            printchar(lexicon, size, ch);
            cout << endl;
            break;
        case PRINTALL:
            if (size > 0 && lexicon != NULL) {
                printAll(lexicon, size);
            }
            break;
        case EXIT:
            return 0;
            break;
        }
        cout << "Enter your choice:" << endl;
        cin >> x;
        while (x < 0 || x > 5) {//while loop for correct input
            cout << "ERROR" << endl;
            cin >> x;
        }
    }
    return 0;
}

void printAll(char**& lex, int size) {
    for (int i = 0; i < size; i++) {
        cout << lex[i] << " ";
    }
    cout << endl;
}

void newStr(char**& lex, int& size, char* word) {
    int i = 0;
    for (i = 0; i < size; i++) {
        if (strcmp(lex[i], word) == 0) {
            //cout << "Word " << word << " already exists\n";
            return;
        }
    }
    if (size == 0) {
        lex = new char* [1];
        for (int i = 0; i < strlen(word); i++) {
            strcpy(*lex, word);
        }
        size++;
    }
    else {
        char** temp = new char* [size + 1];
        for (i = 0; i < size; i++) {
            temp[i] = lex[i];
        }
        //strcpy(temp[size],word);
        strcpy(temp[size], word);
        delete[] lex;
        lex = temp;
        size++;
    }
}

void delStr(char**& lex, int& size, char* word) {
    int j = 0;
    for (int i = 0; i < size; i++) {
        if (strcmp(lex[i], word) == 0) {
            for (j = i; j < size - 1; j++) {
                lex[j] = lex[j + 1];
            }
            size--;
            char** temp = new char* [size];
            for (j = 0; j < size; j++) {
                temp[j] = lex[j];
            }
            delete[] lex;
            lex = temp;
        }
    }
}

void printchar(char** lex, int size, char ch) {
    for (int i = 0; i < size; i++) {
        if (lex[i][0] == ch) {
            cout << lex[i] << " ";
        }
    }
}

char* searchStr(char**& lex, int& size, char* word) {
    for (int i = 0; i < size; i++) {
        if (strcmp(lex[i], word) == 0) {
            cout << "Found" << endl;
            //cout << "Word " << word << " already exists\n";
            return lex[i];
        }
    }
    cout << "Not found";
    return NULL;
}
c++
arrays
pointers
asked on Stack Overflow Jan 10, 2021 by yoyo • edited Jan 10, 2021 by Remy Lebeau

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0