Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 3 of 3
  1. #1
    New Coder
    Join Date
    Feb 2005
    Posts
    18
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Limiting variable between 2 number

    Dear all,

    My program have 4 directional switches with the value shown below:

    up = -2
    right = -3
    down = -5
    left = -9

    I'll to write something like........

    switch = read_switch();
    count = 1;

    if(switch==-3)
    {
    count++;
    }
    if(switch==-9)
    {
    count--;
    }

    switch(count);
    {
    case '1':
    printf("........");
    break;

    case '2':
    printf("........");
    break;

    case '3'
    printf(".........");
    break;
    }

    Is it possible to limit the count between 1 and 3 (mimimum is 1 and maximum is 3 no matter how many 'left' or 'right' i press), so that the program will display something. Cause if i don't limit 'count', 'count' will also increase/decrease by the amount of time i pressed.

    Value of count does no change when i press 'up' or 'down'.

    Hope you get wat i mean

    Thanks in advance
    west_dweller

  • #2
    New Coder
    Join Date
    Nov 2004
    Location
    Netherlands
    Posts
    87
    Thanks
    0
    Thanked 0 Times in 0 Posts
    It depends on how you want to limit it, if it will go back to zero after hitting 3, then you could use the modulus operator, but if you want it to stay at three when it hits it, then a simple check could be implemented. By the way, I think you'll have to change the name of your variable 'switch', for it interferes with a C keyword.

  • #3
    Regular Coder
    Join Date
    May 2004
    Location
    New Hampshire, America
    Posts
    246
    Thanks
    0
    Thanked 2 Times in 2 Posts
    Code:
    if(switch==-3)
    {
    count++;
    }
    if(switch==-9)
    {
    count--;
    }
    
    if (count <= 1) count++;
    if (count >= 3) count--;
    
    switch(count);
    {
    case '1':
    printf("........");
    break;
    
    case '2':
    printf("........");
    break;
    
    case '3'
    printf(".........");
    break;
    }
    As Dr. Evil said, you are definetly going to have to switch that switch, if you catch my meaning.
    (If I misread the question, my apologies)
    Last edited by Dunna; 02-19-2005 at 01:26 AM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •