Make your own free website on Tripod.com
« May 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 with Dictionary
Thursday, 6 March 2008
Scramble with Dictionary

#include <iostream>

#include <fstream>

#include <string>

#include <vector>

using namespace std;

const char File[] = "dictionary.txt";

class Dictionary {public:

Dictionary()

{

retrieve();

}

vector<string> getWords()

{

return words;

}

int Size()

{

return words.size();

}

private:

vector<string> words;

int retrieve()

{

fstream file;

string myWord;

file.open(File, ios::in);

if (file.fail())

{

cout <<
"Error! Input file cannot be opened.\n\n" ;return 0;

}

file >> myWord;

while (!file.eof())

{

words.push_back(myWord);

file >> myWord;

}

file.close();

}

};

class Scramble {

private:

vector<string> DictionaryWords;

int size;

bool Check (string T, string S);

public:

Scramble(Dictionary myLibrary)

{

size= myLibrary.Size();

DictionaryWords = myLibrary.getWords();

}

string getWord(
int i)

{

return DictionaryWords.at(i);

}

void Descramble (string Scrambled);

};

void Scramble::Descramble(string Scrambled)

{

bool flag=false; // stop once found

for(int i=0;i < DictionaryWords.size() && !flag; i++)

{

if(DictionaryWords.at(i).size()==Scrambled.size())if(Check(Scrambled,DictionaryWords.at(i)))

{

cout << "The word is: " << DictionaryWords.at(i)<< endl; flag=

true;

}

}

if (flag)return; else

{

cout<<
"The word: " << Scrambled <<" is not found"<< endl;

}

}

bool Scramble::Check(string T, string S)

{

int n=T.size();

int Found=0; //count matches

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 ++;} // matched

}

if (Found==n)return true; else return false;

}

 

void ClearScreen();

void flush()

{

char ch; while (ch=cin.get()!= '\n');

}

 

int main()

{

Dictionary scrmbl;

string Scrambled;

char choice;

cout << "*********Scramble Game**********" <<endl << endl;

while (true){

cout << "Please choose one of the following options:" << endl;cout <<

"1- Enter a word to descramble" << endl;cout << "2- Quit" << endl << endl;

cin>>choice;

ClearScreen();

if (choice == '1')

{

cout <<
"Please Enter a New Scrambled Word: ";

cin >> Scrambled;

Scramble test(scrmbl);

test.Descramble(Scrambled);

}

else if(choice == '2')

{

cout <<
"Thank you for playing. Press any key to exit.\n";

getchar();

return 0;

}

else

{

cout <<
"Invalid entry. Please select again." << endl;

}

}

}

 

 

 

void ClearScreen()

{

#ifdef _WIN32

system("cls");//windows

#endif

}


Posted by veronicak5678 at 10:11 PM EST
Updated: Thursday, 6 March 2008 10:44 PM EST

Newer | Latest | Older