Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi, I need to create a C++ program that can ask twenty questions by getting the questions and the 2 answers from a file. I

Hi, I need to create a C++ program that can ask twenty questions by getting the questions and the 2 answers from a file. I have the code but it does not want to compile

Write a program that plays with the user the game commonly known as Twenty Questions. However, in this version of the game, the program will not be limited to asking twenty questions. Instead, it may ask any number of questions. Also the program will learn and improve from playing each game.

Is it a non-living object?

Elephant

if not:

Stone

If the user chooses that it is not a living object, it will display that if the anime guessed is a stone. If not the program will save the question and the answer.

header file:

#ifndef BINARY_TREE_H #define BINARY_TREE_H

#include #include

class binaryTree { public: class node; binaryTree(); void addRoot(const std::string &data); void addLeft(node *nd, const std::string &data); void addRight(node *nd, const std::string &data);

node *getRoot();

std::string get(node *node); bool isEmpty();

void print(std::ostream &os=std::cout); static binaryTree read(const char *fname); void write(const char *fname); private: void print(std::ostream &os, node *nd, int indent); static node *read(std::ifstream &ifs); void write(std::ofstream &ofs, node *nd, int depthh); node *root; };

struct binaryTree::node { node(const std::string &data); std::string data; node *l, *r; };

#endif

cpp file:

#include #include #include #include

#include "binaryTree.h"

using namespace std;

binaryTree::binaryTree() : root(0) {}

void binaryTree::addRoot(const string &data) {root = new node(data);}

void binaryTree::addLeft(node *nd, const string &data) { if (nd == 0) throw "Attempt addLeft on null"; if (nd->l != 0) throw "Attempt addLeft on nd with existing l child"; nd->l = new node(data); }

void binaryTree::addRight(node *nd, const string &data) { if (nd == 0) throw "Attempt addRight on null"; if (nd->r != 0) throw "Attempt addRight on nd with existing r child"; nd->r = new node(data); }

typename binaryTree::node *binaryTree::getRoot() {return root;}

bool binaryTree::isEmpty() {return root == 0;}

void binaryTree::print(std::ostream &os) {print(os, root, 0);}

void binaryTree::print(std::ostream &os, node *nd, int indent) { if (nd == 0) return; os << string(indent*3, ' ') << nd->data << endl; print(os, nd->l, indent+1); print(os, nd->r, indent+1); }

binaryTree binaryTree::read(const char *fname) { binaryTree tree; std::ifstream ifs(fname);

if (!ifs) { std::cerr << "*** Warning " << fname << " not found -- starting with new tree" << std::endl; return tree; } tree.root = read(ifs); return tree; }

typename binaryTree::node *binaryTree::read(std::ifstream &ifs) { string line; if (getline(ifs, line)) if (line == "") return 0; else { int pos = line.find_first_not_of(' '); if (pos != string::npos) line = line.substr(pos); node *nd = new node(line); nd->l = read(ifs); nd->r = read(ifs); return nd; } }

void binaryTree::write(const char *fname) { ofstream ofs(fname);

if (!ofs) { ostringstream oss; oss << fname << " could not be opened for output"; throw oss.str(); return; }

write(ofs, root, 0); ofs.close(); }

void binaryTree::write(ofstream &ofs, node *nd, int depthh) { if (nd == 0) { ofs << "" << endl; return; } for (int i = 0; i < depthh; i++) ofs << ' '; ofs << nd->data << endl; write(ofs, nd->l, depthh+1); write(ofs, nd->r, depthh+1); }

binaryTree::node::node(const string &data) : data(data), l(0), r(0) {}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Transactions On Large Scale Data And Knowledge Centered Systems Xxviii Special Issue On Database And Expert Systems Applications Lncs 9940

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Qimin Chen

1st Edition

3662534541, 978-3662534540

More Books

Students also viewed these Databases questions

Question

Does it have at least one-inch margins?

Answered: 1 week ago

Question

Does it highlight your accomplishments rather than your duties?

Answered: 1 week ago