Thread: conditional

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

    conditional

    Hi guys i input a number between 0-12 but it keeps saying that is wrong .... what is wrong with my validation ? So when i get input and i type in the number between 0-12 i always get

    Month error 0 or less or equal to 12 please can someone help me please







    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;
    }







    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;
    
    }

  2. #2
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    how long r u going to work on the same program? 3YRS?

  3. #3
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    ok where is the getuserinput function;

  4. #4
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    Code:
    char* getUserInput(char *prompt, char*result)
    {
       /*char *result;*/
       char buff[BUFF_SIZE];
       
       printf(prompt);
       
       result = fgets(buff, BUFF_SIZE, stdin);
       
       
        if(result == NULL)
        {
           
            printf("Error please enter the input again!\n");
       
        }
        else if(result[strlen(result)-1] != '\n')
        {
            readRestOfLine();
        }
        
      
        return result;
       
       
    }

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > what is wrong with my validation ?
    You don't know how to program would be my guess - I count at least 6 attempts at this rather trivial calendar program.

    You randomly create functions which take random parameters and return random results.

    How about
    Code:
    int getMonth ( void ) {
      int scanResult, month, valid;
      do {
        char buff[BUFSIZ];
        printf( "Enter a month > " );  /* prompt */
        fflush ( stdout );
        fgets ( buff, BUFSIZ, stdin );  /* read a line */
        scanResult = sscanf( buff, "%d", &month );  /* extract a number */
        /* now determine a) was conversion successful */
        /*               b) was result in range */
        valid = scanResult == 1 && ( month >= 1 && month <= 12 );
      } while ( !valid );
      return month;
    }
    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.

  6. #6
    Musicman - Canora
    Join Date
    Aug 2005
    Location
    Melbourne
    Posts
    252
    iv actually put it in seperate functions to modulate it... one for input one for validation is there a more modulized solution like how i have coded it. Whats wrong with the condition in the while?

  7. #7
    C/C++ homeyg's Avatar
    Join Date
    Nov 2004
    Location
    Louisiana, USA
    Posts
    209
    Quote Originally Posted by bazzano
    iv actually put it in seperate functions to modulate it... one for input one for validation is there a more modulized solution like how i have coded it. Whats wrong with the condition in the while?
    The condition in the while will never be reached because nothing ever sets the flag to true in that function.

    Salem hit the nail on the head; please go read a good book on the C language, and then come back and ask good questions rather than posting, as he said, random functions and lines of code that you pretty much have no friggin' clue what they are going to do, and then complain about them when they don't work because you are a friggin' moron.

    And for further reference, you don't need to break up (what should be at the most) a 40 lined program into 17 functions..

  8. #8
    Registered User
    Join Date
    Feb 2006
    Posts
    155
    Code:
    char* getUserInput(char *prompt, char*result)
    {
       /*char *result;*/
       char buff[BUFF_SIZE];
       
       printf(prompt);
       
       result = fgets(buff, BUFF_SIZE, stdin);
       
       
        if(result == NULL)
        {
           
            printf("Error please enter the input again!\n");
       
        }
        else if(result[strlen(result)-1] != '\n')
        {
            readRestOfLine();
        }
        
      
        return result;
       
       
    }

    whats with readrestofline()?
    and u have to remember that result is a char * which points to a string,its not a number,so u have to use atoi to convert it to a number.



    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;
    }
    this bit of code is the most impressive part,but it will work.



    alrite m was never assigned anything? why have the function return an unsigned? or perhaps u r later going to have a use for a m?




    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;
    
    }
    >tmpMonth = validateMonth(valMonth, prompt);

    valMonth was never assigned anything.

    change the month to a number using atoi and then assign it to valmonth.




    and i suppose u have read a bit too much on MODULARITY.
    Last edited by qqqqxxxx; 03-15-2006 at 11:37 PM.

  9. #9
    Registered User
    Join Date
    Feb 2006
    Posts
    20
    why would you put 2 whiles in a loop anyway if they do the same test?
    Code:
    do
       {
         while(flag == TRUE || month<0 || month>12) /* flag 1 for true*/
         {
    	<-some crap goes here->
         }
       }while(flag !=FALSE);
    (flag !=FALSE) <=> (flag == TRUE)
    if something is NOT FALSE it is bound to be TRUE (maybe in real life you could say the answer is semi false ^^). But even without that , how can you ever enter the loop. Maybe you want to start with flag = TRUE and if something goes wrong set it to false and instead of using a while loop use If then else.If your fctn doesnt validate you might want to run it again until it validates.
    Last edited by Narcose; 03-25-2006 at 09:27 AM.

  10. #10
    Registered User
    Join Date
    Jan 2006
    Location
    Berkeley, Ca
    Posts
    195
    Quote Originally Posted by qqqqxxxx
    how long r u going to work on the same program? 3YRS?
    Didn't it take Larry Ellison (co-founder of Oracle) something like 8 years of learning to program before he got his first programming gig? The point is that it takes some of us longer than others to find the path. Some individuals will take the shortest path of least resistance, some will take the longest path of least resistance, and some will just wander off the path and end up in the women's bra section of Wal-mart.
    Last edited by cdalten; 03-25-2006 at 10:40 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. regarding conditional statement in xml
    By cnu_sree in forum C Programming
    Replies: 5
    Last Post: 07-21-2007, 10:22 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 10:56 AM
  4. Help understanding conditional operator
    By Sereby in forum C Programming
    Replies: 7
    Last Post: 08-09-2004, 12:24 PM
  5. Preproccessor conditional compilation
    By Garfield in forum C Programming
    Replies: 5
    Last Post: 09-28-2001, 09:28 AM