I have to create a game where there's a four digit number that I predetermine in the code and the user has to try and guess the correct number.
I have set the number to be 1407.
If the user guesses 1047, he/she would be told that they have 2 bulls and 2 cows.
A bull means they have the right number in the correct position, a cow means they have the right number, but not in the right position. The objective of the game is to get a total of four bulls and then quit. Also each number they guess must be unique, so no repeating numbers.
I have it set up so it can correctly determine how many bulls or how many cows there are. The problem I have is I can't get the program to loop if they don't have four bulls.
This is what I have so far
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
|
int main()
{
cout << "You're now playing 'Bulls and Cows'." << endl
<< "The objective is to correctly guess the random four digit number" << endl
<< "in the correct order. You must not have repeating numbers in your guess. << endl
<< "EX: A guess of '1234' is okay. A guess of '1224' is not okay." << endl
<< "Have fun and enjoy!" << endl
<< "Please enter four different numbers with a space in between each" << endl
<< "number for your guess. Also please make sure you enter your numbers in" << endl
<< "the order you think is correct. Add a space plus '|' at the end of your guess." << endl;
vector<int> numbers = {1, 4, 0, 7};
vector<int> numGuess;
for (;;)
{
int bulls = 0;
int cows = 0;
for (int guess; cin>>guess;)
numGuess.push_back(guess);
if (numGuess[0] == numbers[0])
bulls++;
else if (numGuess[0] == numbers[1] || numbers[2] || numbers[3])
cows++;
if (numGuess[1] == numbers[1])
bulls++;
else if (numGuess[1] == numbers[0] || numbers[2] || numbers[3])
cows++;
if (numGuess[2] == numbers[2])
bulls++;
else if (numGuess[2] == numbers[0] || numbers[1] || numbers[3])
cows++;
if (numGuess[3] == numbers[3])
bulls++;
else if (numGuess[3] == numbers[0] || numbers[1] || numbers[2])
cows++;
cout << bulls << " bulls and " << cows << " cows" << endl;
numGuess.clear();
if(bulls == 4)
return 0;
}
}
|
As it sits now, if I input "7 0 4 1 |"
it will output this:
0 bulls and 4 cows
terminate called after throwing an instance of 'Range_error'
what(): Range error: 0
Aborted
|
I'm assuming that this is some error catch given in the header file I was told to use, but I'm not sure why I'm getting this error.
I clear the numGuess vector after telling the user how many bulls/cows they have so that when it loops around it restarts and it won't be push_back into a vector that already has numbers in it.