### Bulls and Cows game

Hi, I'm reading this book Principles and Practice using C++ and I'm a total beginner(only about a month), I finished this exorcise on chapter 5, just wanted to ask you if you could give this program a brief look as I'm self studying programming and have no idea whether I'm doing things right or not to this point. the program does what I want it to do, just need some opinions....

The exercise is the Bull and Cow game where you have to guess four numbers between 0 to 9, if you got the number and its position right you get 1 bull, right number but the wrong position 1 cow. then it says player should be able to play repeatedly until got the numbers right and that numbers should be random each play using randint() and srand().

So far the book has only introduced a handful of functions from the libraries, I am familiar with vectors, conditional statements,loops and handling errors and I don't really want to use anything that is not yet covered in the book(unless if I have to).

I'd also really appreciate if you could give some advice on these questions ?
- You'd notice that I have used a lot of this variable 'track' in the program which is really handy to prevent the loops from acting weird, Is it ok to use such variables ?

- What's the difference with declaring variables in or out of main()?

- The book has not explained anything about generating random numbers yet, in the program the random numbers should not be repeated, is there any other way to this than the way I have done it ?(without relying much on the functions that the book has not covered)

Thanks....
 ``12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788`` ``````#include #include vector nums; //vector for user's guesses vector key; //the 4 digit code int track=0; //this variable helps handling various trackings in the while loop and increases by 1 each time that the user enters a number int bull=0; int cow=0; int const guess=4; //each try user guess 4 digits //this function adds numbers 1 to 9 to key vector then shuffles them //only the first 4 elements of the key vector will be used in this program //this function will generate non-repeating random numbers between 1 to 9 int rand_geneator(int y){ srand(y); for(int i=0; i<9; ++i){ key.push_back(i+1); } random_shuffle(key.begin(),key.end()); } //this function takes input from the user and check them //user can only enter 4 digits in each try //each single digit cannot be larger than 9 //each digit entered has to be unique and cannot be repeated in each try int input_check(int x){ for(int i=0; i0 && x==nums[i]){error("Same numbers are not allowed\n");} } if(x>9){error("Number larger than 9");} nums.push_back(x); } //this function checks how many guess the user got right void check_bullcow(){ if(nums[track]==key[track]){ //track is 0 after the very first input is entered in each try, check every user's input individually bull +=1; } for(int i=0; i>n; rand_geneator(n); std::cout<<"Enter 4 digits, press Enter after each digit.\n"; int inputs; while(nums.size()>inputs){ input_check(inputs); check_bullcow(); ++track; win_game(bull); end_guess(); } if(nums.size()>guess){error("only 4 guess");} } catch(exception& e){ cerr<<"error: "<
Last edited on
Topic archived. No new replies allowed.