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


