1. #include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
class Ghost{
public:
void insert();
bool search(string key);
void play();
};
void Ghost::insert(){
ifstream ifs("wiki-100k copy.txt");
vector<string> vec;
// Reads in words from file
copy(std::istream_iterator<string>(ifs),
istream_iterator<string>(), back_inserter(vec));
//Outputs words from file
cout<<endl<<"List of words ";
copy(vec.begin(), vec.end(), ostream_iterator<string>(cout,
" "));
}
bool Ghost::search(string key){
vector<string> vec;
vector<string>::iterator it;
while(key.length() >= 4){
cin>>key;
it = find (vec.begin(), vec.end(), key);
if (it != vec.end()){
2. cout << "key "<< *it <<" found in LIST " << ' ';
return true;
}else {
cout << "Word is not in list" << endl;
return false;
}
}
}
void Ghost::play(){
cout << "---Welcome to the Ghost Game!---" << endl;
cout << "Player one enter the first letter:" << endl;
string input;
cin >> input;
search(input);
}
int main() {
Ghost G;
G.play();
}
Please help me debug this program. Currently do not know why
I am getting this error.
Solution
the problem with the above code is if the while statement is not
executed or if the value does not enters the while statement
there is no return statement after while which is the reason for
3. the error. A function which is defined with some return type
should return a value. so,make sure to keep a return statement
after while statement is over.
#include <iostream>
#include <fstream>
#include <algorithm>
#include <vector>
#include <string>
#include <iterator>
using namespace std;
class Ghost{
public:
void insert();
bool search(string key);
void play();
};
void Ghost::insert(){
ifstream ifs("wiki-100k copy.txt");
vector<string> vec;
// Reads in words from file
copy(std::istream_iterator<string>(ifs),
istream_iterator<string>(), back_inserter(vec));
//Outputs words from file
cout<<endl<<"List of words ";
copy(vec.begin(), vec.end(), ostream_iterator<string>(cout,
4. " "));
}
bool Ghost::search(string key){
vector<string> vec;
vector<string>::iterator it;
while(key.length() >= 4){
cin>>key;
it = find (vec.begin(), vec.end(), key);
if (it != vec.end()){
cout << "key "<< *it <<" found in LIST " << ' ';
return true;
}else {
cout << "Word is not in list" << endl;
return false;
}
}
return NULL;
}
void Ghost::play(){
cout << "---Welcome to the Ghost Game!---" << endl;
cout << "Player one enter the first letter:" << endl;
string input;
cin >> input;
search(input);
}