By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
431,780 Members | 1,520 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 431,780 IT Pros & Developers. It's quick & easy.

Animal Guessing binary tree game C++

P: 8
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(question) 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
Share this Question
Share on Google+
5 Replies


RedSon
Expert 5K+
P: 5,000
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

P: 8
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
Expert 5K+
P: 5,000
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

P: 8
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
Expert 5K+
P: 5,000
So now give us an example input and where the code breaks down.
Mar 23 '07 #6

Post your reply

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