Hello everyone, hopefully this is a quick fix.
I ask the user a question, and they need to type Y/N. the function detects which they choose, and returns it as true/false (respectively).
When the function is executed, even though the 'cout' command line is typed once, the program runs it twice in a row.
After the user types the response twice (it's error-checked, just keeping posted code short), the function will return the correct response. This is the output:
Would you like to run this program?[Y/N]
N
Would you like to run this program?[Y/N]
N (user types this again) |
Also, in the main function, when a Boolean is set to the value of the return, it is always set to true, regardless of what the function returns. I've tested the function, and
even when the function returns false, I still get
the value of the 'request' boolean as 'true' every time I run the program.
I'm asking to make sure it's not some obscure error on my part; Please, if possible, don't write the code for me, but if you could tell me what's wrong I'd appreciate it. Thanks!
Code:
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 33 34
|
bool TestFunction ()
{
char yesNo = ' ';
bool choice = false;
cout << "Would you like to run this program?[Y/N] ";
cin >> yesNo;
while(yesNo != 'Y' && yesNo != 'y' && yesNo != 'N' && yesNo != 'n')
{
cout << "Invalid input. Please enter your decision [Y/N]: ";
cin >> yesNo;
}
if(yesNo == 'Y' || yesNo == 'y')
choice = true;
else if(yesNo == 'N' || yesNo == 'n')
choice = false;
return choice;
}
int main ()
{
bool request = false;
request = TestFunction ();
while(request = true)
{
//Loop runs
request = TestFunction ();
}
}
|