473,598 Members | 3,252 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Animal Guessing binary tree game C++

8 New Member
Hi, i have a bit of a problem here.

I have an assignment to do an animal guessing game using an original database and updating it as the user enters new animals in it. The program enters the file data into an array of structures, with the animal and question in the same variable(questi on) and the bool isAnimal tells me whether its an animal or a question. If its a question it is followed by two numbers; one for the position of the line to go to if the user enters "no" and another for "yes". The program works fine until the user inputs "no" for a new animal(updated animal) after he enters yes for a new question(that he entered the last time to differentiate between animals). Here is the code, and sorry if the variable names are bad. Im using windows xp and visual c++ 2005.

Expand|Select|Wrap|Line Numbers
  1. *snipped by Moderator*
  2.  
  3.     if(answer1 == "yes")
  4.         cout<<" I WIN!!!"<<endl << endl;
  5.     else
  6.         if(answer1 == "no")
  7.         {
  8.  
  9.             cout << " I give up, what were you thinking of?" <<endl;
  10.             cin  >> game[x].question;
  11.             game[x].isAnimal = true;
  12.  
  13.             x++;
  14.  
  15.             cout<< "Please type a question that has a yes answer for "<< game[x - 1].question
  16.                 << endl << " and a no answer for " << game[game[c].no].question << " (without a question mark):" << endl;
  17.  
  18.             cin.ignore();
  19.  
  20.             getline(cin, game[x].question);
  21.  
  22.             game[x].isAnimal = false;
  23.             game[x].no = game[c].no;
  24.             game[x].yes = x - 1 ;
  25.             game[c].no = x; 
  26.  
  27.             x++;
  28.  
  29.         }
  30. *snipped by Moderator*
  31.  
Mar 23 '07 #1
5 8920
RedSon
5,000 Recognized Expert Expert
The experts on this site are more than happy to help you with your problems but they cannot do your assignment/program for you.

So you are aware of the rules please read the Posting Guidelines and particularly the Coursework Posting Guidlines.

MODERATOR
Mar 23 '07 #2
Kraken
8 New Member
sorry i did not mean for you to solve it for me, i just want to know why its not working.

By the way, x is the position of the next empty cell in the array of structures and c is the position of the line.

Thanks
Mar 23 '07 #3
RedSon
5,000 Recognized Expert Expert
No problem, someone can help you and point you in the right direction. You just need to be aware of our homework policy to make sure that you are not in violation.

And, I don't know if I edited down your code properly to the offending areas, but you may repost your code edited down to only the few lines that are actually causing the problem. This prevents others who are on a similar assignment from copying and using all of your code.
Mar 23 '07 #4
Kraken
8 New Member
Yeah thanks, i think this part better represents my problem:

Expand|Select|Wrap|Line Numbers
  1. cout <<"Think of an animal and i will try to guess its name" << endl << endl;
  2.  
  3.     cout<< game[1].question <<"? [yes/no]" << endl;
  4.  
  5.     int c = 1;
  6.     bool exit = false;
  7.  
  8.     while(!exit)
  9.     {
  10.  
  11.         cin >> answer;
  12.  
  13.         if(answer == "yes")
  14.         {
  15.             if(game[game[c].yes].isAnimal == true)
  16.             {
  17.                 cout<< "Is it a "<< game[game[c].yes].question << "? [yes/no]" << endl;
  18.                 cin >> answer1;
  19.                 exit = true;
  20.             }
  21.             else
  22.                 if(game[game[c].yes].isAnimal == false)
  23.                 {
  24.                     cout << game[game[c].yes].question << "? [yes/no]" << endl;
  25.                     c = game[c].yes;
  26.                 }
  27.         }
  28.         else
  29.             if(answer == "no")
  30.             {
  31.                 if(game[game[c].no].isAnimal == true)
  32.                 {
  33.                     cout<< "Is it a "<< game[game[c].no].question << "? [yes/no]" << endl;
  34.                     cin >> answer1;
  35.                     exit = true;
  36.                 }
  37.                 else
  38.                     if(game[game[c].no].isAnimal == false)
  39.                     {
  40.                         cout << game[game[c].no].question << "? [yes/no]" << endl;
  41.                         c = game[c].no;
  42.                     }
  43.             }
  44.     }
  45.     cout << endl;
  46.  
  47.  
  48.  
  49.     if(answer1 == "yes")
  50.         cout<<" I WIN!!!"<<endl << endl;
  51.     else
  52.         if(answer1 == "no")
  53.         {
  54.  
  55.  
  56.             cout << " I give up, what were you thinking of?" <<endl;
  57.             cin  >> game[x].question;
  58.             game[x].isAnimal = true;
  59.  
  60.             x++;
  61.  
  62.             game[x].question = game[game[c].no].question;
  63.  
  64.             cout<< "Please type a question that has a yes answer for "<< game[x - 1].question
  65.                 << endl << " and a no answer for " << game[game[c].no].question << " (without a question mark):" << endl;
  66.  
  67.             cin.ignore();
  68.  
  69.             getline(cin, game[game[c].no].question);
  70.  
  71.             game[game[c].no].isAnimal = false;
  72.             game[game[c].no].no = x;
  73.             game[game[c].no].yes = x - 1;
  74.  
  75.             x++;
  76.  
  77.         }
