RSS Forums RSS
Please support our C advertiser: Programming Forums
Views: 1660 | Replies: 7
Reply
Join Date: Mar 2006
Posts: 28
Reputation: musicmancanora4 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
musicmancanora4 musicmancanora4 is offline Offline
Light Poster

function passing variables

  #1  
Mar 17th, 2006
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?


sorry for posting the whole thing but its got something to do with how im passing variables through the function impretty sure.


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;
}
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: function passing variables

  #2  
Mar 17th, 2006
Check your getYear() and getMonth() functions. They are not returning the values you want. That is why you are always getting 0.

Also the parameters in the calDays = totalDays(month, year); call are reversed.
Reply With Quote  
Join Date: Mar 2006
Posts: 28
Reputation: musicmancanora4 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
musicmancanora4 musicmancanora4 is offline Offline
Light Poster

Re: function passing variables

  #3  
Mar 17th, 2006
ok well id want to return the month variable and the year variable so i can pass it wouldnt i?

but then i get a warning saying integer from pointer without a cast?

but then i tried

return *month and it worked but it returned a bogus value for example i typed in 5 for the month and it would return 50 or 60
Reply With Quote  
Join Date: Mar 2006
Posts: 28
Reputation: musicmancanora4 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
musicmancanora4 musicmancanora4 is offline Offline
Light Poster

Re: function passing variables

  #4  
Mar 17th, 2006
yehhh i changed return_success to return month i get the warning type cast integer one. But if i go return *month i get a wrong value
Reply With Quote  
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,481
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Rep Power: 8
Solved Threads: 102
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: function passing variables

  #5  
Mar 17th, 2006
Originally Posted by musicmancanora4
yehhh i changed return_success to return month i get the warning type cast integer one. But if i go return *month i get a wrong value
try using
return valMonth;
and
return valYear;
.
Reply With Quote  
Join Date: Mar 2006
Posts: 28
Reputation: musicmancanora4 is an unknown quantity at this point 
Rep Power: 3
Solved Threads: 0
musicmancanora4 musicmancanora4 is offline Offline
Light Poster

Re: function passing variables

  #6  
Mar 17th, 2006
i just bet ya too it i returned month n year instead of those values thnks for ur help
Reply With Quote  
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation: andor has a spectacular aura about andor has a spectacular aura about andor has a spectacular aura about 
Rep Power: 6
Solved Threads: 29
andor's Avatar
andor andor is offline Offline
Posting Whiz in Training

Re: function passing variables

  #7  
Mar 17th, 2006
In getMonth() and getYear() dont return EXIT_SUCCESS
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 1:54 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC