Infinite Loop when entering invalid input

This is a discussion on Infinite Loop when entering invalid input within the C Programming forums, part of the General Programming Boards category; I'm working on a simple database. The data entry will be a combo of 5 numbers and 2 letters. If ...

  1. #1
    Registered User
    Join Date
    Apr 2006
    Posts
    2

    Infinite Loop when entering invalid input

    I'm working on a simple database. The data entry will be a combo of 5 numbers and 2 letters. If anything else other than exactly 5 numbers and 2 letters are entered an infinite loop is created. How can I filter out the bad input?

    Code:
    #include <stdio.h>
    
    int main()
    {
     	int num;
     	char let1, let2;
     	
     	
     	printf ("ENTER M CARD: ");
     	
     	scanf ("%d%c%c", &num, &let1, &let2);
     	
    
     /**************************************************/	
     if (num == 10105)
     	{if (let1 == 'B' || let1 == 'b' && let2 == 'W' || let2 == 'w')
     	       { printf ("\nM CARD IS 10105BW ");
    		 printf ("\n*****************\n\n\n");
    			 
    			 } else
    			  
    			   printf ("\n INVALID ENTRY\n\n\n");
    		     
     } else   
    /**************************************************/ 			 
     if (num == 10106)
                    {if (let1 == 'Q' || let1 == 'q' && let2 == 'C' || let2 == 'c')
     	       { printf ("\nM CARD IS 10106QC");
    		 printf ("\n*****************\n\n\n");
    			 
    			 } else
    			   
    			   printf ("\n INVALID ENTRY\n\n\n");
    			 
     }	else
     /***********************************************/ 
        
        printf ("\n INVALID ENTRY\n\n\n");
        
    
        
        
        main ();
        
        
    } /******end main */

  2. #2
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,682
    Code:
    /************************************************** */	
     if (num == 10105)
     	{if (let1 == 'B' || let1 == 'b' && let2 == 'W' || let2 == 'w')
     	       { printf ("\nM CARD IS 10105BW ");
    		 printf ("\n*****************\n\n\n");
    			 
    			 } else
    			  
    			   printf ("\n INVALID ENTRY\n\n\n");
    		     
     } else   
    /**************************************************  */ 			 
     if (num == 10106)
    moer over calling main recusively which is not good programming practice. why not use loop instead and u dont have an exit condition to exit from your recursive call of main and return value of main ??

    ssharish2005

  3. #3
    C / C++
    Join Date
    Jan 2006
    Location
    The Netherlands
    Posts
    312
    Code:
    char cstring[];
    
    // ask input, to cstring[];
    
    // check if input is longer than 7 => infinite loop
    
    // else,
    
    for(int i = 7; i < 7; i++)
    {
        if(i < 5)
        {
            if(isdigit(cstring[i]))
            {
                // correct
            }
            else
            {
                while....
            }
        }
        else
        {
            if(isalpha(cstring[i]))
            {
                // correct
            }
            else
            {
                while....
            }
        }
    }
    Operating Systems:
    - Ubuntu 9.04
    - XP

    Compiler: gcc

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,765
    > How can I filter out the bad input?
    Use fgets() to read a line of input
    Use sscanf() to parse that line
    Use the return result of sscanf() to determine if the input parsed correctly.

    > let1 == 'B' || let1 == 'b' && let2 == 'W' || let2 == 'w'
    Look up the C precedence table in your book - this does not do what you think it does.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Apr 2006
    Posts
    2
    Quote Originally Posted by Salem
    >
    > let1 == 'B' || let1 == 'b' && let2 == 'W' || let2 == 'w'
    Look up the C precedence table in your book - this does not do what you think it does.

    How about this?
    ((let1 == 'B' || let1 == 'b') && (let2 == 'W' || let2 == 'w'))

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    That probably does what you expect it to.

    You might want to lok into tolower(), in <ctype.h>, which converts a character to lowercase.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 06-15-2009, 12:24 AM
  2. Input validation loop failure
    By MacNilly in forum C++ Programming
    Replies: 4
    Last Post: 03-01-2006, 03:29 AM
  3. Switch statement = infinite loop
    By Lucid003 in forum C++ Programming
    Replies: 10
    Last Post: 10-10-2005, 01:46 AM
  4. invalid input
    By sworc66 in forum C Programming
    Replies: 8
    Last Post: 11-11-2003, 05:41 AM
  5. infinite loop
    By sballew in forum C Programming
    Replies: 6
    Last Post: 09-22-2001, 12:04 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21