-
OK. Your case label is wrong. Case only accepts either a number of a constant. So you can say case February, because February = 2, which is how you defined your enum.
Now, think about this: when you're inside your February case statement. You're printing that Feb has 28 days at the moment. How about putting your leap-year check within the case statement, and then print out either 28 or 29 depending?
QuantumPete
-
thx for the reply
but how do i place the leap-year check within the case statement.
do i have to define leap yr from the beginning?
-
Inside your February case, use an if to determine if it's a leap year or not and print appropriate information. Shouldn't be difficult.
-
thx elysia for u help again
this is for i got so far... where should i put the if statement for leap yr? cause the below codes dont work when i compile it
Code:
switch(month)
{
case January:
printf("January has 31 days\n");
break;
if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&&
(year%4==0)))&& case February:
printf("February has 29 days\n");
else
printf("February has 28 days\n");
}
return 0;
}
-
Well, of course it won't.
If you think logically... make an internal flowchart.
Where does the leap year code need to be put?
-
sorry i have only learnt c for a couple of days
whatz an internal flowchart?
-
It means that you design or write down how want you want to perform flows.
So, for example, in your example, a simple flowchart might be:
Read input from user
...
Close program
By writing down each step, you can easily know where to place code.
-
C programs are a sequence of instructions to do things. A flowchart is a diagramatic way to describe how a program works.
But you don't need to have a flowchart - you can use many other ways to describe the code. What is important is that you understand that the compiler has very strict rules about what goes where, and how the code can and can't be organized, and the sequence of your code is important. If you want to bake a cake, you have to do things in order: turn on the oven, mix the ingredients, pour the mix in a cakeform, then put it in the oven, wait a specified amount of time, then take the cake out and turn off the oven. If you put the raw ingredients in the oven, then mix them afterwards, it won't be a cake - it may still turn out to be sort of edible, but chances are that it won't be very nice.
Same applies to programs - your program is a RECIPE for doing some particular calculations or work.
--
Mats
-
ah k thx i sort of think logically for a while but my method still don't work (sorry im dumb)
Code:
switch(month)
{
case January:
printf("January has 31 days\n");
break;
case February:
printf("February has 28 days\n");
else if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&&
(year%4==0)))
printf("February has 28 days\n");
}
return 0;
}
-
You have "else if" without if.
How can you "do something else" without "something" first?
-
any tips at all about where i need to put or define the leap year code in the program?
because im really lost
-
You're on the right track, but you're still making a hash of it.
You need to start with an if, not an else. And you probably want that FIRST, then print afterwards - like cake-baking, you're currently putting the ingredients in first (printing 28 days), then mixing (checking if it's a leapyear).
--
Mats
-
Code:
enum months
{
January = 1,
February,
March,
April,
May,
June,
July,
August,
September,
October,
November,
December
};
int main()
{
int year;
enum months month;
printf("Enter year and month:");
scanf("%d %d", &year, &month);
switch(month)
{
case January:
printf("January has 31 days\n");
break;
case February:
if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&&
(year%4==0)))
{
printf("February has 29 days\n");
}
else
{
printf("February has 28 days\n");
}
break;
case March:
printf("March has 31 days\n");
break;
}
return 0;
}
-
thx mats
but i thought i need to get the program to check whether the input before for the year is leap or not
then decide whether feb is 28 or 29 days
Code:
//Create by z3252440
#include <stdio.h>
int main()
{
int year, month;
enum months{January = 1, February, March, April, May, June, July,
August, September, October, November, December};
printf("Enter year and month:");
scanf("%d %d", &year, &month);
if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&&
(year%4==0)))
{
printf("has three hundred and sixty six (366) days\n");
}
else
{
printf("has three hundred and sixty five (365) days\n");
}
switch(month)
{
case January:
printf("January has 31 days\n");
break;
if (((year%4==0 )&& (year%100!=0)) || ((year%400==0) && (year%100==0)&&
(year%4==0)))
case February:
printf("February has 28 days\n");
else
printf("February has 29 days\n");
break;
}
return 0;
}
As can seen above shouldnt the program check the year then decide which statement to print?
cause this is where i am getting really confuse
-