...

View Full Version : Limiting variable between 2 number



west_dweller79
02-16-2005, 09:23 PM
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

Dr. Evil
02-17-2005, 08:54 AM
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.

Dunna
02-19-2005, 01:23 AM
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)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum