Oct 20, 2011 at 6:58am UTC
I am trying to write a program in which the user plays rock, paper, scrissors with the computer until one reaches 10 points.
Question 1:
How do i get the rand() to output a different number every time it goes through the loop ?
Question 2:
Which is the best loop to use in this case ?
any help would be greatly appreciated.
Thank you in advance.
Oct 20, 2011 at 7:14am UTC
1. I'm pretty sure you'll want to use srand as well as rand but I don't know much more about it.
2. I would use a while loop, have the condition something along the lines of (user_score <=10 && comp_score <=10)
Oct 20, 2011 at 7:41am UTC
1 . Call this
srand((unsigned )time(0));
and then, within your loop
random_integer = rand(); //with your range
simple?
2. I'd go for while as well
Oct 20, 2011 at 7:45am UTC
As a general rule of thumb use a for loop if you know at compile time, exactly how many times it should loop, otherwise use a while loop. I'm sure there are loads of times when you shouldn't follow this advice, but I'm pretty sure it holds up most of the time.