Using a file to store a calendar - c

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.

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)

Converting Time how can I improve my code? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 6 years ago.
Improve this question
Just started programming so any suggestion or constructive criticism would be much appreciated. I'm pretty sure there are ways to shorten this I just don't know how, especially the use of functions.
#include <stdio.h>
#include <stdlib.h>
int input, check = 1, ans;
int menu ();
int convert24(int hour, int min, int sec, int meridiem);
int convert12(int hour, int min, int sec, int meridiem);
int result(int hour, int min, int sec, int meridiem);
void processAnother();
int main()
{
do
{
menu();
processAnother();
system("cls");
}while (ans = 'y');
}
int menu ()
{
int hour, min, sec, meridiem;
printf("[1] Convert to 24-hour notation\n");
printf("[2] Convert to 12-hour notation\n");
printf("[3] Quit\n");
scanf("%d", &input);
switch(input)
{
case 1 :
{
printf("Enter hours (1-12):minute(1-59):seconds(1-59): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("[1] AM or [2] PM: ");
scanf(" %d", &meridiem);
return convert24(hour, min, sec, meridiem);
break;
}
case 2 :
{
printf("Enter hours(1-24):minute(1-59):seconds(1-59): ");
scanf("%d %d %d", &hour, &min, &sec);
printf("[1]AM or [2]PM: ");
scanf(" %d", &meridiem);
return convert12(hour, min, sec, meridiem);
break;
}
default :
{
printf("Goodbye :)");
exit(0);
}
}
}
int convert24(int hour, int min, int sec, int meridiem)
{
if ((hour > 12 || hour < 0))
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if ((min >= 60 || min <0))
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if ((sec >= 60 || sec <0))
{
printf("\nError! Invalid Second\n");
check = 0;
}
if (meridiem > 2 || meridiem < 0)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
if (check == 1)
{
if (meridiem == 1)
{
if (hour == 12)
{
hour = 0;
result(hour, min, sec, meridiem);
}
else
{
result(hour, min, sec, meridiem);
}
}
else if (meridiem == 2)
{
if (hour == 12)
{
hour = 12;
result(hour, min, sec, meridiem);
}
else
{
hour+=12;
result(hour, min, sec, meridiem);
}
}
}
processAnother();
}
int convert12(int hour, int min, int sec, int meridiem)
{
if ((hour > 24 || hour < 0))
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if ((min >= 60 || min <0))
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if ((sec >= 60 || sec <0))
{
printf("\nError! Invalid Second\n");
check = 0;
}
if (meridiem > 2 || meridiem < 0)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
if (check == 1)
{
if (meridiem == 1)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
else if (meridiem == 2)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
}
processAnother();
}
int result(int hour, int min, int sec, int meridiem)
{
char *x;
int y;
if(meridiem == 1)
{
x = "AM";
}
else if (meridiem == 2)
{
x = "PM";
}
if (input == 1)
{
y = 24;
}
else if (input == 2)
{
y = 12;
}
printf("The %d-hr Notation is : %d:%d:%d %s\n",y, hour, min, sec, x);
return;
}
void processAnother()
{
printf("Process another [y/n]");
scanf("%c", &ans);
return;
}
Thank you very much.
ans, is used as a char but declared as a int, it will work but it's ugly, also don't abuse global variable, input, check and ans can be declared somewhere else and some of them dont even need to be declared.
On convert12() and convert24() you're doing the same thing on the 20 first line, write a function that return 0 if they're an error and 1 otherwise.
I usualy put the main() function at the bottom of my file it allow me to not prototype every other function on the file
Instead of passing hour, min, sec and meridiem everytime you call most of your functions, create a struct with every element inside and pass a pointer of it.
main() should be declared like this main(void) see here for more information
for this one i'm gona use multiple steps
first:
int convert12(int hour, int min, int sec, int meridiem)
{
if ((hour > 24 || hour < 0))
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if ((min >= 60 || min <0))
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if ((sec >= 60 || sec <0))
{
printf("\nError! Invalid Second\n");
check = 0;
}
if (meridiem > 2 || meridiem < 0)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
if (check == 1)
{
if (meridiem == 1)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
else if (meridiem == 2)
{
if (hour <= 12)
{
result(hour, min, sec, meridiem);
}
else
{
hour -= 12;
result(hour, min, sec, meridiem);
}
}
}
processAnother();
}
can become:
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input))
{
if (time->meridiem == 1)
{
if (time->hour <= 12)
{
}
else
{
time->hour -= 12;
}
}
else if (time->meridiem == 2)
{
if (time->hour <= 12)
{
}
else
{
time->hour -= 12;
}
}
}
result(time, input);
processAnother();
}
I followed my previous advice and moved the call of result at the end
I know it won't work like this but you can now write it like that:
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input))
{
if (time->meridiem == 1 && time->hour > 12)
{
time->hour -= 12;
}
else if (time->meridiem == 2 && time->hour > 12)
{
time->hour -= 12;
}
}
result(time, input);
processAnother();
}
just before we tested if meridiem was between 1 and 2 so we can write this function like this
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input) && time->hour > 12)
{
time->hour -= 12;
}
result(time, input);
processAnother();
}
do the same process for convert24()
If you followed all of my instruction you should have something like this:
#include <stdio.h>
#include <stdlib.h>
// time_t aleardy exist so I used my_time_t instead
typedef struct my_time_s
{
int hour;
int min;
int sec;
int meridiem;
} my_time_t;
void processAnother(void)
{
char ans;
printf("Process another [y/n]");
// note the space at the start
// that means to discard all whitespace
// exit if ans different than 'y'
if (scanf(" %c", &ans) <= 0 || ans != 'y')
exit(0);
}
// don't prototype your fuctions with int if they return nothing
void result(my_time_t *time, int input)
{
// use variable with explicit name
char *meridiem;
int notation;
if(time->meridiem == 1)
{
meridiem = "AM";
}
else if (time->meridiem == 2)
{
meridiem = "PM";
}
// it's the same as old notation if you don't know the << operator search what bitshift is on the internet
// y = 6 << input <==> y = 6 * 2 ^ input
// so if input = 1 : y = 6 * 2 ^ 1 = 12
// if input = 2 : y = 6 * 2 ^ 2 = 24
notation = 6 << input;
printf("The %d-hr Notation is : %d:%d:%d %s\n", notation, time->hour, time->min, time->sec, meridiem);
return;
}
int are_time_correct(my_time_t *time, int input)
{
int check = 1;
// double parenthesis are not needed
// (6 << input) can be equal at 12 or 24 in function of input see above for more informations
if (time->hour > (6 << input) || time->hour < 0)
{
printf("\nError! Invalid Hour\n");
check = 0;
}
if (time->min >= 60 || time->min < 0)
{
printf("\nError! Invalid Minute\n");
check = 0;
}
if (time->sec >= 60 || time->sec < 0)
{
printf("\nError! Invalid Second\n");
check = 0;
}
// i don't think the meridiem can be below 0 so I replace (time->meridiem < 0) by (time->meridiem < 1)
if (time->meridiem > 2 || time->meridiem < 1)
{
printf("\nError! Invalid Meridiem\n");
check = 0;
}
return (check);
}
int convert24(my_time_t *time, int input)
{
if (are_time_correct(time, input) && time->hour == 12)
{
// if time->meridiem == 1 :
// time->hour = 12 * (1 - 1) = 0
// if time->medriem == 2 :
// time->hour = 12 * (2 - 1) = 12
time->hour = 12 * (time->meridiem - 1);
}
result(time, input);
// processAnother(); is not needed you already do it on the main loop
return (0);
}
int convert12(my_time_t *time, int input)
{
if (are_time_correct(time, input) && time->hour > 12)
{
time->hour -= 12;
}
result(time, input);
// processAnother(); is not needed you already do it on the main loop
return (0);
}
int menu(void)
{
int input;
my_time_t time;
// string concatenation
printf("[1] Convert to 24-hour notation\n"
"[2] Convert to 12-hour notation\n"
"[3] Quit\n");
if (scanf(" %d", &input) <= 0)
{
printf("\nWrong input\n");
input = 3;
}
switch(input)
{
case 1 :
{
printf("Enter hours (1-12):minute(1-59):seconds(1-59): ");
if (!scanf(" %d", &time.hour) <= 0 ||
!scanf(" %d", &time.min) <= 0 ||
!scanf(" %d", &time.sec) <= 0)
{
printf("\nWrong input\n");
return (1);
}
printf("[1] AM or [2] PM: ");
if (!scanf(" %d", &time.meridiem) <= 0)
{
printf("\nWrong input\n");
return (1);
}
return convert24(&time, input);
break;
}
case 2 :
{
printf("Enter hours(1-24):minute(1-59):seconds(1-59): ");
if (scanf(" %d", &time.hour) <= 0 ||
scanf(" %d", &time.min) <= 0 ||
scanf(" %d", &time.sec) <= 0)
{
printf("\nWrong input\n");
return (1);
}
printf("[1]AM or [2]PM: ");
if (scanf(" %d", &time.meridiem) <= 0)
{
printf("\nWrong input\n");
return (1);
}
return convert12(&time, input);
break;
}
default :
{
printf("Goodbye :)");
exit(0);
}
}
return (0);
}
// function without parameters are declared with void
int main(void)
{
// a while loop is simpler imo
while (1)
{
// if menu() return 1 it means the user enter a wrong input
// in that case no need to call processAnother()
if (!menu())
{
processAnother();
}
// DON'T USE SYSTEM NEVER EVER SEE HERE FOR MORE INFO https://stackoverflow.com/questions/19913446/why-should-the-system-function-be-avoided-in-c-and-c
// anyway some shell (such as mine) don't reconise `cls` and use clear instead
system("cls");
}
return (0);
}
you also didn't test if scanf returned something, they're some case where you program will segfault

Printing a calendar with month and year in 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;
}

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