I could really use some help with this bull and cow assign.

Moderators: Darobat, RecursiveS, Dante Shamest, Bugdude, Wizard

I could really use some help with this bull and cow assign.

Code: Select all
`// This program demonstrates random numbers.#include <iostream>#include <cstdlib> // rand and srand#include <ctime> // For the time functionusing namespace std;int main(){      unsigned seed = time(0);   srand(seed);      int guess[4];    int   secretCode[] = {rand()%10,rand()%10,rand()%10,rand()%10};   int bull = 0;   int cow = 0;   cout << "\t\t\tHow to play Bulls and Cows:\n";   cout << " The game involves two players, a coder and a cracker.";    cout << "The coder (a computer in\n this case)";   cout << "chooses the four-digit number with all different digits and keeps it\n secret. The cracker tries to";   cout << "guess the secret number. After a guess, the coder\n gives you information about number of digits";   cout << "which were correctly guessed but\n in the wrong place (they are called cows) and how many are";   cout << "both the right digit\n and in the correct place (they are called bulls).";   cout << "For example, If the coder\n chooses the secret number 0419 and the cracker guesses 6039, then the ";   cout << "coder\n scores This as 1 cow, since 0 is a digit in the secret number that’s in the\n wrong place,";   cout << "and 1 bull 9 is in the right place." << endl;                         do            {      secretCode[4];      break;      } while (secretCode[0] == secretCode[1] || secretCode[0] == secretCode[2] || secretCode[0] == secretCode[3] || secretCode[0] == secretCode[0] ||              secretCode[1] == secretCode[2] || secretCode[1] == secretCode[3] || secretCode[1] == secretCode[0] || secretCode[1] == secretCode[1] ||             secretCode[2] == secretCode[3] || secretCode[2] == secretCode[0] || secretCode[2] == secretCode[1] || secretCode[2] == secretCode[2] ||             secretCode[3] == secretCode[0] || secretCode[3] == secretCode[1] || secretCode[3] == secretCode[2] || secretCode[3] == secretCode[3] );            for(int i = 0; i < 4; ++i)      {         cout << secretCode[i];      }               cout << "\n\n\tGuess 4 digets seperated by one space between each diget." << endl;      do   {      bull =0;      cow = 0;      cin >> guess[0];      cin >> guess[1];      cin >> guess[2];      cin >> guess[3];       if (guess[0] == secretCode[0])          ++bull;      if (guess[0] == secretCode[1] || guess[0] == secretCode[2] || guess[0] == secretCode[3])          ++cow;       if (guess[1] == secretCode[1])          ++bull;      if (guess[1] == secretCode[2] || guess[1] == secretCode[3] || guess[1] == secretCode[0])          ++cow;      if (guess[2] == secretCode[2])          ++bull;      if (guess[2] == secretCode[3] || guess[2] == secretCode[0] || guess[2] == secretCode[1])          ++cow;      if (guess[3] == secretCode[3])          ++bull;      if (guess[3] == secretCode[0] || guess[3] == secretCode[1] || guess[3] == secretCode[2])          ++cow;      if (bull == 4)         break;      else       {         cout << bull << " is your # of bulls.\n" << cow << " is your # of cows.\n\n";         cout << "\nNOW TRY AGAIN\n";      }   } while (bull!=4);   cout << "\n\n\n\nYOU'RE A WINNER!!!\n\n\n";   return 0;}`
Attachments
BullsAndCows2.cpp
milleraj66

Posts: 3
Joined: Sun Feb 26, 2012 2:06 am

Re: I could really use some help with this bull and cow assign.

Is this the same task as http://www.cpp-home.com/forum/viewtopic.php?f=6&t=17470?
In any case you should say what is the problem with your code. It's allways hard to guess what is wrong if you don't tell.

The first problem I see is the incomplete initialisation of the random 4 digit number.
What is the foolowing code supposed to do?
Code: Select all
`do            {      secretCode[4];      break;      } while (...)`

I guess you want to put your random number assignment inside this look.
Who needs a signature anyway.

exomo

Posts: 879
Joined: Fri Sep 26, 2003 12:30 pm

Re: I could really use some help with this bull and cow assign.

Sorry about that im new to this site. But I'm trying to get four different random digets, just to specify im trying to get the secretCode array to only be used when 4 different random numbers are cho
sen.
milleraj66

Posts: 3
Joined: Sun Feb 26, 2012 2:06 am

Re: I could really use some help with this bull and cow assign.

BTW welcome to the c++ home forums

If you want four different values in your array you have to assign new values when they are not different.
Code: Select all
`do {    secretCode[0]=rand()%10;    secretCode[1]=rand()%10;    secretCode[2]=rand()%10;    secretCode[3]=rand()%10;} while (secretCode[0] == secretCode[1] || secretCode[0] == secretCode[2] || secretCode[0] == secretCode[3] ||             secretCode[1] == secretCode[2] || secretCode[1] == secretCode[3] ||             secretCode[2] == secretCode[3] );`

I also removed some of the conditions in your while(...), some of them were just doubles, and more important conditions like secretCode[0] == secretCode[0] are allways true, resulting in an infinite loop

even better would be if you only generate numbers that are not in one of the previous array fields, but that is a little more complicated. Or add the numbers from 0 to 9 to a vector, shuffle it and take the first 4 numbers. there a
Who needs a signature anyway.

exomo

Posts: 879
Joined: Fri Sep 26, 2003 12:30 pm

Re: I could really use some help with this bull and cow assign.

Thanks man,
I was banging my head against the wall forever trying to figure this out.
will definitly use this site when i need help.

Thanks again!!!!
milleraj66

Posts: 3
Joined: Sun Feb 26, 2012 2:06 am