musicmancanora4 0 Light Poster

Hey guys im making a calendar program where you enter a month 0 to display all months for that year... 0 - 12.. 1 being janauary etc and then entering the year. Similar to the cal command in unix

sun on tue wed thu fri sat

1 2 3 4 5 etc

display it like that but im not sure if im finding out how the days in the month properly and also where the actual month starts on tuesday or wednesday for example.. Its compiled in unix gcc -ansi -Wall -peantic
it dosnt display anything much... could someone guide me please

int main(void)
{
   unsigned  month;
   unsigned  year;
   unsigned long days = 0;
   int i;
   int *keeptrack;
   int daysOfMonth[MAX_MONTH] ={31,28,31,30,31,30,31,31,30,31,30,31};
   
   /* that prints array
   for(i=0; i<MAX_MONTH; i++)
   {
   printf("days array:%d\n", daysOfMonth[MAX_MONTH]);
   }*/
   
   month = getMonth();
   year  = getYear();
  
  
   if(isLeapYear(year))
   {
      
      daysOfMonth[1] = 29;  /* change feb to 29 days*/
      
   }
   
   displayCalendar(month, year, daysOfMonth, keeptrack);
   
   return EXIT_SUCCESS;
}


/***********************************track,*********  ********************************
* 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");
	   
	   MAX_MONTHfgets(buff, BUFF_SIZE, stdin);
	   *month = atoi(buff); month = getUserInput(prompt, result);
  tmpMonth = validateMonth(vvoid alMonth, prompt, result);
	
	}
   
	month
       
	
	   MAX_MONTH                 
	
       
   }
   while(flag != 0 );
          */
  /* printf(" value of month is:%d", valMonth) ;    */
   
   return valMonth;

}


/**************************************************  **************************
* Function MAX_MONTHgetYear() 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 ar[], int *track)
{
   /* 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;
   int dt;
   int loop;
   int t;
   t=*track;
   
   totalDays(month, year, ar, t);
   
   printf("\n\n\n\n\t\t");
   printf("S  M  Tu W  Th  F  S\n");
   printf("%d", t);
  for(loop=0; loop<t+2; loop++)
  {
      printf("\t");
      for(dt=1; dt<=month; dt++)
  {
      
     
      if(t%7==0 && t!=0)
      {
         printf("\n\n\t\t");
	 printf("%d\t", dt);
	 t++;
      }
  
  }
  
  
  }
   
   
   
   
   
}


/**************************************************  **************************
* Function readRestOfLine() is used for buffer clearing. Source: 
* https://inside.cs.rmit.edu.atrack,u/~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;*/
}

void totalDays(unsigned month, unsigned year, int ar[], int track)
{
   int i;
   int md = 0;
   int leap=0;
   int startYear = 1900;
   
   unsigned d;
   unsigned yrd;  
   
   for(i=startYear; 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+ar[i];
      yrd=(year-startYear)*365;
      d = yrd+leap+md;
      track=d%7;
   }
   
  /*printf("Month day:%d.yearDay1900:%d... 
   day:%d..track%d", md, yrd,d,track);*/


}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of 1.18 million developers, IT pros, digital marketers, and technology enthusiasts learning and sharing knowledge.