Unscrambled words are in the wrong order

Unscrambled words are in the wrong order

Page 1 of 1

6 Replies - 6496 Views - Last Post: 22 January 2008 - 02:45 PM Rate Topic: -----

#1 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

Unscrambled words are in the wrong order

Post icon  Posted 19 January 2008 - 06:28 PM

I used the unscrambler code on this site and added some stuff to make it what I have now. However, after it unscrambles the words and writes them to unscrambled.txt, they are in the wrong order. Can anyone please tell me why this is happening?

Here is all of the code used:

#include <cstdlib> // Includes
#include <iostream> // Includes
#include <fstream> // Includes
#include <string> // Includes
#include <vector> // Includes
#include <algorithm> // Includes
using namespace std; // Using the standard namespace

string sRead; // Declare variables
ifstream scrambled; // Declare variables
ofstream unscrambled; // Declare variable
ifstream wordslist; // Declare variable
ofstream newwords;
string wordA; // Declare variables
string sLine1; // Declare variables
string sW; // Declare variables
string sC; // Declare variables


void CountWords()
{
				  
}



int main() // Start the main() function
{ // Start the main function

cout << "Please type 'sort' to organize the words in wordlist.txt by name and delete;\nduplicates, 'scramb' to unscramble the words in scrambled.txt while using\nthe words in wordlist.txt, 'count' to count the words in wordlist.txt, or \n'quit' to exit.\n" << endl; // Output instructions

	cin >> sW;

			  if (sW == "sort") // If sorting
				 { // Start the sorting code
				 cout << endl;
				 string temp;
				 string temp1;
				 string temp2;
				 string temp3;
				 string temp4;
				 vector<string> wordlist;
				 wordslist.open("wordlist.txt");
				 newwords.open("newwordlist.txt");
				 while (!wordslist.eof())
					{
					 wordslist >> temp;
					 wordlist.push_back(temp);						 
					}   
				 cout << "** Commencing Word Sort!! **" << endl;
				 for (int p=0;p<wordlist.size();p++)
					 {
					 temp1 = wordlist[p];
					 cout << "Now sorting word " << p + 1 << " of " << wordlist.size() << endl;
						   for (int j=0;j<wordlist.size();j++) 
							   {
							   temp2 = wordlist[j];
									 if (temp1==temp2 && p!=j)
										{
										wordlist[j]="";		  
										}	 
							   }			  
					 }
				  
				  for (int p=0;p<wordlist.size();p++) 
					   {
					   if (wordlist[p]!="")
						  {
						  temp3 = wordlist[p];
						  newwords << temp3 << endl;
						  }	
					   }
				  cout << "** Finished Sorting Words!! **" << endl;
				  wordslist.close();
				  newwords.close();
				  wordlist.clear();
				  
				 
				 } else if (sW == "scramb") // End if sorting code and start if unscrambling code
						   { // Start the unscramble code
							  cout << "** Starting to Unscramble the Words!! **" << endl;
							  fstream wordslist;
							  cout << endl;
							  vector<string> wordlist;
							  vector<string> scrambledwords;
							  vector<string> sortedwordlist;
							  string temp;
							  scrambled.open("scrambled.txt");
							  while (!scrambled.eof())
									{
									scrambled >> temp;
									scrambledwords.push_back(temp);
									}
							  scrambled.close();
							  wordslist.open("wordlist.txt");
							  unscrambled.open("unscrambled.txt");
							  wordlist.resize(0);//wordlist resize spot
							  while (!wordslist.eof())
									{
									wordslist >> temp;	 
									wordlist.push_back(temp);			
									}
							  temp4 = sortedwordlist.size();			
							  sortedwordlist.resize(0);//sorted word list resize spot
							  for (int p=0;p<wordlist.size();p++)
								  {
								  sortedwordlist.push_back(wordlist[p]);		  
								  }
							  for (int p=0;p<sortedwordlist.size();p++)
								  {
								  sort(sortedwordlist[p].begin(), sortedwordlist[p].end());		  
								  }
							  for (int p=0;p<scrambledwords.size();p++)
								 {
								 sort(scrambledwords[p].begin(), scrambledwords[p].end());
								 }
							  for (int p=0;p<sortedwordlist.size();p++)	   
								  {   
								  for (int j=0;j<scrambledwords.size();j++)
									  {
									  if (scrambledwords[j] == sortedwordlist[p])
										 {
										 cout << wordlist[p] << endl;
										 unscrambled << wordlist[p] << ",";			  
										 }		  
									  }		  
								  }
						   cout << "** Finished Unscrambling the Words!! **" << endl;								
						   wordslist.close();
						   unscrambled.close();  
						   } else if (sW == "quit") // End the uncramble code and start quit the program code
									 { // Start quit the program code
									 return EXIT_SUCCESS; // Command to return from the main() function and exit
									 } else if (sW == "count") // End quit the program code and start if count code
											   {
												cout << "** Beginning to Count the Words!! **" << endl;
												string temp;
												vector<string> wordlist;
												wordslist.open("wordlist.txt");
												newwords.open("newwordlist.txt");
												 while (!wordslist.eof())
												   {
												   wordslist >> temp;
												   wordlist.push_back(temp);						 
												   }
											   wordslist.close();	
											   cout << "** Finished Counting the Words!! **" << endl;	
											   cout << "There are " << wordlist.size() << " words in wordlist.txt." << endl;	
											   } else {cout << "** No valid answer!! **" << endl;} // End the count code and start if no valid answer code











system("PAUSE");
return 0;
} // End the main() function

