Hey all, here's what I got this time. I'm doing a "Bulls and Cows" game, where the computer has a word, and the user has to guess the word. The number of "bulls" are the letters in the correct spot, and "cows" are letters that are in the word, but not in the right spot. I'm storing the bulls and cows in an array, in a struct. I can loop through this struct from my main.cpp no problem.
Now here's the problem. I want to consolidate my code and put the loop in the class. I'm passing the struct through the parameters, and calling the arguments correctly. It's all working, until I return the first char from my array, because "return" is kicking me out of my loop. Should I just put this loop in my main.cpp, in it's own function? Or is there a way to loop through an array and pass the chars so I can print them one at a time? Thanks for the help.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
|
// Prints the characters that match bulls, and cows
char FBullCowGame::PrintMatchingChars(bool BullsOrCowsArray, FBullCowCount BCC) // BCC is my struct
{
if (BullsOrCowsArray) // Pass in true to indicate bulls
{
for (int MatchedChars2 = 0; MatchedChars2 < BCC.Bulls; MatchedChars2++)
{
return BCC.MatchedBulls[MatchedChars2]; // Everything works, but return kicks out of loop
}
}
else // Otherwise indicate cows
{
for (int MatchedChars = 0; MatchedChars < BCC.Cows; MatchedChars++)
{
return BCC.MatchedCows[MatchedChars];
}
}
}
|
The for loops will work exactly like I want when I place them in my main.cpp, but in a class I'm not sure how to pass all chars that are stored in my arrays.
1 2
|
std::cout << BCGame.PrintMatchingChars(1, BullCowCount) << " "; // Loop through the matching Bull chars to display
std::cout << std::endl;
|
The function only couts the first character in the array, and then stops.
1 2 3 4 5 6 7
|
struct FBullCowCount
{
int Bulls = 0;
int Cows = 0;
char MatchedCows[12];
char MatchedBulls[12];
};
|
The arrays. I picked 12 for no real reason, but this is what the struct looks like.
Thanks for taking a look, and please let me know if I need to explain it further.