Hi, I have a question about my code and the return value. It's rather long but mostly because it's convoluted and/or poorly written.
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
|
bool getResponseToAnotherSimulation()
{
char answer; // holds response
bool boolForAnswer; // holds bool for response
cout << endl << "**********************************************************************" << endl << endl;
cout << "Do you want to do another simulation? Enter Y or N: ";
cin >> answer;
if((answer == 'Y') || (answer == 'y')) { // correct responses
boolForAnswer == true;
}
else if((answer == 'N') || (answer == 'n')) { // correct responses
printClosingBanner();
boolForAnswer == false;
}
else {
while((answer != 'Y') && (answer != 'y') && (answer != 'N') && (answer != 'n')) { // repeat prompt until correct answer given
cout << "Please re-enter your answer as Y or N: ";
cin >> answer;
cout << endl;
}
if((answer == 'Y') || (answer == 'y')) {
boolForAnswer == true;
}
else {
printClosingBanner();
boolForAnswer == false;
}
}
return boolForAnswer;
}
| |
basically, i use this function as my return value in int main to see whether i'm going to run a simulation again. the player can either enter Y y or n N -- if they don't an error message prompts them repeatedly to enter their answer in correct format. my output, however, looks like this
Do you want to do another simulation? Enter Y or N: a
Please re-enter your answer as Y or N: a
Please re-enter your answer as Y or N: a
Please re-enter your answer as Y or N: a
Please re-enter your answer as Y or N: a
Please re-enter your answer as Y or N: a
Please re-enter your answer as Y or N: a
Please re-enter your answer as Y or N: n
Goodbye and Good Luck at Endor!
**********************************************************************
**********************************************************************
Do you want to do another simulation? Enter Y or N:
in other words, after saying 'n' it asks again if i want to run another simulation.
my int main is a do while loop that ends with
1 2 3 4 5 6
|
getResponseToAnotherSimulation();
}
while(getResponseToAnotherSimulation() == true);
return 0;
}
| |
why does it re-run the prompt???!