Need assistance re-writing program

Im having trouble with my program I wrote it using tree nodes but found out that im only sapose to use if else and while loop dont remember how to use the old stuff any ideas?

This program takes inputs from the user to classify an animal as one of Insect, Reptile, Bird and Mammal. The program should first ask a yes or no question and starting classification between Vertebrate and Invertebrate. If the user’s input (Y/N) indicates that the animal is Invertebrate then we classify the animal as an Insect and stop asking more questions.If it is Vertebrate, we print second question out and ask for another input from the user to choose from Cold-blooded and Warm-blooded animals. If it is Cold-blooded, we classify the animal as Reptile and stop the program. Otherwise we continue to the next question and classify the animal as Bird or Mammal.The program should take both lower case and upper case letter from the user, and run repeatedly if the user wants to start over.

I'm having trouble on how to even begin to write it.

Here's and example he gave:

Animal Classifier

Does the animal have backbones (Y/N)? y
The animal is a Vertebrate. Let's continue...

Does the animal's body feel cold (Y/N)? N
The animal is Warm-blooded. Let's continue...

Can it fly (Y/N)? Y
The animal is a Bird.

Do you want to start over (Y/N)? y

Animal Classifier
Does the animal have backbones (Y/N)? Y
The animal is a Vertebrate. Let's continue...

Does the animal's body feel cold (Y/N)? y
The animal is a Reptile.

Do you want to start over (Y/N)? n
Press any key to continue . .

my code works but i have to rewrite it some how to make it simpler
#include <iostream>
#include <string>
#include <sstream>

using namespace std;

struct treeNode {
string QorA;
struct treeNode *yes;
struct treeNode *no;
};

typedef enum resp_t { YES, NO };
struct treeNode *init(void);
struct treeNode *newTreeNode(const string&);
void insert(struct treeNode *root, int val, int x = 0, int y = 0);
resp_t getAnswer(const string&);

int main(int argc, char *argv[]) {
struct treeNode *p, *head = init();
resp_t resp;

do {
p = head;
while ((p->yes != NULL) && (p->no != NULL)) {
resp = getAnswer(p->QorA);
if (resp == YES) {
p = p->yes;
} else {
p = p->no;
}
}
cout << p->QorA << endl << endl;
resp = getAnswer("Continue? ");
} while (resp == YES);

return 0;
}

struct treeNode *newTreeNode(const string &q) {
struct treeNode *n = new struct treeNode;
n->QorA = q;
n->yes = n->no = NULL;
return n;
}

struct treeNode *init() {
struct treeNode *p, *head;

head = newTreeNode(string("Does the animal have a backbone? "));
head->no = newTreeNode(string("Insect."));
head->yes = newTreeNode(string("Does it have cold blood? "));
p = head->yes;
p->yes = newTreeNode(string("Reptile."));
p->no = newTreeNode(string("Can it fly? "));
p = p->no;
p->yes = newTreeNode(string("Bird."));
p->no = newTreeNode(string("Mammal."));

return head;
}

resp_t getAnswer(const string &s) {
static const string valid("yn");
stringstream ss;
string line;
bool inputOk;
char z;
size_t pos;
resp_t resp;

cout << s;
for (inputOk = false; inputOk == false; ) {
getline(cin,line);
ss.clear(); ss.str(line);
if ((ss >> z) && ((pos = valid.find(tolower(z))) != string::npos)) {
inputOk = true;
resp = static_cast<resp_t>(pos);
} else {
cout << "invalid input, try again" << endl << "> ";
}
}
return resp;
}

Last edited on
That's a pretty cool implementation. I find it incredible that that solution was turned down. Academics, they’re so formulaic at times.

Does the program work? Are you sure you need to rewrite it (if it works)?
Topic archived. No new replies allowed.