Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   seg fault (http://www.go4expert.com/forums/seg-fault-t642/)

musicmancanora4 18Mar2006 09:36

seg fault
 
Hey guys iv passed int daysOfMonth[] = {31,28,31,30,31,30,31,31,30,31,30,31}; into the totalDays function but when i run it i get segmentation fault for some reason?

Am i passing the array properly from each function?





Code:


#include "cal.h"

/****************************************************************************
* Function main() is the entry point for the program.
****************************************************************************/
int main(void)
{
  unsigned  month;
  unsigned  year;
  unsigned long days = 0;
  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 28*/
  if(isLeapYear(year))
  {
     
      daysOfMonth[1] = 29;  /* change feb to 29 days*/
     
  }
 
  displayCalendar(month, year, daysOfMonth);
 
  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)void ;*/
  /*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(vvoid alMonth, prompt, result);
       
        }
 
        month
     
       
                           
       
     
  }
  while(flag != 0 );
          */
  /* printf(" value of month is:%d", valMonth) ;    */
 
  return valMonth;

}


/****************************************************************************
* 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 valYear;
}


/****************************************************************************
* 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 avoid lignment). 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, int daysOfMonth[])
{
  /* function called totalDays();*/
  /* total days % 7 finds out how many spaces for each month*/
  /* figure out when to prvoid int a new line*/
 
  int i;
  printf("S  M  Tu W  Th  F  S\n");
 
  totalDays(month, year, daysOfMonth);
 
 
 
}


/****************************************************************************
* Function readRestOfLine() is used for buffer clearing. Source:
* https://inside.cs.rmit.edu.au/~sdb/teaching/C-Prog/CourseDocuments/
* FrequentlyAskedQuestions/
***************************void *************************************************/
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)
{
  /* year = 2006; test for leap year*/
  if((year%400 == 0)|| ( year%4==0 && year % 100 != 0))
  {
      printf("leap year\n");
      return TRUE;
  }
  else
  {
      return FALSE;
  }
  /* return year;*/
}

int totalDays(unsigned month, unsigned year, int daysOfMonth[])
{

 
  int i, md=0, leap=0, track, Y;
  unsigned int d, yrd;
  printf("size of array is%d\n",daysOfMonth[i]);
 
  for(i=Y; i<year; i++)
  {
      if((i%4==0)&&((i%100!=0) || (i%400==0)))
      {
          leap++;
      }
  }
 
  for(i=0; i<(month-1)&&(i<11); i++)
  md=md+daysOfMonth[i];
 
 
  return 0;
}


coderzone 18Mar2006 09:53

Re: seg fault
 
The main cause of the segmentation fault could be you are using a varibale without having been initiliazed or you are deleting a variable which is already deleted.

shabbir 18Mar2006 09:55

Re: seg fault
 
Quote:

Originally Posted by coderzone
The main cause of the segmentation fault could be you are using a varibale without having been initiliazed or you are deleting a variable which is already deleted.

One other reason could be accessing non existent array element.

musicmancanora4 18Mar2006 09:57

Re: seg fault
 
but i have intialized the array in my main

shabbir 18Mar2006 11:31

Re: seg fault
 
Quote:

Originally Posted by musicmancanora4
but i have intialized the array in my main

But you may be accessing the index of an array which may not exist.


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