Printing a calendar with month and year in C - c

I'm trying to make a program that will print out the month from the given year and month that the user inputs. What I have so far just ends up printing out the entire calendar year.
#include<stdio.h>
int determineleapyear(int year);
int days_in_month[]= {0,31,28,31,30,31,30,31,31,30,31,30,31};
char *months[]=
{
" ",
"\n\n\nJanuary",
"\n\n\nFebruary",
"\n\n\nMarch",
"\n\n\nApril",
"\n\n\nMay",
"\n\n\nJune",
"\n\n\nJuly",
"\n\n\nAugust",
"\n\n\nSeptember",
"\n\n\nOctober",
"\n\n\nNovember",
"\n\n\nDecember"
};
int determinedaycode(int year)
{
int d1, d2, d3;
d1 = year/4.0;
d2 = year/100.;
d3 = year/400.;
return 1 + (year + d1 + d2 + d3 + 1) % 7;
}
int determineleapyear(int year)
{
return (year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0));
}
void calendar(int year, int month, int daycode)
{
int day;
printf("%s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
//Correct the position for the first date
for (day = 1; day <= 1 + daycode * 5; day++)
{
printf(" ");
}
//Print all the dates for one month
for (day = 1; day <= days_in_month[month]; day++)
{
printf("%2d", day);
//Is day before Sat? Else start next line Sun.
if ((day + daycode) % 7 > 0)
printf(" ");
else
printf("\n ");
}
// Set position for next month
daycode = (daycode + days_in_month[month]) % 7;
if (determineleapyear(year))
days_in_month[2] = 29;
printf("\n %s %d\n", months[month], year);
}
//------------------------------------------
int main() {
int daycode, month, year;
printf("\nEnter the month: ");
scanf("%d", &month);
if (month < 1 || month > 12)
{
printf("Error: month must be in range 1..12");
return printf("\nEnter the month: ");
scanf("%d", &month);
}
printf("\nEnter the year: ");
scanf("%d", &year);
if (year < 0)
{
printf("Error: year must be > 0");
return scanf("%d", &year);
}
daycode = determinedaycode(year);
calendar(year, month, daycode);
printf("\n");
return 0;
}
Can anyone help me out with making it print just one month?

Your calendar function is running a loop from 1 to 12, which is causing all months to be printed. Remove the loop and replace it with a parameter to allow the user to select a month:
void calendar(int year, int month, int daycode)
{
int day;
printf("%s", months[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
//Correct the position for the first date
for (day = 1; day <= 1 + daycode * 5; day++)
{
printf(" ");
}
//Print all the dates for one month
for (day = 1; day <= days_in_month[month]; day++)
{
printf("%2d", day);
//Is day before Sat? Else start next line Sun.
if ((day + daycode) % 7 > 0)
printf(" ");
else
printf("\n ");
}
// Set position for next month
daycode = (daycode + days_in_month[month]) % 7;
}
Call it in main using the variable read from input:
calendar(year, month, daycode);
I would also highly recommend validating input. Something like the following:
printf("\nEnter the month: ");
scanf("%d", &month);
if (month < 1 || month > 12)
{
printf("Invalid month selected");
return -1;
}

Related

for loop repeats until value is less than three

I dont know why my for loop is not working. I have been trying for so long. It just runs for the do / while and gives return. Everything in the do / while loop. I have declared a macro outside the main function and I am using it as loop condition. I can't add more details, all I have is code.
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MIN_YEAR 2012
#define MAX_YEAR 2022
#define LOG_DAYS 3
int main(void)
{
const int JAN = 1;
const int DEC = 12;
int year = 0;
int month = 0;
char day1 = 01;
char day2 = 02;
char day3 = 03;
float ratingMorning = 0;
float ratingEvening = 0;
printf("General Well-being Log\n"
"======================\n");
do
{
printf("Set the year and month for the well-being log (YYYY MM): ");
scanf(" %d %d", &year, &month);
if ((MIN_YEAR > year || year > MAX_YEAR))
{
printf(" ERROR: The year must be between 2012 and 2022 inclusive\n");
}
if ((JAN > month || month > DEC))
{
printf(" ERROR: Jan.(1) - Dec.(12)\n");
}
if ((JAN <= month && month <= DEC) && (MIN_YEAR <= year && year <= MAX_YEAR))
{
printf("\n*** Log date set! ***\n\n");
break;
}
} while ((MIN_YEAR <= year || year <= MAX_YEAR) && (JAN <= month || month <= DEC));
for (int i = 0; i < LOG_DAYS; i++)
{
printf(" %d", year);
printf("-");
printf(" %d", month);
printf("-");
printf(" %d",day1);
printf(" Morning rating (0.0-5.0): ");
scanf(" %d", &ratingMorning);
if (ratingMorning < 0 || ratingMorning > 5)
{
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
}
printf(" Evening rating (0.0-5.0): ");
scanf(" %d", &ratingEvening);
if (ratingEvening < 0 || ratingEvening > 5)
{
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
}
}
return 0;
}
The test in the while part of the do / while loop is incorrect. It is much simpler to use a for(;;) loop and break when the input is correct, avoiding redundant and sometime inconsistent tests.
The scanf() conversion specifier for float is %f, not %d.
Here is a modified version:
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#define MIN_YEAR 2012
#define MAX_YEAR 2022
#define LOG_DAYS 3
int main(void) {
const int JAN = 1;
const int DEC = 12;
int year = 0;
int month = 0;
char day1 = 1;
char day2 = 2;
char day3 = 3;
float ratingMorning = 0;
float ratingEvening = 0;
printf("General Well-being Log\n"
"======================\n");
for (;;) {
printf("Set the year and month for the well-being log (YYYY MM): ");
if (scanf(" %d %d", &year, &month) != 2) {
printf("invalid input\n");
return 1;
}
if (year < MIN_YEAR || year > MAX_YEAR) {
printf(" ERROR: The year must be between 2012 and 2022 inclusive\n");
} else
if (month < JAN || month > DEC)) {
printf(" ERROR: Jan.(1) - Dec.(12)\n");
} else {
printf("\n*** Log date set! ***\n\n");
break;
}
}
for (int i = 0; i < LOG_DAYS; i++) {
printf(" %d- %d- %d", year, month, day1);
printf(" Morning rating (0.0-5.0): ");
if (scanf("%f", &ratingMorning) != 1) {
printf("invalid input\n");
return 1;
}
if (ratingMorning < 0 || ratingMorning > 5) {
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
continue;
}
printf(" Evening rating (0.0-5.0): ");
if (scanf("%f", &ratingEvening) != 1) {
printf("invalid input\n");
return 1;
}
if (ratingEvening < 0 || ratingEvening > 5) {
printf(" ERROR: Rating must be between 0.0 and 5.0 inclusive!");
continue;
}
// ... handle the ratings and report success
}
return 0;
}
If the problem is that:
if (ratingMorning < 0 || ratingMorning > 5) is not working
i.e. the error line ERROR: Rating must be between 0.0 and 5.0 inclusive!" is never printed, then when you change the %d to %f in the scanf(" %d", &ratingMorning); lines, the errors are printed because now ratingMorning and ratingEvening contain valid values, whereas when they were input as integers, the values were invalid.

Why is my leap year code not working? (Specially int day)

Alright so this is a code from Jupyterlab. And i wanted to know why the int day wont respond to the changes made to the value? The code conering starts from line 37 at //day (January).
#include <stdio.h>
int main() {
int year = 2021;
int month = 1;
int day = 32;
//printf("Enter an integer: ");
//scanf("%d", &year);
// true if number is less than 0
//Year
if (year > 10000 || year < 1) {
printf("Error: Invalid year, ");
}else{
if(year <= 10000 || year >= 1){
printf("%d, ", year);
}
}
//Month
if (month > 12 || month < 1) {
printf("Error: Invalid month\n");
}else{
if(month <= 12 || month >= 1){
printf("%d\n", month);
}
}
//day (January)
if ((month == 1) && (day > 1 || day < 31)) {
printf("%d\n", day);
}else{
if ((month == 1) && (day < 1 || day > 31)) {
printf("Error: Invalid day\n");
}
}
//Febuary
if ((year%400 == 0) && (month == 2) && (day > 1 || day < 29)) {
printf("%d\n", day);
}
if ((year%400 == 0) && (month == 2) && (day < 1 || day > 29)) {
printf("Error: Invalid day\n");
}else{
if ((month == 2) && (day > 1 || day < 28)) {
printf("%d\n", day);
}
if ((month == 2) && (day < 1 || day > 28)) {
printf("Error: Invalid day\n");
}
}
}
Why is it that from //day (January) the code won't respond to the changes made to the int day? When i enter the date int = 32; it just prints out 32 and not the Error: Invalid day? Why is that and what did i do wrong? [PS. i only have for now January and February...]
Because 32 is bigger than 1. You go right into the if statement on day > 1. You need (day >= 1 && day <= 31)

C Program ERROR 2440/2447 in my code works on online commpiler but not in visual studios 2019

my program works on all online compilers but not on visual studios. However whenever I try to run it on visual studios 2019, it doesn't run and gives me 2 errors. error2440 and error2447. I have tried to fix it but nothing seems to work if anyone has any idea on what to do it would be appreciated the code:
EDIT: after putting const in front of char I did not get any errors but I did get multiple warnings. the warnings are:
warning C4101: 'year': unreferenced local variable
warning C4101: 't': unreferenced local variable
warning C4101: 'r': unreferenced local variable
warning C4101: 'weekdays': unreferenced local variable
warning C4101: 'leapyear': unreferenced local variable
error C4700: uninitialized local variable 'year' used
error C4700: uninitialized local variable 'firstday' used
error C4700: uninitialized local variable 'p' used
error C4716: 'printday': must return a value
error C4700: uninitialized local variable 'year' used
#include<stdio.h>
#include<string.h>
int month_days[] = { 0,31,28,31,30,31,30,31,31,30,31,30,31 };
char* month_name[] =
{
" ",
"\n\n\n\t\t1",
"\n\n\n\t\t2",
"\n\n\n\t\t3",
"\n\n\n\t\t4",
"\n\n\n\t\t5",
"\n\n\n\t\t6",
"\n\n\n\t\t7",
"\n\n\n\t\t8",
"\n\n\n\t\t9",
"\n\n\n\t\t10",
"\n\n\n\t\t11",
"\n\n\n\t\t12"
};
int get_first_start_day(int year)
{
int start_day;
int x, y, z;
x = (year - 1) / 4;
y = (year - 1) / 100;
z = (year - 1) / 400;
start_day = (year + x - y + z) % 7;
return start_day;
}
void set_feb_days(int year)
{
if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
month_days[2] = 29;
else
month_days[2] = 28;
}
void display_month_calendar(int days, int start_day)
{
int month, year;
for (month = 1; month <= 12; month++)
{
printf("%s", month_name[month]);
printf("\n\nSun Mon Tue Wed Thu Fri Sat\n");
for (days = 1; days <= 1 + start_day * 5; days++)
{
printf(" ");
}
for (days = 1; days <= month_days[month]; days++)
{
printf("%2d", days);
if ((days + start_day) % 7 > 0)
printf(" ");
else
printf("\n ");
}
start_day = (start_day + month_days[month]) % 7;
}
}
int printday(void)
{
int month[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
char week[7][10];
int date, mon, year, i, t, r, s = 0;
strcpy(week[0], "Sunday");
strcpy(week[1], "Monday");
strcpy(week[2], "Tuesday");
strcpy(week[3], "Wednesday");
strcpy(week[4], "Thursday");
strcpy(week[5], "Friday");
strcpy(week[6], "Saturday");
printf("Please Enter the date and month: ");
scanf("%d%d", &date, &mon);
if ((year % 400 == 0) || ((year % 4 == 0) && (year % 100 != 0)))
month[1] = 29;
for (i = 0; i < mon - 1; i++)
s = s + month[i];
s = s + (date + year + (year / 4) - 2);
s = s % 7;
printf("\n%d/%d is a %s\n", date, mon, week[s]);
}
void occurrenceDays(int p, char firstday)
{
int year;
int monthdays[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 };
firstday = get_first_start_day(year);
char days[7][10];
strcpy(days[0], "Sunday");
strcpy(days[1], "Monday");
strcpy(days[2], "Tuesday");
strcpy(days[3], "Wednesday");
strcpy(days[4], "Thursday");
strcpy(days[5], "Friday");
strcpy(days[6], "Saturday");
int count[7];
for (int i = 0; i < 7; i++)
count[i] = 4;
int pos;
for (int i = 0; i < 7; i++)
{
if (firstday == days[i][10])
{
pos = i;
break;
}
}
int inc = monthdays[p] - 28;
for (int i = pos; i < pos + inc; i++)
{
if (i > 6)
count[i % 7] = 5;
else
count[i] = 5;
}
int weekdays = monthdays[p] - count[0] - count[6];
printf("Their are %d sundays,%d Saturdays and %d weekdys in the month\n", count[0], count[6],
weekdays);
}
int main(void)
{
int year, start_day, leapyear, t, p, firstday, weekdays;
int month;
printf("Please Enter a Four Digit Year : ");
scanf("%d", &year);
start_day = get_first_start_day(year);
set_feb_days(year);
display_month_calendar(year, start_day);
printf("\n");
printf("Please Enter the Starting Day of the year (1=Sun, 7=Sat): 6 ");
printf("Please enter the month : ");
scanf("%d", &month);
occurrenceDays(p, firstday);
printday();
printf("Do you want to try it again?(1 to continue/0 to quit) :");
scanf("%d", &t);
while (t >= 0)
{
if (t == 1)
{
printday();
printf("Do you want to try it again?(1 to continue/0 to quit) :");
scanf("%d", &t);
}
else
{
}
break;
}
return 0;
}
however my error is here:
{
" ",
"\n\n\n\t\t1",
"\n\n\n\t\t2",
"\n\n\n\t\t3",
"\n\n\n\t\t4",
"\n\n\n\t\t5",
"\n\n\n\t\t6",
"\n\n\n\t\t7",
"\n\n\n\t\t8",
"\n\n\n\t\t9",
"\n\n\n\t\t10",
"\n\n\n\t\t11",
"\n\n\n\t\t12"
};
thank you for any help in advance

Using a file to store a calendar

I am somewhat new to C. I'm making a calendar for a full year and storing it into a file. My main problem is that whenever I try and make a file it comes out blank. I've been trying to figure this out for a while and decided to finally ask. Can someone please show me what I am doing wrong? Thanks in advance!
#include <stdio.h>
#include <stdbool.h>
#define MIN_YEAR 2000
#define MAX_YEAR 3000
#define JANUARY 1
#define FEBRUARY 2
#define MARCH 3
#define APRIL 4
#define MAY 5
#define JUNE 6
#define JULY 7
#define AUGUST 8
#define SEPTEMBER 9
#define OCTOBER 10
#define NOVEMBER 11
#define DECEMBER 12
bool getContinueChoice();
int getYear ();
int getStartingDay (int year);
bool isLeapYear (int year);
void makeFile (int year, int startingDay);
void makeMonthHeading (int month, int year, FILE *file);
int getNumberOfDays (int month, int year);
int main()
{
int startingDay,
year;
printInstructions();
while(getContinueChoice() == true)
{
year = getYear();
startingDay = getStartingDay(year);
makeFile(year, startingDay);
printf("\nCalendar made for the year %d.\n", year);
printf("Look for the file called file.txt to see it.\n");
}
printGoodbye();
return 0;
}
bool getContinueChoice()
{
char getChoice;
bool choice;
scanf(" %c", &getChoice);
while(getChoice != 'y' || 'n')
{
printf("\n***************************************\n");
printf("* Error: That was not a (y/n) answer. *\n");
printf("***************************************\n\n");
printf("Would you like to print a calendar? (y/n): ");
scanf(" %c", &getChoice);
}
if(getChoice == 'y')
{
choice = true;
}
else
{
choice = false;
}
return choice;
}
int getYear()
{
int userYear;
printf("\nPlease enter a year.\n");
scanf("%d", &userYear);
return userYear;
}
int getStartingDay(int year)
{
int firstDay;
firstDay = (((year - 1) * 365) + ((year - 1) / 4) - ((year - 1) / 100) +
((year - 1) / 400) + 1) % 7;
return firstDay;
}
bool isLeapYear(int year)
{
bool leapYear;
if((!(year % 4) && (year % 100)) || !(year % 400))
{
leapYear = true;
}
return leapYear;
}
void makeFile(int year, int startingDay)
{
int daysInMonth;
FILE *file;
file = fopen("file.txt","w");
for(int month = JANUARY; month <= DECEMBER; month++)
{
makeMonthHeading(month, year, file);
fprintf(file, "SUN MON TUE WED THU FRI SAT\n");
for(int day = 1; day <= 1 + startingDay * 5; day++)
{
printf(" ");
daysInMonth = getNumberOfDays(month, year);
for(int days = 1; days <= daysInMonth; days++)
{
fprintf(file, "%2d", days);
if((days + startingDay) % 7 > 0)
{
printf(" ");
}
else
{
printf("\n");
}
}
}
}
printf("Calendar made for the year %d", year);
printf("\nLook for the file called file.txt to see it.\n\n");
getContinueChoice();
return;
}
void makeMonthHeading(int month, int year, FILE *file)
{
if(month == JANUARY)
{
fprintf(file, "JANUARY %d\n", year);
}
else if(month == FEBRUARY)
{
fprintf(file, "\nFEBRUARY %d\n", year);
}
else if(month == MARCH)
{
fprintf(file, "\nMARCH %d\n", year);
}
else if(month == APRIL)
{
fprintf(file, "\nAPRIL %d\n", year);
}
else if(month == MAY)
{
fprintf(file, "\nMAY %d\n", year);
}
else if(month == JUNE)
{
fprintf(file, "\nJUNE %d\n", year);
}
else if(month == JULY)
{
fprintf(file, "\nJULY %d\n", year);
}
else if(month == AUGUST)
{
fprintf(file, "\nAUGUST %d\n", year);
}
else if(month == SEPTEMBER)
{
fprintf(file, "\nSEPTEMBER %d\n", year);
}
else if(month == OCTOBER)
{
fprintf(file, "\nOCTOBER %d\n", year);
}
else if(month == NOVEMBER)
{
fprintf(file, "\nNOVEMBER %d\n", year);
}
else if(month == DECEMBER)
{
fprintf(file, "\nDECEMBER %d\n", year);
}
return;
}
int getNumberOfDays(int month, int year)
{
int monthDays;
if(month == JANUARY)
{
monthDays = 31;
}
else if(month == FEBRUARY)
{
if(isLeapYear(year) == true)
{
monthDays = 29;
}
else
{
monthDays = 28;
}
}
else if(month == MARCH)
{
monthDays = 31;
}
else if(month == APRIL)
{
monthDays = 30;
}
else if(month == MAY)
{
monthDays = 31;
}
else if(month == JUNE)
{
monthDays = 30;
}
else if(month == JULY)
{
monthDays = 31;
}
else if(month == AUGUST)
{
monthDays = 31;
}
else if(month == SEPTEMBER)
{
monthDays = 30;
}
else if(month == OCTOBER)
{
monthDays = 31;
}
else if(month == NOVEMBER)
{
monthDays = 30;
}
else if(month == DECEMBER)
{
monthDays = 31;
}
return monthDays;
}
You have two issues on the same line:
while(getChoice != 'y' || 'n')
This does not do what you think it does. Let's work on an example, where getChoice = 'a'
First operation getChoice != 'y' will evaluate to TRUE because 'a' != 'y'. Remember you are getting TRUE out of this operation.
Now, you are left with TRUE || 'n' which always be TRUE
So, your while condition will always be TRUE and this is why you are stuck in that loop.
If you fix that with
while((getChoice != 'y') || (getChoice != 'n'))
You will realize your second problem with that line, which is your logic.
Imagine if the user enters 'y'. Then what happens?
(getChoice != 'y') will be FALSE, but (getChoice != 'n') will be TRUE, so your while condition will be TRUE and you will be stuck in that loop again.
Think about other scenarios and then you will come to a conclusion that you need to use && instead of ||.
So, change that line to:
while((getChoice != 'y') && (getChoice != 'n'))
Also, as #purec said, you need to fclose() the file once you are done with it.

array loading issue

printf("Please enter the start date of your trip Month/Day/Year seperated by a space:");
scanf("%d %d %d", &month, &day, &year);
checkC = error_date(month,day,year);
if (checkC == 2)
{
travel_month[i][0] == month;
travel_day[i][0] == day;
travel_year[i][0] == year;
}
else
while (checkC==1)
{
printf("Please enter the start date of your trip Month/Day/Year seperated by a space:");
scanf("%d %d %d", &month, &day, &year);
checkC= error_date(month,day,year);
}
for (row = 0; row < trip_num; row++)
{
for (col=0; col < DEST; col++)
printf("Trip#:%d %d/%d/%d\n", row+1, travel_month[row][col], travel_day[row][col], travel_year[row][col]);
}
return 0;
}
int error_date(int month, int day, int year)
{
int checkC;
if ( ((month > 0) && (month <= 12)) && ((day > 0) && (day <= 31)) && ((year> 2000) && (year < 2050)) )
{
checkC = 2;
return checkC;
}
else
{
printf("Invalid date please re-enter date\n");
checkC = 1;
return checkC;
}
}
Am I loading the month/day/year wrong, I keep getting weird integers when I'm printing out the array.
You need to use single equals sign for assignment:
travel_month[i][0] == month;
travel_day[i][0] == day;
travel_year[i][0] == year;
should be
travel_month[i][0] = month;
travel_day[i][0] = day;
travel_year[i][0] = year;

Resources