0

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.

2
Contributors
1
Reply
6
Views
6 Years
Discussion Span
Last Post by Ancient Dragon
0

1. You are getting repeated entries because you display the word every time if contains one of the letters you entered. Don't do that. Wait until the program has counted the number of letters in the word then, if the count >= 2, display the word and its count.

Problem 2: The only way I know of to resolve that problem is to keep a list of all the words that contains the letters and their associated letter count. The simplest way to do that is to use a vector of structures.

struct wordcnt
{
    string word;
    int count;
    wordcnt() {count = 0;}
};

vector<wordcnt> wordlist;

Now whenever a word is read from the file that contains two or more letters from the input list then add a new item to the vector. After the program is done reading the file you will need to sort the vector using std::sort declared in <algorithm> header file.

// This function is called by std::sort to sort the
// vector first by count and then by word.
bool compare(wordcnt& w1, wordcnt& w2)
{
    int x = w1.count - w2.count;
    if( x == 0 )
    {
        x = w1.word < w2.word;
    }
    return x > 0;
}

...
... <snip>
std::sort(wordlist.begin(), wordlist.end(), compare);

Finally, display the entire list

vector>wordcnt>::iterator it = wordlist.begin();
for( ; it != wordlist.end(); it++)
{
    cout << (*it).word << "\t" << (*it).count << '\n';
}

Edited by Ancient Dragon: n/a

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.