Create a User-Friendly Calendar Program: Tips and Tricks for Success

  • Thread starter leroyjenkens
  • Start date
  • Tags
    Program
In summary: Month().In summary, the conversation discusses the process of creating a calendar program that prompts the user for a specific year and displays a calendar for that year. The person seeking help has encountered errors and confusion in their code. The expert provides tips and suggestions for improving the code, including renaming variables and functions and adding semicolons. The expert also explains how to correctly use the printMonth function.
  • #1
leroyjenkens
616
49
Hello, I'm trying to make a calendar that will prompt the user to type in a year, and it will show them a calendar for that year. I have the formulas that I use to find out what day January will start on for that year, and which years are leap years.

Here's what I have so far. I've done a lot wrong. What doesn't look right, or doesn't make sense, or what should I do differently?

Thanks.

#include<stdio.h>

printMonth (int startDay, int days)
int leap (int year)
int year (void)
int dayFormula (int year)
int main (void)
{
int year;
int dayFormula;
int leap;

year = year (void);
dayFormula = dayFormula (year);
leap = leap (year);
printMonth (int startDay, int days);
}
int year (void)
{
int year;
printf("Type in a year: ");
scanf("%d", &year);

return year;
}
int dayFormula (year);
{
int dayFormula;
dayFormula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
return dayFormula;
}
int leap (int year)
{
if (!(year % 4) && (year % 100)) || !(year % 400))
days = 29;
else
days = 28;
}
int printMonth (int startDay, int days)
{

}
 
Technology news on Phys.org
  • #2


Please put [noparse]
Code:
 and
[/noparse] around your code, as I have done below. This preserves your indenting, and makes your code easier to read.

I added comments in your code about changes I would make.
leroyjenkens said:
Hello, I'm trying to make a calendar that will prompt the user to type in a year, and it will show them a calendar for that year. I have the formulas that I use to find out what day January will start on for that year, and which years are leap years.

Here's what I have so far. I've done a lot wrong. What doesn't look right, or doesn't make sense, or what should I do differently?

Thanks.
Code:
#include<stdio.h>

printMonth (int startDay, int days)
int leap (int year)
int year (void)
int dayFormula (int year) 
// The four prototypes above need a semicolon at the end of each statement.

int main (void)
{
    int year; // Not a good idea to have a variable with the same name as a function.
                // It's not an error, but it could cause confusion.
    int dayFormula;
    int leap;

    year = year (void); // You don't need void here; a pair of empty parentheses is all you need.
    dayFormula = dayFormula (year);
    leap = leap (year);
    printMonth (int startDay, int days);
}
int year (void)
{
    int year; // You should give this variable a different name or else rename your function.
                // A better name for the function might be getYear().
    printf("Type in a year: ");
    scanf("%d", &year);

    return year; 
}
int dayFormula (year);
{
    int dayFormula;
    dayFormula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return dayFormula;
   // Again, you should not use the same name for the function as one of its local variables.
}
int leap (int year)
{
    if (!(year % 4) && (year % 100)) || !(year % 400))
        days = 29;
    else
        days = 28;
   // This function should return something, but it isn't. 
   // leap isn't the best choice for a name, either. 
   // What you're calculating is the number of days in February of a given year.
   // You should rename the function so that what it does is clearer.
    
}
int printMonth (int startDay, int days)
{

}
 
Last edited:
  • #3


Ok, I changed the things you said. But it's now saying that before my printMonth thing, it says "expected expression before 'int'". I'm not sure what that means. Plus, what should I put in the printMonth function at the bottom?

Thanks

Code:
#include<stdio.h>

printMonth (int startDay, int days);
int getLeap (int year);
int getYear (void);
int dayFormula (int year);

int main (void)
{
    int year;
    int formula;
    int leap;

    year = getYear ();
    formula = dayFormula (year);
    leap = getLeap (year);
    printMonth (int startDay, int days);
}
int getYear (void)
{
    int year;
    printf("Type in a year: ");
    scanf("%d", &year);

    return year;
}
int dayFormula (year);
{
    int formula;
    formula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return formula;
}
int getLeap (int year)
{
    if (!(year % 4) && (year % 100)) || !(year % 400))
        days = 29;
    else
        days = 28;

}
int printMonth (int startDay, int days)
{

}
 
  • #4


