Thread: To not accept number unless it is in the right range

  1. #1
    Registered User
    Join Date
    Mar 2016
    Posts
    203

    To not accept number unless it is in the right range

    I'm working on inserting new number into existing string. The code seems to be working fine but it has thrown up a related issue - while trying to make sure that the position in which the new number is to be inserted, I can check only once if the position index is in the correct range but if an incorrect position index is re-entered then the code proceeds with the wrong position number and throws up funny results. How can I keep sending warning messages to the user until the position is entered within correct range? Thanks; the code is as follows:

    Code:
    #include <stdio.h>  
      
    int main()  
    {  
        int arr[100];  
        int i, size, num, position, temp_position;  
      
        /* 
         * Reads size and elements of array 
         */  
        printf("Enter size of the array : ");  
        scanf("%d", &size);  
        printf("Enter elements in array : \n");  
        for(i=0; i<size; i++)  
        {  
            scanf("%d", &arr[i]);  
        }  
      
        /* 
         * Read element to insert and position of the element 
         */  
        printf("Enter element to insert : ");  
        scanf("%d", &num);  
        printf("Enter the element position : ");  
        scanf("%d", &temp_position);  
      
        /* 
         * If the position of element is not valid 
         */  
        if(temp_position>size+1 || temp_position<=0)  
        {  
            printf("Invalid position! Please enter position between 1 to %d: "); 
            scanf("%d", &position); 
        }  
        else  
        
        position = temp_position; 
        {      
            /* 
             * Inserts element in array and increases the size of the array 
             */  
            for(i=size; i>=position; i--)  
            {  
                arr[i] = arr[i-1];  
            }  
            arr[position-1] = num;  
            size++;  
      
            /* 
             * Prints the new array after insert operation 
             */  
            printf("Array elements after insertion : ");  
            for(i=0; i<size; i++)  
            {  
                printf("%d\t", arr[i]);  
            }  
        }  
      
        return 0;  
    }

  2. #2
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Quote Originally Posted by sean_cantab View Post
    I can check only once if the position index is in the correct range but if an incorrect position index is re-entered then the code proceeds with the wrong position number and throws up funny results. How can I keep sending warning messages to the user until the position is entered within correct range?
    You need to use a loop, which will continue as long as the user enters a value that is out of range.

    A "do-while" loop might be a good choice, as this will allow at least one input before checking the loop condition.

    Also, be sure you are compiling with maximum warnings:

    Code:
    main.c|32|warning: too few arguments for format
    Another thing - right after your "else", there is a single line of code followed by a block. The "position = temp_position" is the only thing under the "else", so the block afterwards will always be executed. I don't think this is the behavior you want - you might want to ensure that the block is directly after the "else".

  3. #3
    Registered User
    Join Date
    Mar 2016
    Posts
    203
    Thank you for the suggestion, it worked and also allowed me to get rid of the temp_position variable you mentioned. Here's the updated code:

    Code:
    #include <stdio.h>  
      
    int main()  
    {  
        int arr[100];  
        int i, size, num, position;  
      
        /* 
         * Reads size and elements of array 
         */  
        printf("Enter size of the array : ");  
        scanf("%d", &size);  
        printf("Enter elements in array : \n");  
        for(i=0; i<size; i++)  
        {  
            scanf("%d", &arr[i]);  
        }  
      
        /* 
         * Read element to insert and position of the element 
         */  
        printf("Enter element to insert : ");  
        scanf("%d", &num);  
         
        
        do
        
        { 
    		printf("Enter the element position : ");  
        scanf("%d", &position); 
    		
    	}
    	
    	while 
    	
    	(position <=0 || position > size + 1);
    	
      
        
            for(i=size; i>=position; i--)  
            {  
                arr[i] = arr[i-1];  
            }  
            arr[position-1] = num;  
            size++;  
      
            /* 
             * Prints the new array after insert operation 
             */  
            printf("Array elements after insertion : ");  
            for(i=0; i<size; i++)  
            {  
                printf("%d\t", arr[i]);  
            }  
        }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Methods to keep a number within a certain range?
    By osiris^ in forum C Programming
    Replies: 4
    Last Post: 12-02-2009, 10:30 AM
  2. Random number range
    By tzakieta in forum C Programming
    Replies: 9
    Last Post: 09-02-2008, 01:24 PM
  3. Random number in range generation.
    By hebali in forum C Programming
    Replies: 19
    Last Post: 03-04-2008, 10:46 AM
  4. help with random number range
    By xxwerdxx in forum C Programming
    Replies: 3
    Last Post: 12-06-2005, 08:30 PM
  5. random number in range
    By lgg in forum Linux Programming
    Replies: 3
    Last Post: 08-14-2005, 05:15 AM