This post has been edited by devilsson2010: 20 January 2008 - 03:32 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Unscrambled words are in the wrong order

#2 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

Re: Unscrambled words are in the wrong order

Posted 20 January 2008 - 03:34 PM

Anybody? I've posted for help on here and the hackthissite.org forums but no one wants to help me :(, anyone know what's wrong with my code?
Was This Post Helpful? 0
  • +
  • -

#3 VernonDozier  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 06-January 08

Re: Unscrambled words are in the wrong order

Posted 21 January 2008 - 07:41 PM

I'll take a look at it and get back to you within a couple of hours.
Was This Post Helpful? 0
  • +
  • -

#4 VernonDozier  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 06-January 08

Re: Unscrambled words are in the wrong order

Posted 21 January 2008 - 08:33 PM

A few things: One, I took out this line int the scramble section to make it compile:

temp4 = sortedwordlist.size();



You've defined "temp4" as a string above, but you are assigning it as an integer here, plus the earlier declaration of "temp4" has gone out of scope, plus you never seem to use "temp4" anywhere later.

I'm not sure what the "sort" section is supposed to do, but it did not sort the words I put into "wordlist.txt". I did notice that you were using the "==" operator when comparing strings, which can get dangerous and have bad results. Instead, consider using the "compare" function from the "string" library when you want to compare strings.

This line in the "scramble" section seemed odd:

fstream wordslist;



You've already declared "wordslist" as type "ifstream" at the top, and you seem to be using it as an "ifstream". I've never declared an input file as an "fstream" and I was a little surprised that I didn't get an error. Maybe they are equivalent or I am missing something, but I would use it as an "ifstream", and since you already declared it up top, I'd get rid of that line.


I am a little confused by the scrambling and unscrambling part of all this. Are you scrambling and unscrambling each individual word so that the word changes or are you keeping the words the same and scrambling and unscrambling the list? It looks like you are trying to take a word and put the letters in that word in abc order. You seem to also be doing the same thing here twice with two different vectors of strings:

	  for (int p=0;p<sortedwordlist.size();p++)
	  {
		  sort(sortedwordlist[p].begin(), sortedwordlist[p].end());		  
	  }
	  for (int p=0;p<scrambledwords.size();p++)
	  {
		  sort(scrambledwords[p].begin(), scrambledwords[p].end());
	  }



If you could explain what all of the different files are supposed to represent, what should be in those files both before and after, and what you are trying to do, that would help because I'm a little confused. It looks like you are treating each word as a vector of characters and trying to use an iterator to sort them, but I see no iterator declared anywhere. I could be way off here. For sure, though, don't use the "==" operator to compare your strings.
Was This Post Helpful? 0
  • +
  • -

#5 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

Re: Unscrambled words are in the wrong order

Posted 22 January 2008 - 12:32 PM

View PostVernonDozier, on 21 Jan, 2008 - 08:33 PM, said:

temp4 = sortedwordlist.size();


Ok I took that out, didn't see that.


View PostVernonDozier, on 21 Jan, 2008 - 08:33 PM, said:

I'm not sure what the "sort" section is supposed to do, but it did not sort the words I put into "wordlist.txt". I did notice that you were using the "==" operator when comparing strings, which can get dangerous and have bad results. Instead, consider using the "compare" function from the "string" library when you want to compare strings.

Ok, I changed the string comparisons to str1.compare(str2) == 0.


View PostVernonDozier, on 21 Jan, 2008 - 08:33 PM, said:

This line in the "scramble" section seemed odd:

fstream wordslist;


Didn't see that either, something like that should bring up a compiler error, but maybe as you said, there is some other thing we don't know about.


View PostVernonDozier, on 21 Jan, 2008 - 08:33 PM, said:

