codetoad.com
  ASP Shopping CartForum & BBS
  - all for $20 from CodeToad Plus!
  
  Home || ASP | ASP.Net | C++/C# | DHTML | HTML | Java | Javascript | Perl | VB | XML || CodeToad Plus! || Forums || RAM 
Search Site:
Search Forums:
  Constructor parameters  veronicak5678 at 05:50 on Wednesday, March 05, 2008
 

I am trying to figure out how to modify this program so the file is read from the constructor:

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.size()==Scrambled.size())
{
if(Check(Scrambled,_word))
{
cout << "The word is: " << _word<< 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== 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("/forum/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();
}
}
}

Going off the only example in my book that comes even close to what I need, I have created this horrible thing:



#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(fstream &file, string Scrambled){
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);
}


}
void Descramble (string Scrambled);
};
//******************************************
void Scramble::Descramble(string Scrambled)
{
bool flag=false;
for(int i=0;i<_word.size();i++)
{
if(_word.size()==Scrambled.size())
{
if(Check(Scrambled,_word))
{
cout << "The word is: " << _word<< 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== 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("/forum/dictionary.txt");
word_list=Scramble (file, Scrambled);
word_list.Scramble::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();
}
}
}
How do I pass a file or filename to a constructor? And how do I retrieve "word_list" if I can't return from a construcotr?









CodeToad Experts

Can't find the answer?
Our Site experts are answering questions for free in the CodeToad forums
//








Recent Forum Threads
•  how to read an ascii format file
•  CrossCheck a browserless framework for testing Javascript
•  should i use a multidimensional array???
•  Re: Doubt regarding CoreJava
•  Constructor parameters
•  Help with Easy Prog
•  sort hash by value, numerically
•  onclick multiple functions
•  source code for Voice Command Edotor


Recent Articles
ASP GetTempName
Decode and Encode UTF-8
ASP GetFile
ASP FolderExists
ASP FileExists
ASP OpenTextFile
ASP FilesystemObject
ASP CreateFolder
ASP CreateTextFile
Javascript Get Selected Text


© Copyright codetoad.com 2001-2008