In one homework project I need to use an object, and its initialization controlled by if condition, so it seems a little complicated, thus I want to make the initialization process as a function. But is it possible for me to initialize this object inside a function and then use it outside?
Here's the code snippet:
void playOneGame(Lexicon& dictionary) {
// TODO: implement
setConsoleClearEnabled(true);
// initialize object: if yes, generate it randomly, else by human input
if (getYesOrNo("Do you want to generate a random board?")) {
// generate boggle randomly by using constructor
Boggle myBoggle(dictionary, "");
} else {
string boardText = getLine("Type the 16 letters to appear on the board:");
//boardText = stripText(boardText);
while (boardText.length() != 16 || containsNonAlpha(boardText)) {
cout << "That is not a valid 16-letter board string. Try again." << endl;
boardText = getLine("Type the 16 letters to appear on the board:");
}
}