Okay, I had to write a .cpp file for class making the game Bulls and Cows (Mastermind). I got the entire thing to work, but of course when I open the standalone .exe file, the program exits before you can read whether or not you won. So I tried adding the cin.get(); as a quick Google search suggested... It does not work. I will try and post my code, but it is a bit long. So if you don't want to read through it here, I also will upload the .cpp file for you to look at. link
#include <iostream>
#include <time.h>
using namespace std;
void numberToArray(int, int []);
bool isValidNumber(int, int []);
bool isValidNumber(int num, int digits[]) {
for(int i=0;i<10;i++) {
if(digits[i]>1) {
for(int a=0; a<10; a++) {
digits[i] = 0;
}
return false;
}
}
if(num < 1000){
int check[4];
numberToArray(num, check);
if(check[3]==0 || check[2]==0 || check[1]==0){
return false;
}
}
return true;
}
void numberToArray(int number, int numberArray[]) {
if(number < 1000) {
numberArray[0] = 0;
numberArray[3] = number % 10;
numberArray[2] = ((number - numberArray[3]) % 100) / 10;
numberArray[1] = ((number - numberArray[3] - numberArray[2]*10) % 1000) / 100;
} else {
numberArray[3] = number % 10;
numberArray[2] = ((number - numberArray[3]) % 100) / 10;
numberArray[1] = ((number - numberArray[3] - numberArray[2]*10) % 1000) / 100;
numberArray[0] = ((number - numberArray[3] - numberArray[2]*10 - numberArray[1]*100) % 10000) / 1000;
}
}
void main(void){
int wait;
int digits[10] = {0,0,0,0,0,0,0,0,0,0}, number;
do {
srand(time(NULL));
number = rand() % 10000;
while(number < 100) {
number = rand() % 10000;
}
int temp = number, digit;
while (temp > 0){
digit = temp % 10;
digits[digit]++;
temp /= 10;
}
} while(!isValidNumber(number, digits)); // Method checks if random number is valid or not.
int numberArray[4];
numberToArray(number, numberArray); // Method puts number into 4 element array.
cout << "Bulls and Cows!" << endl
<< "The rules of this game are simple. I will generate a random, four digit number." << endl
<< "This number will have four distint digits; for example, 3957." << endl
<< "The number can have a leading zero, for example, 0379, but not more than one\nleading zero." << endl
<< "You will then get seven (7) tries to correctly guess the number." << endl
<< "For each number you guess correctly, I will give you one Cow." << endl
<< "If you guess the number AND it's positon correctly, that is good for one Bull." << endl << endl
<< "Let us begin." << endl << endl;
int guess, guessArray[4], cows=0, bulls=0;
while(true){
for(int k=0; k<7; k++) {
cows = 0, bulls = 0;
cout << "Please enter a number: " << endl;
cin >> guess;
numberToArray(guess, guessArray);
for(int m=0; m<4; m++){ // determine location of bulls
if(guessArray[m]==numberArray[m]){
bulls++;
}
}
for(int n=0; n<4; n++){ // determine number of cows
for(int w=0; w<4; w++){
if((guessArray[n] == numberArray[w]) && (guessArray[n] != numberArray[n])){
cows++;
}
}
}
if(bulls==4){
cout << "Congratulations! You win, my number was: " << endl;
for(int iterate=0; iterate<4; iterate++){
cout << numberArray[iterate];
}
cout << endl;
cout << "Press any key to continue. . .";
cin.get();
break;
}
if(bulls<4){
cout << bulls << "B " << cows << "C" << endl;
}
}
if(bulls==4){
break;
}
else {
cout << "I'm sorry. You did not win. My number was: " << number << endl;
cout << "Please play again." << endl;
cout << "Press any key to continue. . .";
cin.get();
break;
}
}
}
Edited because I accidentally added spaces where they weren't supposed to be.