Parameter Help

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++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

cin >> selection;

srand(time(0));  //seed the random number

for(int i=1; i<=12; i++) //12 iterations starting from 1
{
  randomizer = rand() % 2;
  
  if (randomizer == 0)
  {
	  
	selection = selection + 0.5;
	cout << selection << endl;
  } 
  else
  {
	selection = selection - 0.5; 
 	cout << selection << endl;
  }

}
Not really sure what you're asking, but what type is selection? I'd imagine you just made it an int.
yes it is an INT.

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.
1
2
3
4
5
6
7
8
9
10
11
  if (randomizer == 0)
  {
	  
	selection = selection + 0.5;
  } 
  else
  {
	selection = selection - 0.5; 
  }

  if ( 0 <= selection && selection <= 10 ) cout << selection << endl;
but that doesn't limit it. all it does it print out less numbers and skip the negative numbers or over 10. I want 12 numbers printed.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
for(int i=1; i<=12; ) //12 iterations starting from 1
{
  randomizer = rand() % 2;
  
  if (randomizer == 0)
  {
	  
	selection = selection + 0.5;
  } 
  else
  {
	selection = selection - 0.5; 
  }

  if ( 0 <= selection && selection <= 10 ) 
  {
      cout << selection << endl;
      i++;
  }
}
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.
If number is less than 0 or greater than 10, don't output and decrement i. Should be able to write that into code.
how would you do that? I have tried numerous times but it just keeps messing up.

p.s. pretty dang new to c++
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
for(int i=1; i<=12; i++ ) //12 iterations starting from 1
{
  randomizer = rand() % 2;
  
  if (randomizer == 0)
  {
	  
	selection += ( selection == 10  ) ?  -0.5 : 0.5;
  } 
  else
  {
	selection += ( selection == 0  ) ?  0.5 : -0.5;
  }

  cout << selection << endl;
}
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--;
}
thanks a ton for all the help guys. I got it to work! +1 point for all of you
Topic archived. No new replies allowed.