Welcome to Dream.In.Code
Become a C++ Expert!

Join 149,911 C++ Programmers for FREE! Get instant access to thousands of C++ experts, tutorials, code snippets, and more! There are 2,203 people online right now. Registration is fast and FREE... Join Now!




Unscrambled words are in the wrong order

 
Reply to this topicStart new topic

Unscrambled words are in the wrong order, Unscrambled words are in the wrong order

devilsson2010
19 Jan, 2008 - 05:28 PM
Post #1

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
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:

CODE
#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 Jan, 2008 - 02:32 PM
User is offlineProfile CardPM
+Quote Post

devilsson2010
RE: Unscrambled Words Are In The Wrong Order
20 Jan, 2008 - 02:34 PM
Post #2

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
Anybody? I've posted for help on here and the hackthissite.org forums but no one wants to help me sad.gif, anyone know what's wrong with my code?
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Unscrambled Words Are In The Wrong Order
21 Jan, 2008 - 06:41 PM
Post #3

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

I'll take a look at it and get back to you within a couple of hours.
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Unscrambled Words Are In The Wrong Order
21 Jan, 2008 - 07:33 PM
Post #4

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

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

CODE

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:

CODE

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:

CODE

      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.




User is offlineProfile CardPM
+Quote Post

devilsson2010
RE: Unscrambled Words Are In The Wrong Order
22 Jan, 2008 - 11:32 AM
Post #5

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
QUOTE(VernonDozier @ 21 Jan, 2008 - 08:33 PM) *

CODE

temp4 = sortedwordlist.size();


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


QUOTE(VernonDozier @ 21 Jan, 2008 - 08:33 PM) *

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.


QUOTE(VernonDozier @ 21 Jan, 2008 - 08:33 PM) *

This line in the "scramble" section seemed odd:

CODE

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.


QUOTE(VernonDozier @ 21 Jan, 2008 - 08:33 PM) *

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:

CODE

      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.gif

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?


CODE
{ // 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 Jan, 2008 - 12:37 PM
User is offlineProfile CardPM
+Quote Post

VernonDozier
RE: Unscrambled Words Are In The Wrong Order
22 Jan, 2008 - 12:47 PM
Post #6

New D.I.C Head
*

Joined: 6 Jan, 2008
Posts: 46

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

CODE

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


and

CODE

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:

CODE

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 Jan, 2008 - 12:45 PM
User is offlineProfile CardPM
+Quote Post

devilsson2010
RE: Unscrambled Words Are In The Wrong Order
22 Jan, 2008 - 01:45 PM
Post #7

New D.I.C Head
*

Joined: 2 Jan, 2008
Posts: 46


My Contributions
Works perfectly. Many thanks VernonDozier smile.gif
User is offlineProfile CardPM
+Quote Post

Reply to this topicStart new topic
Time is now: 1/8/09 01:52PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month