Here's the C++ solution to Exercise: Cows and Bulls.

#include <iostream>#include <string>#include <stdlib.h>#include <time.h>using namespace std;string generate_secret_number() {// Generates a 4-digit secret number (between 1000 and 9999)srand(time(0));int secret_number = rand() % 9000 + 1000;return to_string(secret_number);}string get_user_input() {int guess = 0;cin >> guess;// Checks if guess is a 4-digit number (between 1000 and 9999)while (guess < 1000 || guess > 9999) {cout << "Invalid input!" << endl;cin >> guess;}return to_string(guess);}int count_bulls(string secret_string, string guess_string, bool secret_digit_used[], bool guess_digit_used[]) {int count = 0;// Iterate over the 4 digits and count the number of equal digitsfor (int i = 0; i < 4; i++) {if (secret_string[i] == guess_string[i]) {count++;// Mark digits usedsecret_digit_used[i] = true;guess_digit_used[i] = true;}}return count;}int count_cows(string secret_string, string guess_string, bool secret_digit_used[], bool guess_digit_used[]) {int count = 0;// Iterate over all possible position pairs and increase count if digits are equalfor (int i = 0; i < 4; i++) {for (int j = 0; j < 4; j++) {// Don't use an alredy used digitif (secret_digit_used[i] || guess_digit_used[j])continue;if (secret_string[i] == guess_string[j]) {count++;// Mark digits usedsecret_digit_used[i] = true;guess_digit_used[j] = true;}}}return count;}int main() {// Generate secret numberstring secret_string = generate_secret_number();cout << "Secret number chosen (between 1000 and 9999)." << endl;// cout << secret_string << endl; // For debugging ...cout << "Start guessing! ... " << endl;int nturns = 0;while (true) {// Input guess numberstring guess_string = get_user_input();nturns++;// For marking which digits have been "used"bool secret_digit_used[4] = {false, false, false, false};bool guess_digit_used[4] = {false, false, false, false};// Count bullsint bulls = count_bulls(secret_string, guess_string, secret_digit_used, guess_digit_used);// Check if guess is correctif (bulls == 4) {cout << "Congratulations, you did it!" << endl;cout << "You took " << nturns << " to guess the number" << endl;break;}// Count cowsint cows = count_cows(secret_string, guess_string, secret_digit_used, guess_digit_used);// Output the informationcout << "Turn " << nturns << ": Your guess " << guess_string << " has " << bulls << " bulls and " << cows << " cows." << endl;}// Output secret number once game is overcout << "Secret number is " << secret_string << endl;return 0;}

^{To try out this code, follow this link.}

Sample interaction 1:

Secret number chosen (between 1000 and 9999).Start guessing! ...1234Turn 1: Your guess 1234 has 0 bulls and 0 cows.5678Turn 2: Your guess 5678 has 2 bulls and 0 cows.5656Turn 3: Your guess 5656 has 1 bulls and 0 cows.5757Turn 4: Your guess 5757 has 2 bulls and 1 cows.8909Turn 5: Your guess 8909 has 1 bulls and 0 cows.9090Turn 6: Your guess 9090 has 0 bulls and 1 cows.5759Turn 7: Your guess 5759 has 1 bulls and 2 cows.7579Turn 8: Your guess 7579 has 1 bulls and 3 cows.7759Turn 9: Your guess 7759 has 0 bulls and 4 cows.5977Congratulations, you did it!You took 10 to guess the numberSecret number is 5977

Sample interaction 2:

Secret number chosen (between 1000 and 9999).Start guessing! ...444Invalid input!10000Invalid input!1212Turn 1: Your guess 1212 has 1 bulls and 0 cows.3434Turn 2: Your guess 3434 has 2 bulls and 0 cows.5656Turn 3: Your guess 5656 has 0 bulls and 0 cows.7878Turn 4: Your guess 7878 has 0 bulls and 0 cows.9090Turn 5: Your guess 9090 has 1 bulls and 0 cows.1111Turn 6: Your guess 1111 has 0 bulls and 0 cows.9999Turn 7: Your guess 9999 has 1 bulls and 0 cows.2349Turn 8: Your guess 2349 has 0 bulls and 4 cows.9432Congratulations, you did it!You took 9 to guess the numberSecret number is 9432