I am doing a bulls and cows game . I need for somwhere in my big loop, for it kn
ID: 3756700 • Letter: I
Question
I am doing a bulls and cows game . I need for somwhere in my big loop, for it know that if the size of the final Input does not equal the size of the code to guess size then it needs to ask the user again, meaning starting the loop over until they do equal each other. Else if they do equal each other then, continue the rest of the code assigning the cows and bulls. I am just not sure where to put this if statement and else statement or if I need some sort of else if or do while. Hope to hear any feedback, thank you.
while(!won) {
// their guess and the bulls and cows
// getting their guess input and storing it
vector<int> UserGuess;
int UserInput=0;
cout << "Enter Guess: ";
cin >> UserInput;
int remainder= UserInput;
int index= digits;
// cout << UserInput << endl;
int cows=0;
int bulls=0;
// storing each number from guess which is stored backwards
while(index!= 0) {
remainder = UserInput % 10;
UserInput = UserInput / 10;
UserGuess.push_back(remainder);
index = index - 1;
}
vector<int> finalInput;
// reversing that stored guess
for(int i=UserGuess.size()-1; i >= 0; --i) {
finalInput.push_back(UserGuess.at(i));
}
// if finalinput size doesnt equal code to guess then ask again
if (finalInput.size() != codetoguess.size()){
cout << "You can only use " << codetoguess.size() << endl;
}
else {
// assigning cows and bulls
Explanation / Answer
Hey,
The answer to your question is
It can be done noting the fact that there is a flag initialized as 0 in starting and when both matches make the flag 1 and discontinue the while loop when flag is 1.
int flag=0;
while(flag!=1) {
// their guess and the bulls and cows
// getting their guess input and storing it
vector<int> UserGuess;
int UserInput=0;
cout << "Enter Guess: ";
cin >> UserInput;
int remainder= UserInput;
int index= digits;
// cout << UserInput << endl;
int cows=0;
int bulls=0;
// storing each number from guess which is stored backwards
while(index!= 0) {
remainder = UserInput % 10;
UserInput = UserInput / 10;
UserGuess.push_back(remainder);
index = index - 1;
}
vector<int> finalInput;
// reversing that stored guess
for(int i=UserGuess.size()-1; i >= 0; --i) {
finalInput.push_back(UserGuess.at(i));
}
// if finalinput size doesnt equal code to guess then ask again
if (finalInput.size() != codetoguess.size()){
cout << "You can only use " << codetoguess.size() << endl;
flag=0;
}
else
{
flag=1;
}
}
// assigning cows and bulls
Kindly revert for any queries
Thanks.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.