Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   conditional problem (http://www.go4expert.com/forums/conditional-problem-t639/)

musicmancanora4 15Mar2006 18:54

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


shabbir 15Mar2006 22:08

Re: conditional problem
 
I could not understand which While loop you are talking about and what variable condition always gets into while loop.

coderzone 15Mar2006 22:19

Re: conditional problem
 
Probably he is talking about the while loop discussed in the thread do while prob on validation


All times are GMT +5.5. The time now is 13:56.