Hello,
First I would like to say thank you for looking and any help will be greatly appreciated.
I am trying to write a code which does the following:
1) Read a file (The file contains list of words)
2) The purpose of the program to ask the user to enter any number of letters; these letters will then be used to find all words that contain them in the input file. First output the words with most letters in them and going all the way down to words that only contain 2.
For example:
Input file has the following:
apple
orange
watermelon
grape
bed
rock
television
stereo
sofa
couch
sex
food
fake
large
The user is asked to enter how many letter will he like to enter (In this example we will use 4)
Letter 1 x
Letter 2 a
Letter 3 s
Letter 4 e
The output should be (All words that contain at least 2 of those letters)
sex
apple
orange
watermelon
grape
fake
large
This is the code I have:
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
int main()
{
string getWord; //Will be used to store the word read from the file
ifstream engDicLis("engDic1.txt");
if(engDicLis.is_open())
cout << "File opened sucessfully!\n\n";
else
cout << "Unable to open file!\n\n";
cout << "How many letters would you like to enter: ";
int numLet, numLetCnt = 0; //numLet = how many letter user enters
cin >> numLet;
cout << endl;
char *charSear = new char[numLet]; //used to keep all letter entered by user
while(numLetCnt < numLet)
{
cout << "Enter the " << numLetCnt+1 << " letter: ";
cin >> charSear[numLetCnt];
numLetCnt++;
}
cout << endl;
cout << "You have entered: " << endl;
for(int i = 0; i < numLet; i++)
cout << charSear[i] << endl;
cout << endl;
int z;
while(engDicLis.good())
{
getline(engDicLis, getWord);
for(int j = 0; j < getWord.length(); j++)
{
for(z = 0; z < numLet; z++)
{
if(getWord[j] == charSear[z])
cout << getWord << endl;
}
}
}
engDicLis.close();
return 0;
}//end-of-main()
Here is a sample output from my run:
-----------------------------------------------------------------------------------------
File opened sucessfully!
How many letters would you like to enter: 4
Enter the 1 letter: s
Enter the 2 letter: e
Enter the 3 letter: k
Enter the 4 letter: a
You have entered:
s
e
k
a
apple
apple
orange
orange
watermelon
watermelon
watermelon
grape
grape
bed
rock
television
television
television
stereo
stereo
stereo
sofa
sofa
sex
sex
fake
fake
fake
large
large
bits
-----------------------------------------------------------------------------------------
Problem 1, I am getting repeated entries
Problem 2, fake should be at the top because it includes 3 of the entered letters.
I know my code if flawed but the problem that I can't seem to figure out is after I get a word from the file how do I compare each letter of that word with letter entered by the user. If the letter is in the word then I need to remember it and check for the next letter, and so on. After that is done I check which word has most letters list that first and then list words that contain less and less matches. If further clarification is needed please do let me know rathr than just ignoring the question.
Thank you in advance for your help.