Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   function passing not returning proper value (http://www.go4expert.com/forums/function-passing-returning-proper-value-t641/)

musicmancanora4 17Mar2006 18:29

function passing not returning proper value
 
Hi guys im having a slight problem passing the month and year variable to a function that i have created called int totalDays(unsigned year, unsigned month) now when i do a print inside the function it prints 0. Even though i have inputted using a key for example 6 for the month and 2006 for the year.


So pretty much after i have entered the input for month and year it does not carry the value that i have entered into the totalDays function instead its always 0 why is that?




Code:

int main(void)
{
  unsigned  month;
  unsigned  year;
  int i;
  int daysOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31};
 
 
  month = getMonth();
  year  = getYear();
 
  displayCalendar(month, year);
 
  /* function call : if its a leap year february is now 29 days instead of 28*/
  if(isLeapYear(year))
  {
     
      daysOfMonth[1] = 29;  /* change feb to 29 days*/
  }
   
  return EXIT_SUCCESS;
}


/****************************************************************************
* Function getMonth() prompt the user for a number MIN_MONTH to MAX_MONTH and
* returns that number. The number 0 is valid because this indicates that the
* user wants to select all months.
****************************************************************************/
unsigned getMonth()
{
 
  /*** declare variables*/
 
 
  char *prompt = "Please enter a month between 0 - 12 !\n";
  char *month;
  int valMonth;
  int monthValid;
 
  do
  {
  monthValid = TRUE;
  month = getUserInput(prompt);
 
  valMonth = strtod(month,NULL);
  monthValid = validateMonth(valMonth);
  }
  while(!monthValid);
  /*printf("hello %s",month);*/
  /*printf("Please enter a month between 0 - 12\n");*/

 
  /* validation check if month entered is less then 0 > 12*/
 
 
  /*doint arr[] = {1,2,3};
  {
        fgets(buff, BUFF_SIZE, stdin);
       
       
        *month = atoi(buff);
     
        while(flag == 1 || *month <0 || *month >12)
        {
          flag = 0;
         
          printf("Wrong input for month from 0 - 12 only!\n");
          printf("Please enter a month between 0 - 12\n");
         
          fgets(buff, BUFF_SIZE, stdin);
          *month = atoi(buff); month = getUserInput(prompt, result);
  tmpMonth = validateMonth(valMonth, prompt, result);
       
        }
 
        month
     
       
                           
       
     
  }
  while(flag != 0 );
          */
       
 
  return EXIT_SUCCESS;

}


/****************************************************************************
* Function getYear() prompts the user for a number MIN_YEAR to MAX_YEAR and
* returns that number.
****************************************************************************/
unsigned getYear(char *month)
{
  char *prompt = "Please enter the year\n";
  char *year;
  int valYear;
  int yearValid;
 
  do
  {
    year = getUserInput(prompt);
    valYear = strtod(year,NULL);
    yearValid = validateYear(valYear);
   
  }
  while(!yearValid);
 
  return EXIT_SUCCESS;
}


/****************************************************************************
* Function displayCalendar() displays the calendar for the user.
* The function will display the calendar for a whole month.
* If the user supplied a month of "0", then a calendar for a whole year
* is displayed instead each month displayed under the previous one (you
* don't need to try to display months side by side).
* Give attention to getting the output format exactly as shown below
* (including headings and alignment). Here's an example for March 2006:
* --------------------
*      March 2006
*  S  M Tu  W Th  F  S
*          1  2  3  4
*  5  6  7  8  9 10 11
* 12 13 14 15 16 17 18
* 19 20 21 22 23 24 25
* 26 27 28 29 30 31
*
****************************************************************************/
void displayCalendar(unsigned month, unsigned year)
{
  /* function called totalDays();*/
  /* total days % 7 finds out how many spaces for each month*/
  /* figure out when to print a new line*/
  int calDays;
 
  calDays = totalDays(month, year);
 
}


/****************************************************************************
* Function readRestOfLine() is used for buffer clearing. Source:
* https://inside.cs.rmit.edu.au/~sdb/teaching/C-Prog/CourseDocuments/
* FrequentlyAskedQuestions/
****************************************************************************/
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 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;
 
 
}

int validateMonth(unsigned  month)
{
 
    if(month<0 || month>12) /* flag 1 for true*/
    {
       
        printf("Month error 0 or less or equal to 12 please\n");
        return FALSE;
    }
   
    return TRUE;
   
    /*return 1;*/
}

int validateYear(unsigned year)
{
 
  if(year<0 || year>3000)
  {
      printf("between 0 - 3000 only!!\n");
      return FALSE;
  }
 
  return TRUE;
}

int isLeapYear(unsigned year)
{
  if((year%400 == 0)|| ( year%4==0 && year % 100 != 0))
  {
     
      return TRUE;
  }
  else
  {
      return FALSE;
  }
 
}

int totalDays(unsigned year, unsigned month)
{

    printf("year is:%d  month is:%d", year, month);

/******* in here it should print what i have inputted in the month and year but it always gives me 0? why is that?*******/
 
  return EXIT_SUCCESS;
}


shabbir 17Mar2006 19:08

Re: do while prob on validation
 
I have moved this into a new thread.

coderzone 17Mar2006 19:15

Re: function passing not returning proper value
 
Because you are returning the EXIT_SUCCESS and assigning that into the month variable and then printing it.


All times are GMT +5.5. The time now is 16:11.