The error you're getting is from not using printMonth() correctly. In the definition for printMonth, this function is advertised as returning an int. The prototype for it omits the return type - it should be there.

Code:
int main (void)
{
    int year;
    int formula;
    int leap;

    year = getYear ();
    formula = dayFormula (year);
    leap = getLeap (year);
    printMonth (int startDay, int days);
}
In your call to printMonth() -- last line, above -- you are using it as if it were a void function (i.e., one that returns void). You are also putting in types for the parameters in the call to printMonth. They shouldn't be there. The call to printMonth should look something like this.
Code:
result = printMonth(startDay, days);

The result (or whatever you want to call it) needs to be declared in main(). The startDay and days variables need to be declared and initialized to appropriate values before you call printMonth().

I've been throwing around some terminology, so to make sure you understand the difference between a function prototype (or declaration), function definition, and function call, I'll give some quick definitions.

Function prototype - The declaration statement in which you list the return type for the function, and the types of all of its parameters. A function prototype typically appears above the definition for main(). If you put the definition for the function above main(), the definition serves as the prototype.
Here is what your prototype should look like. Note that I have included the return type, int.
Code:
int printMonth (int startDay, int days);

Function definition - The block that includes the body of the function - its code. You haven't written the function body yet, so it's empty.
Code:
int printMonth (int startDay, int days)
{

}

Function call - The expression or statement in which you call (use) your function. If a function returns a value, the call often appears on the right side of an assignment statement, but can appear anywhere a value of that type (the type returned by the function) can appear.
Code:
result = printMonth(startDay, days);

Your intent for printMonth() is for it to print a month, but which month? The function would be more useful if you passed the month as a parameter. I think I understand the purpose of startDay (the day of the week that the first of the given month is on), but what is the purpose of days? That would be useful only if the month you're printing happens to be February.

Before I can give any help for printMonth() you need to explain what you want it to do. It's not clear to me that you understand what you want it to do. You have it as returning a value. What should that value represent? What should the function do? Before you write the code you should have an algorithm in mind - the specific steps, in pseudocode, that you want this function to carry out.

You still have some problems with getLeap():
1. day is not declared.
2. the function doesn't return anything.
 
  • #5


Thanks for spending the time to help me with this.
I'm very new to programming. About 4 weeks ago, I didn't even know how programming was done. I knew they typed some stuff somewhere on a computer, but that was the extent of my knowledge.

Code:
#include<stdio.h>

int printMonth (int startDay, int days);
int getLeap (int year);
int getYear (void);
int dayFormula (int year);

int main (void)
{
    int year, formula, leap, result, startDay, days;

    year = getYear ();
    formula = dayFormula (year);
    leap = getLeap (year);
    result = printMonth (startDay, days);
}
int getYear (void)
{
    int year;
    printf("Type in a year: ");
    scanf("%d", &year);

    return year;
}
int dayFormula (year)
{
    int formula;
    formula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return formula;
}
int getLeap (int year)
{
    if (!(year % 4) && (year % 100) || !(year % 400))
        days = 29;
    else
        days = 28;

}
int printMonth (int startDay, int days)
{

}

Here's the fixes. Did I fix them correctly? The error now goes down to line 34 where "days" is undeclared. I'm not sure what to do about that other than put "int days" into the function, but that doesn't seem like it would grant me any progress.

And to answer your questions: printMonth is to print all the months. I think I'm supposed to use it 12 times. At least that's what I understood from the notes I took in class. How do I pass the month as a parameter?

Yes, startDay is the first day of the month. I wrote down some formula that the teacher wrote on the board: (day + 31)%7. That's supposed to get the first day of the month.
Days is how many days the month has. It seems like it's only relevant for February though, like you said.

I know what I want to do, but turning it into a program is hard. The only steps I know of is "prompt user to enter a year" and then "print year". The "print year" step needs to be divided into steps, I guess, since that's the entire program. But I don't know how to divide it, since it does it all at once.

And with getLeap, I'm not sure what to return. Return is what it causes the program to output, right?

Thanks again.
 
  • #6


