I am trying to figure out how to modify this program so the file is read from the constructor:
Would I just move the code for the "play" option to the Scramble class?Code:#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(); } } }



LinkBack URL
About LinkBacks



