Attempt to output "autocorrect" words (using .txt files)

Trying to finalize two function's codes I think my void Dictionary is fine, but void Alternate isn't working properly (it outputs nothing (None) at the end of each test case

my code: https://pastebin.com/syvBf913

test cases: https://pastebin.com/FEvpLiBR

.txt typo: https://pastebin.com/nwP4sGKZ

.txt typo2: https://pastebin.com/W6EGB8qG

.txt words is big so I had to use google drive: https://drive.google.com/file/d/1DdnfKYGY5-vtDaU1bCWfUUGdqeG835Xa/view?usp=sharing

The problem is line 24/28: You use half as an index to compare, but since half does not change neither minimum nor maximum will change.
This is not the way binary search works.

Consider using standard library functions like lower_bound:

http://www.cplusplus.com/reference/algorithm/lower_bound/
Hello juicypwner,

In addition to what coder777 has mentioned these are some of the problems I see in the code. The comments should explain most of it, but if there is anything that you do not understand let me know.

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
117
#include <iostream>
#include <iomanip>
//#include <limits>
#include <string>
//#include <cctype>  // <--- For "std::tolower() and std::toupper()" + others.

#include <fstream>
#include <sstream>

using namespace std;  // <--- Best not to use.

const int NUMCHARS = 26;
//constexpr int NUMCHARS{ 26 };
const int MAXCHOICES = 10;
//constexpr int MAXCHOICES{ 10 };

// <--- Avoid using global variables like these. They should be defined in "main" and passed to the function(s)
// or the function(s) that would use them, depends on what you need.
char typos[NUMCHARS][MAXCHOICES];
int choices[NUMCHARS];
string *words;
int numWords = 0;


//return true or false - whether word can be found in words[] array
bool dictionaryWord(string word)
{
    //CODE HERE
    int minimum = 0, maximum = numWords - 1, half = (minimum + maximum) / 2;

    while (minimum <= maximum)
    {
        if (words[half] == word)
        {
            return true;
        }
        else if (words[half] < word)
        {
            minimum = half + 1;
        }
        else
        {
            maximum = half - 1;
        }
    }

    return false;
}

int alternateWords = 0;

//consider all the alternate characters for current index, recurse for all positions
void countAlternates(int index, string word)
{
    //CODE HERE!
    int charIndex = word[index] - 'a';

    for (int i = 1; i <= index; i++)
    {
        word[index] = typos[charIndex][i];

        if (index == word.length())
            cout << word;
    }
}

int main()
{
    ifstream dictionaryFile("words.txt");
    // <--- How do you know it is open and usable?

    string line;

    //count all dictionary words
    while (dictionaryFile >> line)
        numWords++;

    //read all dictionary words
    words = new string[numWords];  // <--- You have created a dynamic array on the heap, but where do you delete it? It must be deleted.

    dictionaryFile.clear();

    dictionaryFile.seekg(0, ios::beg);

    for (int i = 0; i < numWords; i++)
        dictionaryFile >> words[i];

    //typo possibilities for each key
    ifstream choicesFile("typo.txt");
    // <--- How do you know it is open and usable?

    for (int i = 0; i < NUMCHARS; i++)
    {
        getline(choicesFile, line);

        istringstream choiceStr(line);

        int numChoices = 0;

        while (choiceStr >> typos[i][numChoices])
            numChoices++;

        choices[i] = numChoices;
    }

    //we need to find all possible alternate words for the input word
    string searchWord;

    cin >> searchWord;

    countAlternates(0, searchWord);

    if (!alternateWords)
        cout << "None";

    return 0;  // <--- Not required, but makes a good break point.
}

The #includes are what I have found to be common to most programs. These days I do not write these first lines I just paste them in. The second group is any header files specific to the program. I find the layout helpful, but you do not have to do this.

If your compiler is using the C++11 standards or later I find that "constexpr" is a better choice, but "const" is still acceptable.

I am not sure about the "countAlternates" function because I have not set up all the files to be able to test the program.

When opening a file stream to read a file it is reall a good idea to check that it did open and is usable. Back when I was first learning to work with files I came up with this:
1
2
3
4
5
6
7
8
9
10
11
const std::string inFileName{ "" };  // <--- Put File name here.

std::ifstream inFile(inFileName);

if (!inFile)
{
    std::cout << "\n File " << std::quoted(inFileName) << " did not open." << std::endl;
    //std::cout << "\n File \"" << inFileName << "\" did not open." << std::endl;

    return 1;
}

This may seem a bit much and it can be shortened in the future, but it does help in the beginning.

Andy

Edit: typo.
Last edited on
Topic archived. No new replies allowed.