Solved

dictionary file words.txt

Posted on 2008-06-15
9
1,902 Views
Last Modified: 2011-10-19
Consider the dictionary file words.txt, which contains one word per line. Write a program of your language choice to read a list of characters separated by spaces (through the command line) and print out all words containing every input character.

For examples, if the input characters are "a b c d e f"

The output may be:

There are 3 words containing all supplied characters:
   1: barefaced
   2: boldface
   3: feedback


My program complies and when I run it...it hangs up.  Do yall have any idea why?
#include <iostream>

#include <fstream>

#include <string>

#include <vector>
 

using namespace std;
 

int main ()

{

	char letters[26];

	int matches,wordcount,lenOfLetters;

	vector<string> myWords;

	bool next;
 

	cout << "Please enter in 6 letters >> ";

	cin.getline (letters,26);

	

	cout << "The following letters is what you typed in: " << letters << "." << endl;
 

	string line;

	ifstream dictionary ("words.txt");
 

	if (dictionary.is_open())

	{

	    while (!dictionary.eof() )

		{

			getline (dictionary,line);

			lenOfLetters=strlen(letters);

		

			for (int i=0,matches=0;i<lenOfLetters && next==false;i++)
 

           {

              string::size_type loc = line.find( letters[i],0 );

              if( loc == string::npos )

               {

                next=true;

               }

               else

               {

                   matches++;

               }

		  }

		  if (lenOfLetters==matches)

		  {

            wordcount++;

            myWords.push_back(line);           

          }

        

        }                        

	

	

cout << "There are " << wordcount << " words containing all supplied characters:" << endl;

for (int i=0;i<myWords.size();i++)

{

        cout << i<<myWords[i] << endl;

}

	

	dictionary.close();

	}

	

	else cout << "Unable to open file" << endl; 

	//system ("PAUSE");

	return 0;

}

Open in new window

words.txt
0
Comment
Question by:texanweb
  • 3
  • 3
  • 2
  • +1
9 Comments
 
LVL 84

Expert Comment

by:ozo
ID: 21790655
perl -ne 'BEGIN{$r=qr/@{[map"(?=.*$_)",@ARGV]}/x;@ARGV="words.txt"}print if/$r/' a b c d e f
0
 

Author Comment

by:texanweb
ID: 21790666
What does this mean????
perl -ne 'BEGIN{$r=qr/@{[map"(?=.*$_)",@ARGV]}/x;@ARGV="words.txt"}print if/$r/' a b c d e f
0
 
LVL 84

Expert Comment

by:ozo
ID: 21790680
it is a command to run a program of my language choice to read a list of characters separated by spaces (through the command line) and print out all words containing every input character.
0
 

Author Comment

by:texanweb
ID: 21790925
I still don't see how this has to do with C++ programming.  I believe what your saying is perl language and I have absolute 0 knowledge on perl .... but I do have some knowledge on C++.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 53

Accepted Solution

by:
Infinity08 earned 500 total points
ID: 21790930
1) You should initialize the int and bool variables :

        int matches = 0, wordcount = 0, lenOfLetters = 0;
        bool next = false;


2) You should reset the matches and next values at the start of each iteration (ie. for each word).


3) The letters string you read in still contains spaces ... shouldn't you remove those ?


4) Consider using :

            while (getline(dictionary, line))
                {

    instead of :

            while (!dictionary.eof() )
                {
                        getline (dictionary,line);
0
 
LVL 84

Expert Comment

by:ozo
ID: 21791059
you said "of your language choice" but if you want to know what's wrong with your Code snippet,
for (int i=0,matches=0;i<lenOfLetters && next==false;i++)
declares matches local to the loop, and next is uninitialized
                        next=false;
                        int i;
                        for (i=0,matches=0;i<lenOfLetters && next==false;i++)
0
 
LVL 17

Expert Comment

by:rstaveley
ID: 21791763
http:#21790655 is very classy, ozo!
0
 

Author Comment

by:texanweb
ID: 21796486
Ok, I have done some code changing.  The program works, but its not outputting all of the lines.  For example, when I type in "abcdef"... its only coming up with 2 words, not 3.

Any ideas?
#include <iostream>

#include <fstream>

#include <string>

#include <vector>
 

using namespace std;
 

int main ()

{

	char letters[26];

	int matches = 0, wordcount = 0, lenOfLetters = 0;

	vector<string> myWords;

	bool next = false;
 

	

	cout << "Please enter in 6 letters >> ";

	cin.getline (letters,26);

	cout << endl;

	cout << "The following letters is what you typed in: " << letters << "." << endl;

	

	string line;

	ifstream dictionary ("words.txt");
 

	if (dictionary.is_open())

	{

	    while (getline(dictionary, line))

		{

			getline (dictionary,line);

			lenOfLetters=strlen(letters);

			matches=0;

			for (int i=0 ; i < lenOfLetters ; i++)

			{

				string::size_type loc = line.find( letters[i], 0 );

				next = false;

				

				if(!next)

				{

					if( loc == string::npos )

					{

						next=true;

					}

					else

					{

						matches++;

					}

				}

			}

			if (lenOfLetters==matches)

			{

				wordcount++;

				myWords.push_back(line);

			}

		}

		cout << "There are " << wordcount << " words containing all supplied characters:" << endl;

		for (int i=0;i<myWords.size();i++)

		{

			cout << i+1 << ". " << myWords[i] << endl;

		}

		dictionary.close();

	}

	else cout << "Unable to open file" << endl;

	system ("PAUSE");

	return 0;

}

Open in new window

0
 
LVL 53

Assisted Solution

by:Infinity08
Infinity08 earned 500 total points
ID: 21796594
>> The program works, but its not outputting all of the lines.

Note that you call getline twice in each iteration ... The second getline should not be there :

            while (getline(dictionary, line))
                {
                        getline (dictionary,line);


Isn't this :

                                next = false;
                               
                                if(!next)
                                {

a bit predictable ? ;) Setting it to false first, and then checking whether it's false ... Are you sure the next = false; should be there ? Somewhere else ?
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
FizzBuzz challenge 9 73
Python Encoding Problem \u2013 4 86
word0 challenge 3 58
Problem to start Neon 20 53
Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Windows Script Host (WSH) has been part of Windows since Windows NT4. Windows Script Host provides architecture for building dynamic scripts that consist of a core object model, scripting hosts, and scripting engines. The key components of Window…
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now