Public Group

# coin flip problem

This topic is 2166 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Not either 1 or 2 would be !(choice == 1 || choice ==2).

##### Share on other sites

Thanks very much for the quick reply, a quick follow up question. I have been struggling with while loops for the last few days, the above script is my current attempt to loop the script if the user input is not one of the acceptable values, from what i have read it would seem that a do-while loop would apply but i should be able to do it with a while statement?

Also, Mr Stx, I would highly suggest tackling one error at a time and not adding code when you know you have an error. This practice should make it easier to fix things as you find them. Above, you have changed the main line to loop, but you also still have the problem of treating randRange() as a variable. Now, this error has nothing to do with your infinite loop--the replies you've gotten on that should be helpful--but that just happens to be the case in this situation. If you add too much all at one time, it can become much more difficult (as a beginner) to figure out where the problem is really coming from. So fix the randRange() thing, then straighten out your boolean algebra(woot!), and then move on.

While I was typing this I see you have another post. I put in a link above, I don't know how helpful it will be.

##### Share on other sites

Not either 1 or 2 would be !(choice == 1 || choice ==2).

Take a closer look at how you wrote your code, and answer each question for yourself in each situation.

while ( choice != 1 || choice != 2)

input     test 1   --   test 2   --   summary

1           F               T              (F || T) = T

2           T               F              (T || F) = T

3           T               T              (T || T) = T

Step through SiCrane's code and see what you get in each of these cases.

##### Share on other sites

Thanks guys ill spend a few hours looking into the information you have kindly supplied me and if I'm still stuck ill post again.

##### Share on other sites

ok I have done a good bit of research and have the information provided in this thread great starting points, I have come across a hurdle regarding the "!(choice == 1 || choice ==2)" statement, I understand the concept of it but the compiler doesn't allow me to enter this as the "!" isn't in brackets.

##### Share on other sites
You would wrap the whole thing in parenthesis. I.e.:
if (!(choice == 1 || choice == 2))


##### Share on other sites

if(!(choice == 1 || choice ==2))

should work.

Are you doing

if !(choice == 1 || choice ==2)

that won't work, "if" needs brackets straight away.

##### Share on other sites

while (!( choice == 1 || choice == 2))

still gives me the infinite loop on an invalid input

##### Share on other sites

Could you post the current state of your code?

##### Share on other sites

while (!( choice == 1 || choice == 2))

still gives me the infinite loop on an invalid input

You mean on things that aren't integers? That's when you go back up to Washu's post and read about fail() and clear(). Short version: if a stream contains something that it wasn't expecting, like a letter when you're trying to extract an int, the stream sets the fail bit and causes all subsequent reads to fail until you handle the error. Personally, rather than read integers directly from the stream, I prefer to read lines into a string with std::getline() and dump those strings into a stringstream and extract from that.

• 13
• 22
• 23
• 15
• 10