Bulls and cows problem, again?

Yes, it's a pretty basic question.. but I can't figure it out.
like this:

vector<int> guess;
for (int g; cin>>g;)
guess.push_back(g);

in terminal it does not know when to stop unless I type other type, like 2e.
I want only 4 numbers in vector guess. After that what should I press to stop?
Please help.
Thanks.

↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑SOLVED↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑
This is based on the Bulls and cows problem, you may have heard of it , if not, here's an example:
It's a two-player number-guessing game called “Bulls and Cows”, to be played between a the computer and a person.1 The computer thinks of a secret number sequence, consisting of four distinct numbers, each in the range {0, 1, . . . , 9}. The human opponent has to determine these four numbers, in their proper sequence, by making repeated guesses. A guess of a number in its proper slot is called a “bull”, whereas a guess in a number that’s not in its proper slot is called a “cow”. For example, if the secret number sequence is 3, 1, 4, 5 and the human opponent guesses 3, 2, 5, 4, then the computer would output:
Bulls: 1, Cows: 2

And I have 2 problems:
1st:
I am trying to make it work like this:
Need help (0/1)? 0
Guess #1? 3 3 2 1
Bulls: 1, cows: 0
Guess #2? 2 3 1 4
Bulls: 0, cows: 1

My code is like this:

#include <bjarne/std_lib_facilities.h>

void offer_help()
{
cout << "The program will generate a secret number sequence, consisting of four distinct numbers, each in the range 0 to 9. You have to determine these four numbers, in their proper sequences by making repeated guesses. A guess of a number in its proper slot is called a 'bull', whereas a guess in a number that is not in its proper slot is called a 'cow'. You can input negative numbers to give up.\n";
}

int main(){
cout << "Need help (0/1)? ";
int help;
cin >> help;
if (help==1){
offer_help();
}
if (help!=0 && help!=1) error("Please enter 0 for no or 1 for yes.\n");
for (;;)
{
vector<int> answer ={1, 2, 3, 4};
vector<int> guess;
int bulls=0;
int cows=0;
int guess_number=1;
cout << "Guess #" <<guess_number<< "? ";
for (int times=0; times<4; times++){
int g;
cin>>g;
guess.push_back(g);}
if (guess[0]<0 || guess[1] <0 || guess[2] <0 || guess[3] <0) error("Negative number detected, byebye.\n");
if (guess[0]>9 || guess[1]>9 || guess[2]>9 || guess[3]>9 || (!cin)) error("Please enter intergers from 0 to 9.\n");
if (guess[0]==guess[1] || guess[0]==guess[2] || guess[0]==guess[3] || guess[1]==guess[2] || guess[1]==guess[3] || guess[2]==guess[3]) error("Please enter four different numbers.\n");
if (guess[0]==answer[0])
bulls++;
else if (guess[0] == answer[1] || answer[2] || answer[3])
cows++;
if (guess[1]==answer[1])
bulls++;
else if (guess[1] == answer[0] || answer[2] || answer[3])
cows++;
if (guess[2]==answer[2])
bulls++;
else if (guess[2] == answer[0] || answer[1] || answer[3])
cows++;
if (guess[3]==answer[3])
bulls++;
else if (guess[3] == answer[0] || answer[1] || answer[2])
cows++;
if (bulls<4){
guess_number++;
cout << "There are " <<bulls<< " bulls, " <<cows<< " cows.\n";
}
else if (bulls==4)
cout << "Congratulations! Let's do it one more time! (Input negative numbers when you want to quit).\n";
}
}

Problem: I have no idea how to make that guess_number++; works, wherever I put this line of code, it's not the same with the answer.

Problem #2:
How can I generate 4 random numbers? I'm now using four fixed numbers :1234, so that I can do the rest of the code first(this exercise is based on Stroustrup's PPP chapter 1-5, which doesnt seem to have ran() or whatever.)
Last edited on
1
2
3
4
5
std::vector<int> guess ;
const std::size_t MAX_GUESSES = 4 ;

int g ;
while( guess.size() < MAX_GUESSES && std::cin >> g ) guess.push_back(g) ;
Just use the for loop 4 times.

1
2
3
4
5
for(int ix = 0; ix < 4; ix++)
{
cin >>  g;
guess.push_back(g);
}
Last edited on
Thanks, have a great night.
Topic archived. No new replies allowed.