Help needed with a dictionary in c++.

Hello guys, I'm a junior student of computer science. The professor of file processing want me to do a program that could edit, delete, view, insert, search and replace text in a text file, plus that he wants a dictionary. The dictionary needs to be a new text file with every single word in the main text file, without repetitions. I just don't know how to do it and I need your help, I'll appreciated any kind of suggestion. Sorry for my bad English and the words in Spanish that you are going to find, I traduced the program to English so you guys can understand it better. Thanks! I'll be in contact.
Last edited on
I traduced the program to English so you guys can understand it better.

Thanks for that. It really does make it easier to read. (traduced should be translated, by the way.)

In quite a few places you're using the construct:

while (getline(inFile, string1).good ())

which should be:

while (getline(inFile, string1))

It is possible that getline encounters the end of file while reading from the file, but is still successful at extracting a string from the file. good() will return false if eof has been encountered.

deleteText and replaceText don't work. If you choose to viewText after executing deleteText you will see that the file is unchanged. If you choose to viewText after executing replaceText you will see that the text has not been replaced, unless it happens to be at the very beginning of the file and the replacement text happens to be exactly the same length as the text to be replaced.

Typically when you modify a text file, you want to:

-open the file and read all of the contents in.
-close the file
-make any changes to the contents.
-reopen the file for output (truncating the file)
-write the contents back to the file.
-close the file.

As far as making the dictionary goes, use a container. Read the file word by word. If the word isn't already in the container, search the container for the word. If the word isn't in the container, put it in.

Thanks for the information, but I don't really know what a container is in c++. Its like an array ? and the problem that I'm going to find are the dots, commas, etc. If I want to do a dictionary I can't add these things to the "Container"
Thanks for the information, but I don't really know what a container is in c++. Its like an array ?

A container is an object which holds objects of a different type. An array is a very primitive container. std::vector, std::set, std::map, std::unordered_map, std::queue, std::deque, std::string... all containers.


the problem that I'm going to find are the dots, commas, etc. If I want to do a dictionary I can't add these things to the "Container"

So trim those things from the word before checking the container for it.
Ok guys I'm trying this code that I did, but isn't creating the "dictionary.txt".
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
void dictionary()
{
    ifstream inFile;
    ofstream outFile;
    inFile.open("info.txt", ifstream::in);

    if(inFile.fail())
    {
        cout << "The file was not successfully opened" << endl;
        exit(1);
    }
    vector<string> Dictionary;

    string tempWord;
    while(!inFile.eof()) // si no llega al final
    {
        inFile >> tempWord;
        if(Dictionary.size() == 0)// si el vector tiene 0 elementos
        Dictionary.insert(Dictionary.begin(), tempWord); // inserta palabra al principio del vector
        else
        {
            for(unsigned int i = 0; i < Dictionary.size(); i++)
            {
                Dictionary.insert(Dictionary.begin()+i, tempWord);
            }
        }
    }
    inFile.close();
    outFile.open("dictionary.txt", ofstream::out);

    if(outFile.fail())
        cout << "Cannot open the file" << endl;
    else
    {
        for(unsigned int i = 0; i < Dictionary.size(); i++)
        {
        outFile << Dictionary.at(i) << endl;
        }
        outFile.close();
    }

}
but isn't creating the "dictionary.txt"

What is happening? Are you getting the "Cannot open the file" message at line 32?
Is something else happening?

I don't understand your loop at line 22. It appears to me to be inserting tempWord once for every word already in the dictionary.

For a dictionary, std::set makes more sense. A set will automatically order the container for you and will ensure that there are no duplicate words.

Using a vector, you need to order the container yourself and also check yourself that there are no duplicate words.
AbstractionAnon I found that the loop in line 22 was useless and was creating a infinite loop so I decided to edit it. Now I fixed it but I have two more problems, I created the dictionary without problems but it contains symbols and repeated words. What do you guys suggest ?

NEW CODE
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
void dictionary()
{
    ifstream inFile;
    ofstream outFile;
    inFile.open("info.txt", ifstream::in);

    if(inFile.fail())
    {
        cout << "The file was not successfully opened" << endl;
        exit(1);
    }
    vector<string> Dictionary;

    string tempWord;
    while(!inFile.eof()) // si no llega al final
    {
        inFile >> tempWord;    
        Dictionary.push_back(tempWord); // inserta la palabra en el indice i
        
    }
    inFile.close();
    outFile.open("dictionary.txt", ofstream::out); // se crea archivo diccionario

    if(outFile.fail())
        cout << "Cannot open the file" << endl;
    else
    {
        for(unsigned int i = 0; i < Dictionary.size(); i++) // printea cada dato en el archivo como diccionario.
        {
        outFile << Dictionary.at(i) << endl;
        }
        outFile.close();
    }

}
it contains symbols and repeated words. What do you guys suggest ?

It's already been pointed out that you need to remove the symbols yourself before adding the words to the dictionary.

If you want to avoid repeated words, you have two choices:
1) Search the vector yourself for duplicates before adding a word.
2) Use a std::set. std::set will eliminate duplicates automatically.
I'm having many problems removing the dots, commas, exclamation and interrogation points. Can someone show me a short code or link me a website or video that explains how to do it ? Thanks =(
I'm having many problems removing the dots, commas, exclamation and interrogation points. Can someone show me a short code or link me a website or video that explains how to do it ? Thanks =(


The dots, commas, exclamation and question marks always occur at the ass end of a word right? So check the last letter of the word. If it's a punctuation mark, remove it.
Topic archived. No new replies allowed.