I am a little confused by the scrambling and unscrambling part of all this. Are you scrambling and unscrambling each individual word so that the word changes or are you keeping the words the same and scrambling and unscrambling the list? It looks like you are trying to take a word and put the letters in that word in abc order. You seem to also be doing the same thing here twice with two different vectors of strings:

	  for (int p=0;p<sortedwordlist.size();p++)
	  {
		  sort(sortedwordlist[p].begin(), sortedwordlist[p].end());		  
	  }
	  for (int p=0;p<scrambledwords.size();p++)
	  {
		  sort(scrambledwords[p].begin(), scrambledwords[p].end());
	  }



If you could explain what all of the different files are supposed to represent, what should be in those files both before and after, and what you are trying to do, that would help because I'm a little confused. It looks like you are treating each word as a vector of characters and trying to use an iterator to sort them, but I see no iterator declared anywhere. I could be way off here. For sure, though, don't use the "==" operator to compare your strings.


What that code above does is take each individual words in the vectors sortedwordlist and scrambledwords and alphabetizes the letters in it. Then it compares them to see if it is equal to anything in the wordlist. Example: I have a scrambled word 'pyaph', and I have another word 'happy'. What the sort does is arrange it by letter, so both pyaph and happy get arranged to: ahppy. That means that both are equal and that 'pyaph' was just the scrambled word 'happy'. Hope I explained it ok :blink:

Ok, this is what I changed the code to, it comes out in the right order now but there is always duplicate of the last word. Any ideas?


{ // Start the unscramble code
string str1;
string str2;
cout << "** Commencing Word Unscramble **" << endl;
vector<string> wordlist;
vector<string> scrambledwords;
vector<string> sortedwordlist;
string temp;
scrambled.open("scrambled.txt");
cout << "** Gathering Scrambled Words **" << endl;
while (!scrambled.eof())
	  {
	  scrambled >> temp;
	  scrambledwords.push_back(temp);
	  }
scrambled.close();
wordslist.open("wordlist.txt");
unscrambled.open("unscrambled.txt");
wordlist.resize(0);//wordlist resize spot
cout << "** Gathering Wordlist **" << endl;
while (!wordslist.eof())
	  {
	  wordslist >> temp;	 
	  wordlist.push_back(temp);
	  }
sortedwordlist.resize(0);//sorted word list resize spot
for (unsigned int i=0;i<wordlist.size();i++)
	{
	sortedwordlist.push_back(wordlist[i]);
	}
for (unsigned int i=0;i<sortedwordlist.size();i++)
	{
	sort(sortedwordlist[i].begin(), sortedwordlist[i].end());		  
	}
for (unsigned int i=0;i<scrambledwords.size();i++)
   {
   sort(scrambledwords[i].begin(), scrambledwords[i].end());
   }
cout << "** Checking Scrambled Words Against Wordlist **" << endl;   
for (unsigned int i=0;i<scrambledwords.size();i++)	   
	{
	str1 = scrambledwords[i];
	for (unsigned int j=0;j<sortedwordlist.size();j++)
		{
		str2 = sortedwordlist[j];
		if (str1.compare(str2) == 0)
		   {
		   cout << wordlist[j] << endl;
		   unscrambled << wordlist[j] << ",";
		   }		  
		}		  
	}
cout << "** Finished Unscrambling Words **\n" << endl;
wordslist.close();
unscrambled.close();
break;

This post has been edited by devilsson2010: 22 January 2008 - 01:37 PM

Was This Post Helpful? 0
  • +
  • -

#6 VernonDozier  Icon User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 56
  • Joined: 06-January 08

Re: Unscrambled words are in the wrong order

Posted 22 January 2008 - 01:47 PM

I think you are reading in the words incorrectly from the input files "scrambled.txt" and "wordlist.txt" with these while loops:

while (!scrambled.eof())
{
	  scrambled >> temp;
	  scrambledwords.push_back(temp);
}



and

while (!wordslist.eof())
{
	  wordslist >> temp;	
	  wordlist.push_back(temp);
}



These while loops are being traversed one too many times. So if you have an input file "wordlist.txt" like this:


house
dog
mouse
frog


For the above list of four words, you end up with the following wordlist vector:

house
dog
mouse
frog
frog

Same thing with the scrambled word file/vector.

If you change the loop control to this:

while (wordslist >> temp)
{
	  wordlist.push_back(temp);
}



You won't end up with the duplicate "frog" at the end of the vector. Ditto with the "scrambled.txt" file.

Please note the edit in the last post in the last piece of code. It was wrong the first time I submitted. It is now correct.

This post has been edited by VernonDozier: 22 January 2008 - 01:45 PM

Was This Post Helpful? 0
  • +
  • -

#7 devilsson2010  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 46
  • Joined: 02-January 08

Re: Unscrambled words are in the wrong order

Posted 22 January 2008 - 02:45 PM

Works perfectly. Many thanks VernonDozier :)
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1