•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C section within the Software Development category of DaniWeb, a massive community of 391,905 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,563 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C advertiser:
Views: 1549 | Replies: 7
![]() |
•
•
Join Date: Mar 2006
Posts: 28
Reputation:
Rep Power: 3
Solved Threads: 0
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.
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;
}
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation:
Rep Power: 6
Solved Threads: 29
•
•
Join Date: Mar 2006
Posts: 28
Reputation:
Rep Power: 3
Solved Threads: 0
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
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
•
•
Join Date: Jun 2005
Location: Tokyo, Japan
Posts: 1,480
Reputation:
Rep Power: 8
Solved Threads: 98
•
•
•
•
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
return valMonth;
return valYear;
•
•
Join Date: Jun 2005
Location: Novi Sad, Serbia
Posts: 273
Reputation:
Rep Power: 6
Solved Threads: 29
•
•
•
•
Originally Posted by musicmancanora4
i just bet ya too it i returned month n year instead of those values thnks for ur help
#include <stdio.h>
#include <string.h>
#include <ctype.h>
//#include "ansidecl.h"
//#include "safe-ctype.h"
#define TRUE 1
#define FALSE 0
#define EXIT_SUCCESS 0
#define BUFF_SIZE 255
/************************************************** **************************
* 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];
unsigned char i;
unsigned char len;
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();
}
len = strlen(result);
for (i=0; i<len-1; i++) //
if (!isdigit(result[i]))
return NULL;
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;*/
}
/************************************************** **************************
* 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 = -1;
do
{
monthValid = TRUE;
month = getUserInput(prompt);
// valMonth = strtod(month,NULL);
if (month)
valMonth = atoi(month);
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 valMonth;
}
int validateYear(unsigned year)
{
if(year<0 || year>3000)
{
printf("between 0 - 3000 only!!\n");
return FALSE;
}
return TRUE;
}
/************************************************** **************************
* Function getYear() prompts the user for a number MIN_YEAR to MAX_YEAR and
* returns that number.
************************************************** **************************/
unsigned getYear()
{
char *prompt = "Please enter the year\n";
char *year;
int valYear;
int yearValid;
do
{
year = getUserInput(prompt);
// valYear = strtod(year,NULL);
if (year)
valYear = atoi(year);
yearValid = validateYear(valYear);
}
while(!yearValid);
return valYear;
}
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;
}
/************************************************** **************************
* 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);
}
int isLeapYear(unsigned year)
{
if((year%400 == 0)|| ( year%4==0 && year % 100 != 0))
{
return TRUE;
}
else
{
return FALSE;
}
}
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(year, month);
/* 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;
}
if(month<0 || month>12) /* flag 1 for true*/
to
if(month<=0 || month>12) /* flag 1 for true*/
or
if(month<0 || month>=12) /* flag 1 for true*/
Not tested fully but I think it may work
![]() |
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
•
•
•
•
DaniWeb C Marketplace
Similar Threads
- Problem passing variables to thread (Perl)
- passing variables threw query (MySQL)
- Passing Variables/Parameters - By Ref/Value? (Computer Science and Software Design)
- Passing variables into form fields (PHP)
Other Threads in the C Forum
- Previous Thread: check number is string
- Next Thread: Please Help Me! This is really important!



Linear Mode