1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

conditional problem

Discussion in 'C' started by musicmancanora4, Mar 15, 2006.

  1. musicmancanora4

    musicmancanora4 New Member

    Joined:
    Mar 9, 2006
    Messages:
    42
    Likes Received:
    0
    Trophy Points:
    0
    Hey guy i really need some help here when i get input i type in a number between 0 and 12 but the dam thing always comes up with the error saying that its the wrong month input..It.

    Im pretty sure there is nothing wrong with my while loop but i am confused as too which variable i should be testing as well?

    It dosnt even test the condition of the variable properly because no matter what you type in it always goes inside the while loop either if the condition is true or false?


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

    Code:
    void readRestOfLine()
    {
       int c;
    
       /* Read until the end of the line or end-of-file. */   
       while ((c = fgetc(stdin)) != '\n' && c != EOF);
    
       /* Clear the error and end-of-file flags. */
       clearerr(stdin);
    }
    
    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;
       
       
    }
    
    unsigned validateMonth(unsigned month, char *prompt, char* result)
    {
         unsigned m = 0;
         /*int flag = FALSE; */ /* false value*/
       
         if(*result<0 || *result>12) /* flag 1 for true*/
         {
    	
            printf("Month error 0 or less or equal to 12 please\n");
    	getUserInput(prompt, result);
            m =1;
         }
       
         
         return m;
    }
    
    
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,287
    Likes Received:
    364
    Trophy Points:
    83
    I could not understand which While loop you are talking about and what variable condition always gets into while loop.
     
  3. coderzone

    coderzone Super Moderator

    Joined:
    Jul 25, 2004
    Messages:
    734
    Likes Received:
    37
    Trophy Points:
    0
    Probably he is talking about the while loop discussed in the thread [thread=638]do while prob on validation[/thread]
     

Share This Page