Thread: Fix loop

  1. #1
    Registered User
    Join Date
    Sep 2012
    Posts
    7

    Question Fix loop

    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++

    Code:
    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;
      }

  2. #2
    Registered User C_ntua's Avatar
    Join Date
    Jun 2008
    Posts
    1,853
    Code:
    If (selection > 10 || selection < 0) 
        do something;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-26-2011, 07:36 PM
  2. Replies: 23
    Last Post: 04-05-2011, 03:40 PM
  3. for loop ignoring scanf inside loop
    By xIcyx in forum C Programming
    Replies: 2
    Last Post: 04-17-2007, 01:46 AM
  4. loop the loop - feeling foolish
    By estos in forum C Programming
    Replies: 2
    Last Post: 04-07-2007, 02:45 AM
  5. Replies: 3
    Last Post: 03-14-2006, 11:09 AM