leroyjenkens said:
Thanks for spending the time to help me with this.
I'm very new to programming. About 4 weeks ago, I didn't even know how programming was done. I knew they typed some stuff somewhere on a computer, but that was the extent of my knowledge.

Code:
#include<stdio.h>

int printMonth (int startDay, int days);
int getLeap (int year);
int getYear (void);
int dayFormula (int year);

int main (void)
{
    int year, formula, leap, result, startDay, days;

    year = getYear ();
    formula = dayFormula (year);
    leap = getLeap (year);
    result = printMonth (startDay, days);
}
int getYear (void)
{
    int year;
    printf("Type in a year: ");
    scanf("%d", &year);

    return year;
}
int dayFormula (year)
{
    int formula;
    formula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return formula;
}
int getLeap (int year)
{
    if (!(year % 4) && (year % 100) || !(year % 400))
        days = 29;
    else
        days = 28;

}
int printMonth (int startDay, int days)
{

}

Here's the fixes. Did I fix them correctly? The error now goes down to line 34 where "days" is undeclared. I'm not sure what to do about that other than put "int days" into the function, but that doesn't seem like it would grant me any progress.
getLeap is still not right because the local variable days is not declared, and you don't return a value. Here's how it should look:
Code:
int getLeap (int year)
{
    int days;
    if (!(year % 4) && (year % 100) || !(year % 400))
        days = 29;
    else
        days = 28;
    return days;
}
leroyjenkens said:
And to answer your questions: printMonth is to print all the months. I think I'm supposed to use it 12 times.
This means that it is supposed to print one month at a time, so its purpose is NOT to print all 12 months.
leroyjenkens said:
At least that's what I understood from the notes I took in class. How do I pass the month as a parameter?
You need to define the function so that it has another parameter, like so:

Code:
int printMonth(int month, int startDay, int days)
{
   // body of function
}
All I've done here is to add another parameter, month. One possible use for the days parameter is that if you happen to be printing the calendar for February, you'll display the right number of days in a leap year. Also I don't know why you have it returning a value. I would be inclined to write it as a void function, one that doesn't return a value.


leroyjenkens said:
Yes, startDay is the first day of the month. I wrote down some formula that the teacher wrote on the board: (day + 31)%7. That's supposed to get the first day of the month.
Days is how many days the month has. It seems like it's only relevant for February though, like you said.
This expression will evaluate to a number in the range 0, 1, 2, ..., 5, 6, so your program could use this idea to determine what day of the week the first of the month falls on. It would have to "know" that 0 corresponds to Sunday, 1 to Monday, and so on, although I have seen some calendars that are arranged Monday, ..., Sunday instead of Sunday, ..., Saturday.

Your printMonth function would also need to know how many days are in each month, and that could be done using an array - have you studied arrays yet?
leroyjenkens said:
I know what I want to do, but turning it into a program is hard. The only steps I know of is "prompt user to enter a year" and then "print year". The "print year" step needs to be divided into steps, I guess, since that's the entire program. But I don't know how to divide it, since it does it all at once.
The steps are calling printMonth 12 times, once for each month.
leroyjenkens said:
And with getLeap, I'm not sure what to return. Return is what it causes the program to output, right?
No, return <something> causes the function to send a value (<something>) back to the caller.

Here's an example of a simple function that returns a number that is twice as large as the number passed to it.
Code:
int doubleValue(int num)
{
   return 2 * num;
}

You would call it this way:
Code:
int value = 8;
int result;

result = doubleValue(value);
After the call to doubleValue(), result will be set to 16.
 
  • #7
Thanks for the response.
I made all the changes you mentioned.
So does this look good so far?
Code:
#include<stdio.h>

int printMonth (int startDay, int days);
int getLeap (int year);
int getYear (void);
int dayFormula (int year);

int main (void)
{
    int year, formula, leap, result, startDay, days;

    year = getYear ();
    formula = dayFormula (year);
    leap = getLeap (year);
    result = printMonth (startDay, days);
}
int getYear (void)
{
    int year;
    printf("Type in a year: ");
    scanf("%d", &year);

    return year;
}
int dayFormula (year)
{
    int formula;
    formula = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return formula;
}
int getLeap (int year)
{
    int days;
    if (!(year % 4) && (year % 100) || !(year % 400))
        days = 29;
    else
        days = 28;
    return days;
}
void printMonth (int month, int startDay, int days)
{

}

At line 15 where it says result = printMonth, it says the void value isn't being ignored like it should be, so I got rid of it.

So now it prints 28 in 1991 and 29 in 1992, which is correct. How do I implement that into the calender?

Your printMonth function would also need to know how many days are in each month, and that could be done using an array - have you studied arrays yet?
We just started on that. Would I do something like this...
a[7] = 31
b[4] = 30
c[??] = ?
For how many months have how many days? I'm not sure what to do with February here.
After the call to doubleValue(), result will be set to 16.
That's confusing. How does the first function know what "value" is?
 
  • #8
leroyjenkens said:
At line 15 where it says result = printMonth, it says the void value isn't being ignored like it should be, so I got rid of it.
If you implement printMonth as a void function, then it is an error to have the call to printMonth on the right side of an assignment statement. A void function does not represent a value; it causes something to happen.

leroyjenkens said:
So now it prints 28 in 1991 and 29 in 1992, which is correct. How do I implement that into the calender?


We just started on that. Would I do something like this...
a[7] = 31
b[4] = 30
c[??] = ?
You wouldn't use three separate arrays; you would use one array with 12 elements, like this:

Code:
int monthDays[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Alternatively, you could have an array with 13 elements, with 0 in the first (index 0) element.
Code:
int monthDays[] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};

Using the 2nd array, April is the 4th month, and monthDays[4] == 30. You wouldn't use monthDays[0].

If the year is a leap year you wouldn't use the value in the array; you would use 29 instead.
leroyjenkens said:
For how many months have how many days? I'm not sure what to do with February here.

That's confusing. How does the first function know what "value" is?
It doesn't. In the call to doubleValue, the actual parameter (value) is evaluated (8) and is passed in. The local variable num is set to 8, and 16 is returned.
 
  • #9
I'm at a point where I don't know what to do. I made all those functions and I don't know how to connect them, I don't know how to use the array, I don't know what else I can add to the program. This is a ridiculous project to expect of us.
 
  • #10
I'm just turning in what I have so far and get, maybe, partial credit. It's glaringly obvious this program is too hard for someone at my level. I'm thoroughly confused about what to do with this program.
 
  • #11
Well I did some research and tried something new. Can someone show me what all is wrong with this?

Code:
#include <stdio.h>
int monthNames (void)
int main (void)
{
    int monthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int monthNames[12]={January,February,March,April,May,June,July,August,September,October,November,December};

}
int getYear (void)
{
    int year;
    printf("Enter a year: ")
    scanf("%d", &year);
    return year;
}
int dayFormula (void)
{
    int day;
    day = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return day;
}
int leapYear (void)
{
    int days;
    if (!(year % 4) && (year % 100) || !(year % 400))
        days = 29
    else
        days = 28
    return days;
}
int printMonth (int month, int startDay, int day)
{
    for (month=0; month<=11; month++)
    {
        printf("%d", monthNames);
        printf("Sun Mon Tue Wed Thu Fri Sat Sun");

        for (startDay=0; startDay<=day; startday++)
            printf("%d", startDay);
    }
}
 
  • #12
leroyjenkens said:
I'm at a point where I don't know what to do. I made all those functions and I don't know how to connect them
Yes, that's really the crux of this problem. It needs to be clear in your mind what needs to happen to display the calendar. To do this, you need to have an algorithm in mind, and from the algorithm you write the code that implements that algorithm.

Start small - print a calendar for the month of January. It should start at the right day of the week for the year whose calendar you are printing. Starting at whatever is the right day of the week, print the dates for the rest of that week, and then print a new line character. After that, print the dates for the next week, and another new line character. Continue for the remaining weeks of January. Use the array to know when the last date of that month is.

Next step - print the calendar for the month of February. You will need to start it at the appropriate day of the week so the the 1st of Feb. comes right after the 31st of January. The only tricky part is knowing whether to print 28 days or 29 days for February.

If you can get the dates right for the first two months, the rest should be pretty easy.

leroyjenkens said:
, I don't know how to use the array, I don't know what else I can add to the program. This is a ridiculous project to expect of us.
 
  • #13
leroyjenkens said:
Well I did some research and tried something new. Can someone show me what all is wrong with this?

Code:
#include <stdio.h>
int monthNames (void)
int main (void)
{
    int monthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
    int monthNames[12]={January,February,March,April,May,June,July,August,September,October,November,December};

}
int getYear (void)
{
    int year;
    printf("Enter a year: ")
    scanf("%d", &year);
    return year;
}
int dayFormula (void)
{
    int day;
    day = (((year-1)*365)+((year-1)/4)-((year-1)/100)+((year-1)/400)+1)%7;
    return day;
}
int leapYear (void)
{
    int days;
    if (!(year % 4) && (year % 100) || !(year % 400))
        days = 29
    else
        days = 28
    return days;
}
int printMonth (int month, int startDay, int day)
{
    for (month=0; month<=11; month++)
    {
        printf("%d", monthNames);
        printf("Sun Mon Tue Wed Thu Fri Sat Sun");

        for (startDay=0; startDay<=day; startday++)
            printf("%d", startDay);
    }
}
Your code here won't compile, since there are syntax errors. Also, even if there were no syntax errors, there wouldn't be any output, since doesn't have any executable statements - just a couple of declarations.

Here are the syntax errors or errors in logic that I noticed.

1. int monthNames (void)
You are declaring monthNames as if it were a function that has no arguments.
2. int monthNames (void)
A declaration like this needs to end with a semicolon - ;
3. int monthDays[12]={31,28,31,30,31,30,31,31,30,31,30,31};
This statement is syntactically correct, but it's not useful. This array is a local variable whose scope is only inside main(). It is not visible to any other function. You should either make it a global variable (defined outside of main) or make it a local variable inside the function that uses it, printMonth.
4. int monthNames[12]={January,February,March,April,May,June,July,August,September,October,November,December};
This definition will generate compiler errors since the names January, February, etc. are not defined.
The array should really be an array of strings, not int, and each string literal needs to be surrounded by double quotes, like this.
char * monthNames[12]={"January","February","March", etc.};

5. Just like the other array, the scope of monthNames is only the main function. The array should be global or else it should be defined in whatever function uses it.

I gave some tips in my other post, for how you could proceed. Something I didn't mention there is comments - you don't have a single comment in your code. Each function should have a comment that explains what the function does, as well as what values it expects for the parameters, and what the function returns. The compiler doesn't care about comments, but using them helps you be clear on what you're trying to do.
 
  • #14
Thanks for all the help. I'll have to turn it in as is. This is the first time I've taken a class where it seems like I'm just too dumb to understand it.
 

Related to Create a User-Friendly Calendar Program: Tips and Tricks for Success

What is a calendar program?

A calendar program is a computer software that allows users to organize and schedule events, appointments, and tasks. It typically displays dates, days of the week, and months in a user-friendly interface.

How can a calendar program help me?

A calendar program can help you stay organized and keep track of important events and tasks. It allows you to set reminders and alerts for upcoming events, view your schedule in different formats (daily, weekly, monthly), and easily make changes or updates to your schedule.

What are some common features of a calendar program?

Some common features of a calendar program include the ability to add and edit events, set reminders and alerts, sync with other calendars and devices, and color-code events. Some advanced features may include the ability to invite others to events, share calendars, and set recurring events.

How do I use a calendar program?

Using a calendar program is typically simple and user-friendly. You can start by adding events or appointments by clicking on the desired date and entering the event details. You can also customize your calendar by changing the view, adding colors or labels to events, and setting reminders or alerts. Most calendar programs also have tutorials or help resources available for more specific instructions.

Are there any free calendar programs available?

Yes, there are many free calendar programs available for download or use online. Some popular options include Google Calendar, Apple Calendar, and Microsoft Outlook. These programs usually have basic features available for free, but may offer more advanced features for a fee.

Similar threads

  • Programming and Computer Science
Replies
14
Views
2K
  • Programming and Computer Science
Replies
23
Views
1K
  • Programming and Computer Science
Replies
7
Views
1K
  • Programming and Computer Science
Replies
3
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
3
Replies
75
Views
4K
  • Programming and Computer Science
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
951
  • Engineering and Comp Sci Homework Help
Replies
3
Views
683
  • Programming and Computer Science
Replies
4
Views
7K
Back
Top