Jump to content
  • Advertisement
Sign in to follow this  
Mr Stx

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.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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 this post


Link to post
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.

Edited by Paradigm Shifter

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!