Mar 23 '07 #5
RedSon
5,000 Recognized Expert Expert
So now give us an example input and where the code breaks down.
Mar 23 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

1
3397
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am > cs students and are now facing difficult problems in understanding > what a binary tree is, how it works, and the algorithm to display, > arrange, find, delete the leaf node in binary tree. > > I hope that anyone with expereince in building binary tree could help > me in explaining the binary tree functions, and give a sample code of > the whole picture behind the...
8
2766
by: Jerry Khoo | last post by:
hello, everybody, i am kinda new here, nice to meet u all. Now, i am cs students and are now facing difficult problems in understanding what a binary tree is, how it works, and the algorithm to display, arrange, find, delete the leaf node in binary tree. I hope that anyone with expereince in building binary tree could help me in explaining the binary tree functions, and give a sample code of the whole picture behind the tree. And...
11
2516
by: jova | last post by:
Is there a difference between a Binary Tree and a Binary Search Tree? If so can someone please explain. Thank You.
3
2847
by: tsunami | last post by:
hi all; I have an array and want to insert all the elements from this array to a binary search tree.That array is an object of the class of a stringtype which includes overloaded "< > = ==" functions and every other thing it needs. void InsertElementFromArray(StringType Array,int first element,int last element);
4
9012
by: Tarique Jawed | last post by:
Alright I needed some help regarding a removal of a binary search tree. Yes its for a class, and yes I have tried working on it on my own, so no patronizing please. I have most of the code working, even the removal, I just don't know how to keep track of the parent, so that I can set its child to the child of the node to be removed. IE - if I had C / \ B D
5
9559
by: pembed2003 | last post by:
Hi, I have a question about how to walk a binary tree. Suppose that I have this binary tree: 8 / \ 5 16 / \ / \ 3 7 9 22 / \ / \ / \
0
6344
by: nimisha | last post by:
I have been trying to learn how to do a c++ tic tac toe game, with first implementing the game tree in a function and then having the AI in another function which uses minimax algorithm. I came across the following code in my search for sources that would help me get a better understanding: /** evaluate board state from player's point of view */ int evaluate(Node node, int player) { /* using NEGMAX version of MINIMAX */ int value...
1
2922
by: hn.ft.pris | last post by:
I have the following code: Tree.h defines a simple binary search tree node structure ########## FILE Tree.h ################ #ifndef TREE_H #define TREE_H //using namespace std; template <typename Tclass Tree{ private: Tree<T*left;
7
3860
by: Vinodh | last post by:
Started reading about Binary Trees and got the following questions in mind. Please help. Definition of a Binary Tree from "Data Structures using C and C++ by Tanenbaum" goes like this, "A binary tree is a finite set of elements that is either empty or is partitioned into three disjoint subsets. The first subset contains a single element called the 'Root' of the tree. The other two subsets are themselves binary trees, called the 'Left'...
0
7987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8397
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8264
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6718
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
3897
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3939
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2412
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1250
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.