coin flip problem

Started by
29 comments, last by rip-off 10 years, 7 months ago

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

Advertisement

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.

Here is my technical background info.

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.

Here is my technical background info.

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.

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.

You would wrap the whole thing in parenthesis. I.e.:

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

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

should work.

Are you doing

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

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

"Most people think, great God will come from the sky, take away everything, and make everybody feel high" - Bob Marley

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

still gives me the infinite loop on an invalid input

Could you post the current state of your code?


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.

This topic is closed to new replies.

Advertisement