Make your own free website on Tripod.com
« December 2018 »
S M T W T F S
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31
You are not logged in. Log in
Entries by Topic
All topics  «
Blog Tools
Edit your Blog
Build a Blog
RSS Feed
View Profile
Scramble Program With Dictionary
Sunday, 2 March 2008
Scramble Program Part 2

#include <iostream>
#include <string>
#include <fstream>
#include <vector>
using namespace std;


class Scramble {
      private:
               vector<string> _word;
               bool Check (string T, string S);
      public:
              Scramble(vector<string> words){ 
              _word=words;
}
void Descramble (string Scrambled);
};
//******************************************
void Scramble::Descramble(string Scrambled)
{
      bool flag=false;
      for(int i=0;i<_word.size();i++)
      {
              if(_word[i].size()==Scrambled.size())
              {
                   if(Check(Scrambled,_word[i]))
                   {
                        cout << "The word is: " << _word[i]<< endl;
                        flag=true;}
              }
      }
      if (!flag)
      {
              cout<< "The word: " << Scrambled <<" is not found"<< endl;
              return;
      }
}
//******************************************
bool Scramble::Check(string T, string S)
{
      int n=T.size();
      int Found=0;
      string temp=S;
      for (int i=0; i<n; i++)
      {
              bool flag = true;
              for (int j=0; j < n && flag;j++)
              if(T[i]== temp[j])
              {
                   temp[j]=' ';
                   flag=false;Found ++;
              }
      }
      if (Found==n)
              return true;
      else
              return false;
}
//******************************************
int main(){
      cout << "Welcome to the Scramble Game!\n\n";
      while (true)
      {
              int play;
              cout << "\nWould you like to play? \nEnter 1 for Yes or 2 for No:\n";
              cin >> play;
              if (play==1)
              {
                   cout <<"\nPlease enter the scrambled word: ";
                   string Scrambled ;
                   cin >> Scrambled;
                   ifstream file("dictionary.txt");
                   if(!file)
                   {
                     cout <<"\nError! The file couldn't be opened\n";
                   }
                   vector<string> word_list;
                   string word;
                   while(file >> word)
                {
                        word_list.push_back(word);
                   }
                   Scramble test(word_list);
                   test.Descramble(Scrambled);
                   file.close();
                   cin.get(); cin.get();
              }
              else if (play==2)
              {
                   cout << "\n\nGoodbye!\n\n";
                   cin.get(); cin.get();
                   return 0;
              }
              else
              {
                   cout << "\nInvalid entry. Try again.\n\n";
                   cin.get(); cin.get();
              }
      }
}


Posted by veronicak5678 at 1:51 AM EST

Newer | Latest | Older