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
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:
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.
#include <iostream>
#include <iomanip>
//#include <limits>
#include <string>
//#include <cctype> // <--- For "std::tolower() and std::toupper()" + others.
#include <fstream>
#include <sstream>
usingnamespace std; // <--- Best not to use.
constint NUMCHARS = 26;
//constexpr int NUMCHARS{ 26 };
constint 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)
{
returntrue;
}
elseif (words[half] < word)
{
minimum = half + 1;
}
else
{
maximum = half - 1;
}
}
returnfalse;
}
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.