Calendar Program

T

tigrfire

I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0



*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
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

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
.
.
.

(output continues for all 12 months)

I'm not asking for source code necessarily, not that it wouldn't help,
but I'd just like some suggesstions on how to either modify or change
my current code in order to achieve a program that's not just a bunch
of wasted, repetitive code, like it currently is.

Here's what I've written, the problem is, it will only work, obviously,
for when there's no leap year, and January 1 comes on a Sunday. I don't
have a lot of programming experience and pretty much everything you see
here is all I can utilize.

/* Homework 1
Calendar Program */
#include <stdio.h>

int main()
{

/* declarations */
int daycode, leapyear, monthnum, daynum;
monthnum=1;

/* Display of Calendar */
printf(" MONTHLY CALENDAR\n\n");

printf("This program displays a calendar. You need to provide the day
of the week\non which January 1 falls, and indicate whether or not the
year is a leap year.");

printf("\n\nEnter the code number for the day of the week on which
January 1 falls:\n0-Sun 1-Mon 2-Tue 3-Wed 4-Thu 5-Fri
6-Sat");

printf("\n\nEnter day code now (0-6): " );
scanf("%d",&daycode);

printf("\nIs the calendar for a leap year? (type 1 for yes, 0 for no):
");
scanf("%d", &leapyear);

if (daycode==0 && leapyear==0 && monthnum <= 12)
{
printf(" *** CALENDAR ***\n\n");
if (monthnum==1)
{
printf("January\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 14)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 21)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 28)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 31)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==2)
{
printf("\n\nFebruary\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 4)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 11)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 18)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 25)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 28)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
}
if (monthnum==3)
{
printf("\n\nMarch\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 4)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 11)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 18)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 25)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 31)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==4)
{
printf("\n\nApril\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 1)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while(daynum <= 8)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 15)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 22)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 29)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 30)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==5)
{
printf("\n\nMay\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 6)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while(daynum <= 8)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 13)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 20)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 27)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 31)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==6)
{
printf("\n\nJune\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 3)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 10)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 17)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 24)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 30)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==7)
{
printf("\n\nJuly\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 1)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while(daynum <= 8)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 15)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 22)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 29)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 31)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==8)
{
printf("\n\nAugust\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 5)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while(daynum <= 8)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 12)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 19)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 26)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 31)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==9)
{
printf("\n\nSeptmember\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 2)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while(daynum <= 9)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 16)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 23)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 30)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==10)
{
printf("\n\nOctober\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 14)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 21)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 28)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 31)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==11)
{
printf("\n\nNovember\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 4)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 11)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 18)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 25)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 30)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
if (monthnum==12)
{
printf("\n\nDecember\n\n");

printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
printf(" ");
while (daynum <= 2)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while(daynum <= 9)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum <= 16)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 23)
{
printf (" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 30)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum <= 31)
{
printf(" %d ",daynum);
daynum++;
}
monthnum++;
}
printf("\n");
return 0;
}


Thanks for any help.
 
O

osmium

tigrfire said:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0



*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
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

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
.
.
.

(output continues for all 12 months)

I'm not asking for source code necessarily, not that it wouldn't help,
but I'd just like some suggesstions on how to either modify or change
my current code in order to achieve a program that's not just a bunch
of wasted, repetitive code, like it currently is.

Here's what I've written, the problem is, it will only work, obviously,
for when there's no leap year, and January 1 comes on a Sunday. I don't
have a lot of programming experience and pretty much everything you see
here is all I can utilize.
<snip>

For the "meaty" part of the program, the part that displays the actual
numbers, you want a function that can print 28, 39, 30 or 31 days with a gap
at the beginning of from 0 to 6 days. So I suggest trying to write a
function suggested by the following prototype.

void print_month(int skip, int tot_days); /* 0..6, 28..31*/

This may suggest subsidiary functions, it may not. It kind of depends on
how your mind works.

The header that goes Sun Mon ,,, Sat could be called as the first line of
the above function, it's always the same.

The line that prints the name of the month could be in a two dimensional
array ( a constant) so you merely provide the month number to cause it to be
printed. You could modify print_month so it could print the name of the
month as well. Two dimensional arrays are a bit nasty so do this last.
 
T

tigrfire

thanks for the response but i cant use any functions besides main nor
can i use arrays. pretty much everything i have written already is the
total number of things i can utilize to do this.
 
K

Keith Thompson

tigrfire said:
thanks for the response but i cant use any functions besides main nor
can i use arrays. pretty much everything i have written already is the
total number of things i can utilize to do this.

Please don't assume that everyone can see the article to which you're
replying. You need to provide some context. See 99% of the followup
articles in this newsgroup for examples of how to quote enough text to
make each article make sense on its own. Google makes it gratuitously
difficult to do this properly.

If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.

Having said that, *why* can't you use any functions besides main or
arrays? I can't think of any good reason for such restrictions.

Unless this is homework. If so, you should have said so in the first
place -- and based on what you've said so far, it's a ridiculous
assignment.
 
W

Walter Roberson

Having said that, *why* can't you use any functions besides main or
arrays? I can't think of any good reason for such restrictions.
Unless this is homework. If so, you should have said so in the first
place -- and based on what you've said so far, it's a ridiculous
assignment.

The first comment of the OP's code was:

/* Homework 1
Calendar Program */
 
T

tigrfire

It is a homework program, I'm not trying to lie to you. I didn't ask
for the answer, I asked for suggestions, please don't flame me without
understanding what I asked for. I didn't say source code couldn't be
posted here, not that I would copy it, just that it would still be of
aid to me. I merely asked for suggestions or alternative ways of doing
the program, not that it be written for me and I don't interpret that
as cheating in any way.
 
W

Walter Roberson

thanks for the response but i cant use any functions besides main nor
can i use arrays. pretty much everything i have written already is the
total number of things i can utilize to do this.

Are you allowed to use macros?
Are you allowed to use the question-colon operator ?: ?
Are you allowed to use pointers?
Are you allowed to use string literals such as "January" ?
Are you allowed to use the 'for' statement?
Are you allowed to use the % operator? The bitwise and operator?
Multiplication and division?
 
K

Keith Thompson

tigrfire said:
It is a homework program, I'm not trying to lie to you. I didn't ask
for the answer, I asked for suggestions, please don't flame me without
understanding what I asked for. I didn't say source code couldn't be
posted here, not that I would copy it, just that it would still be of
aid to me. I merely asked for suggestions or alternative ways of doing
the program, not that it be written for me and I don't interpret that
as cheating in any way.

Ok, I didn't notice the "Homework 1" comment; sorry (and thanks to
Walter for pointing it out).

You still need to provide context when you post a followup. Please
see my previous article for instructions.

And you tell us you can't use functions other than main() or arrays.
We have no idea what other silly restrictions you're operating under,
which limits how much help we can offer.
 
I

Ian Malone

tigrfire said:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0

As you said in another post you can't use functions or arrays.
I'm not sure what the aim of the exercise is. Except maybe
to force you to do it this way and then show you how it should
be done.

Since what you basically have is one function to be called in a
loop (does a month of a given number of days, starting from a
specified day) it's possible to either call main recursively
(more trouble than it's worth, especially given that you can't
use arrays) or put the statements in a loop.

At which point the array restriction starts to bite. Of course
an array is little more than an adjacent list of objects.
You'll just have to consider how you might do;
char *array[12], c;
array[0] = "January";
array[1] = "February";
/* ... initialise rest of array[] and ii to something sensible */
c = array[ii];
without the convenience of [] doing the indexing for you.

(And finally, if you're not allowed to use arrays, are you
allowed to use string literals?)
 
J

Jordan Abel

It is a homework program, I'm not trying to lie to you. I didn't ask
for the answer, I asked for suggestions, please don't flame me without
understanding what I asked for. I didn't say source code couldn't be
posted here, not that I would copy it, just that it would still be of
aid to me. I merely asked for suggestions or alternative ways of doing
the program, not that it be written for me and I don't interpret that
as cheating in any way.

Well, the most sensible "alternative ways" are ones you've said
you're forbidden to do. You could fake functions using a few state
variables and gotos, but I seriously doubt that would go over well
with your instructor. That really is an assignment much more
complicated than it's reasonable to expect someone to do without
using subroutines. Your teacher is a jerk.
 
S

Simon Biber

tigrfire said:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0



*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
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

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
.
.
.

(output continues for all 12 months)

If you're not allowed to use arrays, how about a switch statement? Is
that allowed?

It turns out the % operator is very useful in cases like this. You can
use it to work out when to insert a newline (at the end of each week),
and also use it to work out what the daycode of the next month will be.

Here's a full solution. I trust you won't submit it and pretend it is
your own work.


#include <stdio.h>
#include <stdlib.h>

int main(void)
{
int daycode;
int leapyear;
int month;

/* Display of Calendar */
printf(" MONTHLY CALENDAR\n\n");

printf("This program displays a calendar. You need to provide the"
" day of the week\non which January 1 falls, and indicate"
" whether or not the year is a leap year.");
printf("\n\nEnter the code number for the day of the week on which "
"January 1 falls:\n"
"0-Sun 1-Mon 2-Tue 3-Wed 4-Thu 5-Fri 6-Sat");
printf("\n\nEnter day code now (0-6): " );
fflush(stdout);
if(scanf("%d", &daycode) != 1 || daycode < 0 || daycode > 6)
{
printf("Invalid input\n");
exit(EXIT_FAILURE);
}

printf("\nIs the calendar for a leap year? (type 1 for yes, 0 for"
" no):");
fflush(stdout);
if(scanf("%d", &leapyear) != 1 || leapyear < 0 || leapyear > 1)
{
printf("Invalid input\n");
exit(EXIT_FAILURE);
}

for(month = 1; month <= 12; month++)
{
int num_days;
int day;

switch(month)
{
case 1: num_days = 31; printf("January\n\n"); break;
case 2: num_days = 28 + leapyear; printf("February\n\n"); break;
case 3: num_days = 31; printf("March\n\n"); break;
case 4: num_days = 30; printf("April\n\n"); break;
case 5: num_days = 31; printf("May\n\n"); break;
case 6: num_days = 30; printf("June\n\n"); break;
case 7: num_days = 31; printf("July\n\n"); break;
case 8: num_days = 31; printf("August\n\n"); break;
case 9: num_days = 30; printf("September\n\n"); break;
case 10: num_days = 31; printf("October\n\n"); break;
case 11: num_days = 30; printf("November\n\n"); break;
case 12: num_days = 31; printf("December\n\n"); break;
default: printf("Internal error, whoops!\n"); exit(EXIT_FAILURE);
}

printf("Sun Mon Tue Wed Thu Fri Sat\n");
for(day = 0; day < daycode; day++)
{
printf(" ");
}
for(day = 1; day <= num_days; day++)
{
printf("%3d ", day);

/* determine when to output newline */
if(day != num_days && (day + daycode) % 7 == 0) printf("\n");
}
printf("\n\n");

/* determine the daycode for the next month */
daycode = (daycode + num_days) % 7;
}
return 0;
}
 
T

tigrfire

You've all been very helpful, especially Simon, who guessed correctly
that I could use the switch statement. As far as all your other
questions go, I couldn't use anything from the questions you asked
besides the for statement and multiplication and division. Hopefully
I'll be able to do this by tommorow night, when it's due, and I'll post
my final results here. Thanks for all your help everyone.
 
A

AK

tigrfire said:
I'm trying to write a program that will display the following output:
MONTHLY CALENDAR

This program displays a calendar. You need to provide the day of the
week
on which January 1 falls, and indicate whether or not the year is a
leap year.

Enter the code number for the day of the week on which January 1 falls:
0- Sun 1- Mon 2- Tue 3- Wed 4- Thu 5- Fri 6- Sat

Enter day code now (0 - 6): 4

Is the calendar for a leap year? (type 1 for yes, 0 for no): 0



*** CALENDAR ***

January

Sun Mon Tue Wed Thu Fri Sat
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

February

Sun Mon Tue Wed Thu Fri Sat
1 2 3 4 5 6 7
8 9 10 11 12 13 14
.
.
.

(output continues for all 12 months)

I'm not asking for source code necessarily, not that it wouldn't help,
but I'd just like some suggesstions on how to either modify or change
my current code in order to achieve a program that's not just a bunch
of wasted, repetitive code, like it currently is.

Here's what I've written, the problem is, it will only work, obviously,
for when there's no leap year, and January 1 comes on a Sunday. I don't
have a lot of programming experience and pretty much everything you see
here is all I can utilize.

Hi

Is this excactly what your teacher has given you? Why I ask is, because
I got a similar assignment from my teacher which asked me to get only 2
i/ps - month and year and display the calendar of that month in that
year as output formatted as in your case. Besides, there is one problem
with your assignment. If you are in window's command mode, your full
output won't be visible in the screen since it is too long. Anyway, it
is a class assignment, a ridiculous one too.

Regards
AK
Owner
Programmer's HQ
http://groups.google.com/group/programhq
 
T

tigrfire

It's possible you either attend my university, or have a teacher who
got his/her assignment from the same place. In any case, I wasn't able
to take much from Simon, this being my first programming assignment
ever and having only had a week of experience, though his was the one I
was most able to understand, so kudos again. I think I'm calling it
quits for tonight though, unable to get this format to work out for me,
I'm kind of at a stand still. If anyone wants to run through it and
figure out why it doesn't work, I won't stop them and it'd help me out
a ton. Thanks.


#include <stdio.h>

int main()
{

/* declarations */
int daycode, leapyear, monthnum, daynum, firstday, monthdays;
monthnum=1;

/* Display of Calendar */
printf(" MONTHLY CALENDAR\n\n");

printf("This program displays a calendar. You need to provide the day
of the week\non which January 1 falls, and indicate whether or not the
year is a leap year.");

printf("\n\nEnter the code number for the day of the week on which
January 1 falls:\n0-Sun 1-Mon 2-Tue 3-Wed 4-Thu 5-Fri
6-Sat");

printf("\n\nEnter day code now (0-6): " );
scanf("%d",&daycode);

printf("\nIs the calendar for a leap year? (type 1 for yes, 0 for no):
");
scanf("%d", &leapyear);

while (monthnum<=12)
{
if (daycode==0)
{
printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
if (monthnum==1||3||5||7||8||11||12)
{
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=14)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=21)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=31)
{
printf(" %d ",daynum);
daynum++;
}
}
else if (monthnum==4||6||9||10)
{
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=14)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=21)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=30)
{
printf(" %d ",daynum);
daynum++;
}
}
else if (monthnum==2)
{
while (daynum <= 7)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=14)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=21)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
if (leapyear==0)
{
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
}
else
{
while (daynum<=29)
{
printf(" %d ",daynum);
daynum++;
}
}
}
else
{
return 0;
}
}

else if (daycode==1)
{
printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
{
if (monthnum==(1||3||5||7||8||11||12))
{
printf(" ");
while (daynum <= 6)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=13)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=20)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=27)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=31)
{
printf(" %d ",daynum);
daynum++;
}
}
else if (monthnum==(4||6||9||10))
{
while (daynum <= 6)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=13)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=20)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=27)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=30)
{
printf(" %d ",daynum);
daynum++;
}
}
else
{
while (daynum <= 6)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum < 10)
{
printf(" %d ",daynum);
daynum++;
}
while (daynum<=13)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=20)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
while (daynum<=27)
{
printf(" %d ",daynum);
daynum++;
}
printf("\n");
if (leapyear==0)
{
while (daynum<=28)
{
printf(" %d ",daynum);
daynum++;
}
}
else
{
while (daynum<=29)
{
printf(" %d ",daynum);
daynum++;
}
}
}
}
}
monthnum++;
printf("\n\n");
}
return 0;
}
 
M

Mike Wahler

tigrfire said:
It's possible you either attend my university, or have a teacher who
got his/her assignment from the same place. In any case, I wasn't able
to take much from Simon, this being my first programming assignment
ever and having only had a week of experience, though his was the one I
was most able to understand, so kudos again. I think I'm calling it
quits for tonight though, unable to get this format to work out for me,
I'm kind of at a stand still. If anyone wants to run through it and
figure out why it doesn't work, I won't stop them and it'd help me out
a ton. Thanks.

I didn't try to compile or run your code, or
look at it in depth, but one problem did jump
out at me. See below.
#include <stdio.h>

int main()
{

/* declarations */
int daycode, leapyear, monthnum, daynum, firstday, monthdays;
monthnum=1;

/* Display of Calendar */
printf(" MONTHLY CALENDAR\n\n");

printf("This program displays a calendar. You need to provide the day
of the week\non which January 1 falls, and indicate whether or not the
year is a leap year.");

printf("\n\nEnter the code number for the day of the week on which
January 1 falls:\n0-Sun 1-Mon 2-Tue 3-Wed 4-Thu 5-Fri
6-Sat");

printf("\n\nEnter day code now (0-6): " );
scanf("%d",&daycode);

printf("\nIs the calendar for a leap year? (type 1 for yes, 0 for no):
");
scanf("%d", &leapyear);

while (monthnum<=12)
{
if (daycode==0)
{
printf("Sun Mon Tue Wed Thu Fri Sat\n");
daynum=1;
if (monthnum==1||3||5||7||8||11||12)

This doesn't do what you probably think. Here, you're
comparing 'monthnum' only once, to a single value, not
against several values, as it appears you believe.

The expression '1||3||5||7||8||11||12' will yield a single
value, which will always be true. Then you compare it
with 'monthnum'. What you probably want is:

if(monthnum == 1 || monthnum == 3 || monthnum == 5 /* etc */)

A 'shortcut' way to do this would be with a 'switch', letting
each 'case' 'fall through to the next, e.g.:

switch(monthnum)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 11:
case 12:
/* do your stuff */
break;
/* and I see the same issue further down in your code, so
we just keep going with the other values: */
case 4:
case 6:
case 9:
case 10:
/* do your stuff */
break;
default:
/* something wrong */
break;
}

But doesn't something look odd? How many days in November?
In December? :)

Again, this was just a quick look, all I've got time
for right now.

HTH,
-Mike
 
K

Keith Thompson

Mike Wahler said:
This doesn't do what you probably think. Here, you're
comparing 'monthnum' only once, to a single value, not
against several values, as it appears you believe.

The expression '1||3||5||7||8||11||12' will yield a single
value, which will always be true. Then you compare it
with 'monthnum'.

Not quite. "==" binds more tightly than "||", so the condition is
equivalent to:

(monthnum==1) || 3 || 5 || 7 || 8 || 11 || 12

If monthnum is 1, (monthnum==1) evalutes to 1 (true), and everything
else is short-circuited. Otherwise, (monthnum==1) evalutes to 0, 3 is
true, and everything else is short-circuited. The condition as a
whole is always true.

But once you realize that it doesn't mean what you think it means,
it's probably not important to figure out just *how* it misbehaves
(unless it's part of an existing program and you're trying to track
down a bug).
What you probably want is:

if(monthnum == 1 || monthnum == 3 || monthnum == 5 /* etc */)

Yes.
 
M

Mike Wahler

Keith Thompson said:
Not quite. "==" binds more tightly than "||", so the condition is
equivalent to:

(monthnum==1) || 3 || 5 || 7 || 8 || 11 || 12

If monthnum is 1, (monthnum==1) evalutes to 1 (true), and everything
else is short-circuited. Otherwise, (monthnum==1) evalutes to 0, 3 is
true, and everything else is short-circuited. The condition as a
whole is always true.

Yes, you're right, I looked at the code and wrote that
'correction' rather hurriedly (which is of course no
excuse for giving wrong information). Thanks for
the correction.

-Mike
 
T

tigrfire

Thanks Mike and Keith for helping me out. I found out just today before
reading your responses that the way the OR works is indeed as you say
it is and not how I had thought. That being said, I made said
corrections to my program only to realize that I was indeed over my
head with the amount of work to be done if I was going to continue
doing it my way. I decided to look more in depth at the way Simon
described the program, as well as your way too Mike, and read up on how
the switch statement works. It's quite the Godsend in this application
so I started over with that as my base. As far as the output formatting
goes, I owe Simon all the credit for showing me how to use % to modify
the output and its behavior. Though I'm still a little hazy, I think
I've learned quite a bit from this. My final code is no better, in
fact, only worse than Simon's because of his redundancy-checking
(correct terminology?), so it's of little use to post here. Simon,
Mike, Keith, and all you others, thanks for the help. I'm sure I'll be
back though as this isn't my in-depth field of study and everyone has
problems somewhere.

Thanks Again.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top