I made this loop, but how do I make it so it doesn't output numbers below 0 or over 10?
It asks for a user to input a number 1-10 earlier and then randoms a 0 or 1. If a 0 then add 0.5, if a 1 then subtract 0.5. Thanks I am just stuck after messing with it for hours. Pretty new to C++
So this program randomizes a zero or a one. If it is a zero you add 0.5 to "selection"(which is just any number 1-10) that the user inputs. If it is a one you subtract 0.5 and you do this 12 times.
ALL I WANT is to make it so you can't go -0.5 or 10.5.
I really appreciate the help vlad from moscow. Only thing with this one is that the results for example were:
0.5
0.5
0
0
0.5
0.5
1
1
0.5
0.5
0
0
The numbers can't repeat themselves back to back because they always need to be adding or subtracting. If a 0 tries to go to -0.5, it needs to go to 0.5 not stay at 0.
Programming is simply turning a real world problem, into instructions a computer can understand. The easiest way to do this is to break up the real world problem into small tasks. Let's see if this help:
If number is less than 0 or greater than 10, don't output and decrement i
Here we can see three tasks that need to happen. We'll start with the condition:
if(number < 0 || number > 10)
This is pretty much a straight translation of the sentence. You can also think ahead and change this condition slightly, I'll leave that to you to figure out if you wish.
Now the second task is really more of not doing something.
1 2 3 4
if(number < 0 || number > 10) //Same as above
{
//...Doing nothing, because we don't want to see this number
}
So now we accomplished two of the tasks. Checking to see if the number is something we don't want, and then not outputting it. This just leaves the third task of decrementing i. Do you see why we need to do this?
1 2 3 4 5
if(number < 0 || number > 10) //Same as above
{
//...Doing nothing, because we don't want to see this number
i--;
}