Thread: validation in do while in validateMonth()

  1. #1
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252

    validation in do while in validateMonth()

    Hey guys my while condition isnt working working properly in validateMonth what happens it just stays in there. If i type in 5 or any number between 0-12 it will still say its the wrong input. Why isnt it testing the condition properly?


    Code:
    unsigned getMonth()
    {
       
       /*** declare variables*/
      
      unsigned  tmpMonth;
      int  valid = 0;
      char *prompt = "Please enter a month between 0 - 12 !\n";
      char *month;
      unsigned valMonth;
      
    
      
      month = getUserInput(prompt);
      tmpMonth = validateMonth(valMonth, prompt);
      
    
       
       return EXIT_SUCCESS;
    
    }


    Code:
    unsigned validateMonth(unsigned month, char *prompt)
    {
         unsigned m;
         int flag = FALSE; /* false value*/
       do
       {
         while(flag == TRUE || month<0 || month>12) /* flag 1 for true*/
         {
    	flag = FALSE;
            printf("Month error 0 or less or equal to 12 please\n");
    	getUserInput(prompt);
            
         }
       }while(flag !=FALSE);
         
         return m;
    }

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Code:
    /*** declare variables*/
      
      unsigned  tmpMonth;
      int  valid = 0;
      char *prompt = "Please enter a month between 0 - 12 !\n";
      char *month;
      unsigned valMonth;
    Technically, you are defining the variables. If you had gone something like
    Code:
    extern int valid;
    Then you would be declaring the variable.
    Last edited by cdalten; 03-14-2006 at 10:50 PM.

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    char *month = getUserInput(prompt);
    Unless getUserInput malloc()s the user's input, you'll want to do something else, like use fgets().

    Code:
    unsigned validateMonth(unsigned month, char *prompt)
    {
         unsigned m;
         int flag = FALSE; /* false value*/
       do
       {
         while(flag == TRUE || month<0 || month>12) /* flag 1 for true*/
         {
    	flag = FALSE;
            printf("Month error 0 or less or equal to 12 please\n");
    	getUserInput(prompt);
            
         }
       }while(flag !=FALSE);
         
         return m;
    }
    flag is never TRUE, m is uninitialized (even though you return it), and getUserInput()'s input isn't saved.

    Turn on compiler warnings.
    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. c++ validation
    By rahulsk1947 in forum C++ Programming
    Replies: 4
    Last Post: 06-08-2009, 01:53 PM
  2. set validation
    By jmarsh56 in forum C++ Programming
    Replies: 4
    Last Post: 12-13-2005, 08:49 AM
  3. DATE <TIME.H> data validation
    By bazzano in forum C Programming
    Replies: 4
    Last Post: 08-07-2005, 01:10 AM
  4. validation routine help plz
    By rajesh23 in forum C Programming
    Replies: 8
    Last Post: 09-23-2003, 07:21 AM
  5. [C#] Validation class for events?
    By gicio in forum C# Programming
    Replies: 4
    Last Post: 01-03-2003, 12:19 PM