I've got an overdue assignment for a data structures class, and I am having the largest problem with reading in data from a text file. The program is essentially recreating Akinator with a binary seach tree holding the data. Below is the relevant code:
Code:
//header file
class game{
public:
string ans;
BST<string> data;
BSTNode<string>* item;
fstream datafile;
string newQuestion;
const char* receive;
game();
void collect(const char* filename);
string getAns(){
return ans;
}
void ask(BSTNode<string>* item, string ans);
int askQuestion(BSTNode<string>* item, string ans, int i);
void getQuestion(ostream& ostrm);
void appendTree(string ques, string ans){
datafile << ans << ": " << ques << "\n";
};
};
//section where I need the help
void game::collect(const char* filename){
int size = 30;
datafile.open(filename, ios::in);
if(!datafile)
fatal("Error opening stream!");
for(;;) {
getline(datafile, receive, ","); //here's the lil' ........er
if(datafile.eof())
break;
data.insert(receive);
}
if (data.isEmpty()){
cout << "No data in database file. You'll need to make a question first." << endl;
getQuestion(cout);
}
datafile.close();
}
(I should point out a few things. First, assume I put "using namespace std" in another header file. Also, there are a few things, such as fatal() and anything BST related, that are abstract. Those are unrelated to my problem. receive has been initialized to 256 characters in the constructor. Lastly, I need to write to the same file I read from, hence fstream.)
I absolutely cannot use any variation of the get() function to read the data in from a text file. Layout looks something like:
cat: has 4 legs, has tail, has fur
Before I even worry about the colon, I want to focus on the commas. These are the following solutions I've tried:
Code:
dataline.get(receive, size, ",");
dataline.getline(receive, size, ",");
dataline.get(receive, size);
dataline.getline(receive, size);
getline(dataline, receive);
getline(dataline, receive, ",");
Without fail, the following errors appear:
IntelliSense: no instance of overloaded function "getline" matches the argument list
(this one appears a few times per variable format involved) error C2784: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &,std::basic_string<_Elem,_Traits,_Alloc> &)' : could not deduce template argument for 'std::basic_istream<_Elem,_Traits> &' from *insert variable format here*
error C2780: 'std::basic_istream<_Elem,_Traits> &std::getline(std::basic_istream<_Elem,_Traits> &&,std::basic_string<_Elem,_Traits,_Alloc> &,const _Elem)' : expects 3 arguments - 2 provided
I have spoken to multiple teachers, scoured the interwebs, created new project files in the compiler, updated the compiler and service packs to the latest versions, etc. I've tried it all, it still doesn't work, and I'm about ready to chuck my laptop across the room in anger. Is there anyone out there who can give me some sort of solution to this consistent nightmare?