Hey guys,
Im trying to make a program that guesses the name of an animal in the users head by asking him or her a bunch of questions. The questions, answers, and line number for yes/no are inlcuded in the file animals.txt as follows:
Q Does it have feathers? 2 7
Q Is it a mammal? 3 4
A lizard
Q Does it have stripes? 5 6
Q Does it hop? 10 11
A tiger
Q Is it tasty to eat? 8 9
A owl
A chicken
A elephant
A kangaroo
Im using a struct to organize this information:
struct QandA
{
bool animal; // true if question is an animal
string question; // holds either a question or animal name
int yes;
int no;
};
I've only just started the program but I'm getting errors when i try to try to make codes like:
info[k].question.assign(tempstring,2,(tempstring.length()-6));
I get an error C2228: left of '.assign' must have class/struct/union.
Any advice on how to fix this problem and any additional input would be greatly appreciated.
Thanks in advance,
Code:
// Program attempts to guess the name of an animal in the user's mind by asking a series of questions which have a yes or no answer
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
void main ()
{
struct QandA
{
bool animal; // true if question is an animal
string question; // holds either a question or animal name
int yes;
int no;
};
string lines[200];
QandA info[300];
ifstream source;
char c;
int i = 0;
string bigstring = "";
string file1, tempstring;
source.open("animals.txt");
while(source.fail()) //Code for validation of input file
{
cout<<"Error: Invalid input file name. Please make sure the file is in the default directory or provide full path."<<endl;
cin>> file1;
source.close();
source.clear();
source.open (file1.c_str());
}
while (! source.eof()) //Loading the text from the input file into the string
{
source.get(c);
bigstring += c;
if (c == '\n')
{
lines[i] = bigstring ;
bigstring = "";
i++;
}
//cout<<text[i];
}
/*for (int j=0;j<i;j++)
{
cout<<"Line "<<j<<" is: "<<lines[j]<<endl;
}*/
for (int k=0;k<i;k++)
{
lines[k] = tempstring;
if (tempstring.at(0) == 'A' && tempstring.at(1) == ' ')
{
info[k].animal = true;
info[k].question.assign(tempstring,2,(tempstring.length()-6));
}
if (tempstring.at(0) == 'Q' && tempstring.at(1) == ' ')
{
info[k].animal = false;
info[k].question.assign(tempstring,2,(tempstring.length()-6));
info[k].no.assign(tempstring,(tempstring.length()-3),1);
info[k].yes.assign(tempstring,(tempstring.length()-1),1);
}
}
cout<<info[0];
}