+- Southperry.net (https://www.southperry.net)
+-- Forum: Social (https://www.southperry.net/forumdisplay.php?fid=14)
+--- Forum: Rubik's Cube (https://www.southperry.net/forumdisplay.php?fid=58)
+--- Thread: Problem with code in C :( (/showthread.php?tid=39786)
Problem with code in C :( - Kashimiya - 2011-03-26
//Function prototypes
int dayOfWeek(int doomsday, int leapYear, int month, int day);
int isLeapYear(int year);
int yearToDoomsday(int year);
int daysToNextThursday(int year, int month, int day);
int isValidYear(int year);
//Main function
int main(int argc, char* argv[]){
int dayName = 0;
int day = 0;
int month = 0;
int year = 0;
int doomsday = 0;
int leapYear = 0;
printf("Enter a date after 1582 in dd mm yyyy form: \n");
scanf("%d", &day);
scanf("%d", &month);
scanf("%d", &year);
assert(year >= START_GREGORIAN_CALENDAR);
//Checking for leap year
if(isValidYear(year)) {
leapYear = isLeapYear(year);
doomsday = yearToDoomsday(year);
dayName = dayOfWeek(doomsday, leapYear, month, day);
if (year >= START_GREGORIAN_CALENDAR){
yearIsValid = 1;
if(isLeapYear(year)){
printf("\nThe year %d is a leap year.\n", year);
} else {
printf("\nThe year %d is not a leap year.\n", year);
}
} else {
printf("Year is before 1582.\n");
}
//Function to determine number of days till next Thursday
int daysToNextThursday (int year, int month, int day){
int dayofweek;
int daysToGo;
daysToGo = (7 - dayofweek);
printf("\nThere are %d days until next Thursday.\n", daysToGo);
return daysToGo;
}
Hi intelligent Southperrians!
I'm currently taking a computing course and we've just been learning how to do basic things in C.
I'm supposed to write a program which determines the day of the week given a certain input, after working out whether or not it is a leap year and the doomsday for that year.
I've run into a problem with my code with the last part, it always prints out "There are 7 days until next Thursday" when it shouldn't ._.
Would anyone care to point out my mistake?
Thanks in advance
Problem with code in C :( - SaptaZapta - 2011-03-26
Where does the variable "dayofweek", in function "daysToNextThursday", get its value?
Problem with code in C :( - Kashimiya - 2011-03-26
Ahh, thanks, so the problem would be that the value of "dayofweek" is 0 inside this function because nothing has been done to it?
Would you be able to point me in the right direction for a quick fix to this problem? :S
EDIT: I've tried putting in the dayOfWeek function into the daysToNextThursday function and now it's returning some absurd number >_>
Problem with code in C :( - SaptaZapta - 2011-03-26
Yes, that is the problem.
The best way to fix it would be to change daysToNextThursday so it accepts the dayofweek as its (only) input argument. You compute that in your main program, so just pass it on.
If you are not allowed to change the function interfaces, then you must call the dayofWeek function again, from inside daysToNextThursday, to get that value.
//Function prototypes
int dayOfWeek(int doomsday, int leapYear, int month, int day);
int isLeapYear(int year);
int yearToDoomsday(int year);
int daysToNextThursday(int year, int month, int day);
int isValidYear(int year);
//Main function
int main(int argc, char* argv[]){
int dayName = 0;
int day = 0;
int month = 0;
int year = 0;
int doomsday = 0;
int leapYear = 0;
printf("Enter a date after 1582 in dd mm yyyy form: \n");
scanf("%d", &day);
scanf("%d", &month);
scanf("%d", &year);
assert(year >= START_GREGORIAN_CALENDAR);
//Checking for leap year
if(isValidYear(year)) {
leapYear = isLeapYear(year);
doomsday = yearToDoomsday(year);
dayName = dayOfWeek(doomsday, leapYear, month, day);
if (year >= START_GREGORIAN_CALENDAR){
yearIsValid = 1;
if(isLeapYear(year)){
printf("\nThe year %d is a leap year.\n", year);
} else {
printf("\nThe year %d is not a leap year.\n", year);
}
} else {
printf("Year is before 1582.\n");
}
//Function to determine number of days till next Thursday
int daysToNextThursday (int year, int month, int day){
int dayofweek;
int daysToGo;
daysToGo = (7 - dayofweek);
printf("\nThere are %d days until next Thursday.\n", daysToGo);
return daysToGo;
}
Looking at your code now.
Problem with code in C :( - Kashimiya - 2011-03-27
Ah, cheers, I ended up fixing it by putting in some more variables, it's all good now.
Thanks