Range validation in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kreuzen
    New Member
    • Aug 2008
    • 2

    Range validation in C

    Hi everybody,

    I'm trying to write a program that checks what the minimum and maximum number out of all the range numbers given.
    Example: 1-10, 2-20, 3-30
    min = 1
    max = 30
    There are also special cases where the range can be:
    -20 (this is from 0 to 20)
    or
    20- (this is from 20 to the highest number possible, which is INT_MAX).

    If I type in the "20-" range, it only seems to work for the FIRST and LAST set of range I type in.

    Example: 1-10,2-20,3-30

    If I change 1-10 to 1- , it will make max = INT_MAX. Also, If I change 3-30 to 3-, it will make max = INT_MAX. Although, If I try changing 2-20 to 2-, it will just change it to the highest VISIBLE number which is 30 (even though it's suppose to be INT_MAX).


    Here is my code:
    Code:
    sscanf(resultEquals, "%d-%d", &resultMin[index], &resultMax[index]);
    min = resultMin[0];
    max = resultMax[0];
    if(resultMax[index] == 0){
    	resultMax[index] = INT_MAX;
    }
    
    if(resultMin[index] < 0){
    	resultMax[index] = (resultMin[index] * -1);
    	resultMin[index] = 0;
    }
    
    if(resultMin[index] < min){
    	min = resultMin[index];
    }
    
    if(resultMax[index] > max){
    	max = resultMax[index];
    }
    
    index++;
    Any idea as to why this is happening?

    Feedback is appreciated.
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    I think you need to get the input as a single string and tokenize the string using -
    After tokenizing you can check whether both the values valid numbers and set the values.


    Raghu

    Comment

    Working...