For loop trouble, possible logic issue

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.
Last edited on
You read ints on line 21. The | is not an int and it does terminate the loop. Good for the first try (unless the user gives less than 4 digits).

However, the failure to convert | into number has two side effects.
1. The | remains in the stream.
2. The stream's failbit is set. All following input operations fail automatically. All.

Why don't you read exactly 4 numbers with the loop?

Note on conditions:
The a==b || c || d is same as (a==b) || (c) || (d)
is same as calling foo:
1
2
3
4
5
6
bool foo( int a, int b, int c, int d ) {
  if (a==b) return true;
  if (c) return true;
  if (d) return true;
  return false;
}

You should probably use a==b || a==c || a==d
So I guess what I should ask, is how to only make it read in just 4 numbers?
What do you know about the for-loop, its syntax and components?
I know that the for loop has different cases you could have.
There's range based for loops, there's also the basic for loops where it's
 
for(initialization; some sort of condition; then you can increment your initialization here)
http://www.cplusplus.com/doc/tutorial/control/ has explanation that might clarify the details.
Topic archived. No new replies allowed.