Function issues

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 ();
           }
}
Last edited on
it's error-checked, just keeping posted code short
Are you saying that you have the user enter their input twice on purpose, and you left part of the code out? If so, then post everything. We can't possibly know what is wrong by just looking at part of the whole.
Updated the code section to reflect my error-check. Sorry for being vague, wasn't aware that I was.

It's directly under the "Would you like to run the program," and above the true/false if statements in the TestFunction function. (it's the while loop).

I noticed that when I activate the error check (I enter a letter that isn't Y, y, N, or n), and then fix it, the "Would you like to run the program" only gets output once. If I type Y or N, or y or n on the first attempt, that's when it's displayed twice.
Last edited on
Replace all the && with || - with the && means and, it will just keep looking because yesNo cant be all of thos things - with ||, or, it will return true as soon as one of the statements is true
Last edited on
Replace all the && with || - with the && means and, it will just keep looking because yesNo cant be all of thos things - with ||, or, it will return true as soon as one of the statements is true 


I used the &&'s in the error checking for if it isn't any of those 4 choices. If I use ||, then it will tell me my input is invalid, even if I typed one of the 4 letters.

The if statements use ||'s because it can be one or the other (since the user could type Y or y as yes).

Unless I'm missing something, those should be correct in that aspect.
Oh you're right, brain fart :p
Oh you're right, brain fart :p

Heheh :D

On other news, it magically stopped producing the same line twice. I have no idea what happened with that.

The boolean 'request' is still picking up the wrong value though. The function returns the CORRECT value, I've checked it in the int main function with cout lines. For some reason, the function return isn't translating over to the variable 'request' (which is the larger issue anyway)
I can't find anything that would make the program behave in the way you described. Are you sure you're posting everything?
Sent the full code over PM, its a bit long but hopefully you can get through it. Can't post it publicly for multiple reasons ^.^

EDIT: Line 25 has only one = sign. This overwrites the boolean 'request' to true. This is the reason for the boolean never accepting the return value of false, for any future searchers. It needs to be while(request==true) instead.

My IDE (VS2012) never showed it as an error, and can be easily overlooked as a result.

The Duplicate output is a mystery that ended up fixing itself. Thanks to everyone for the help and support!
Last edited on
Topic archived. No new replies allowed.