|
-
March 10th, 2008, 02:08 PM
#1
errors
Hey all... Well im supposed to create a program in which you input the day/month/year in digit form and the output tells you whether the date you've entered is valid.
For example if you enter 30 in February it should give you a false statement ... and so i've created this program
Code:
#include <stdio.h>
int main ()
{
int month; /* input - month */
int day; /* input - day */
int year; /* input - year */
/* Prompt the user and read in a month digit */
printf ("Please enter a month in number form. >");
scanf ("%d", &month);
/* Prompt the user and read in a day
printf ("Please enter a day (1-31). >");
scanf ("%d", &day); */
/* Prompt the user and read in a year */
printf ("Please enter a year (4 digits). >");
scanf ("%d", &year);
if ((month==1) && (day<=31)) {
printf("This date is valid")
}
else if ((month==1) && (day>31)){
printf("This date is NOT valid")
}
else if ((month==2) && (year%400==0) && (day<=29)) {
printf("This date is valid")
}
else if ((month==2) && (year%400==0) && (day>29)){
printf("This date is NOT valid")
}
else if ((month==2) && (year%100==0) && (day<=28)) {
printf("This date is valid")
}
else if ((month==2) && (year%100==0) && (day>28) {
printf("This date is valid")
}
else if ((month==2) && (year%4==0) && (day<=29) {
printf("This date is valid")
}
else if ((month==2) && (year%4==0) && (day>29) {
printf("This date is NOT valid")
}
else if ((month==3) && (day<=30)) {
printf("This date is valid")
}
else if ((month==3) && (day>30)){
printf("This date is NOT valid")
}
else if ((month==4) && (day<=31)) {
printf("This date is valid")
}
else if ((month==4) && (day>30)){
printf("This date is NOT valid")
}
else if ((month==5) && (day<=31)) {
printf("This date is valid")
}
else if ((month==5) && (day>31)){
printf("This date is NOT valid")
}
else if ((month==6) && (day<=30)) {
printf("This date is valid")
}
else if ((month==6) && (day>30)){
printf("This date is NOT valid")
}
else if ((month==7) && (day<=31)) {
printf("This date is valid")
}
else if ((month==7) && (day>31)){
printf("This date is NOT valid")
}
else if ((month==8) && (day<=31)) {
printf("This date is valid")
}
else if ((month==8) && (day>31)){
printf("This date is NOT valid")
}
else if ((month==9) && (day<=30)) {
printf("This date is valid")
}
else if ((month==9) && (day>30)){
printf("This date is NOT valid")
}
else if ((month==10) && (day<=31)) {
printf("This date is valid")
}
else if ((month==10) && (day>31)){
printf("This date is NOT valid")
}
else if ((month==11) && (day<=30)) {
printf("This date is valid")
}
else if ((month==11) && (day>30)){
printf("This date is NOT valid")
}
else if ((month==12) && (day<=31)) {
printf("This date is valid")
}
else if ((month==12) && (day>31)){
printf("This date is NOT valid")
}
}
And i get these errors ....:
Project4.c: In function `main':
Project4.c:24: parse error before `}'
Project4.c:27: parse error before `}'
Project4.c:32: parse error before `}'
Project4.c:35: parse error before `}'
Project4.c:38: parse error before `}'
Project4.c:39: parse error before `{'
Project4.c:41: parse error before `}'
Can anyone help me out pleasE? Thanks!
-
March 10th, 2008, 02:18 PM
#2
Re: errors
It's immediately apparent that you're missing a bunch of semicolons -- mostly after your calls to printf.
-
March 10th, 2008, 02:23 PM
#3
Re: errors
Your program could benefit from arrays, or at least consolidation of your if statements.
if((month == 1 || month == 3 || month ==5...) && (day < 1 || day > 31))
Also, 0 is less than 31, but not a valid day
March has 31 days, April has 30.
-
March 10th, 2008, 02:43 PM
#4
Re: errors
Thanks ... so I have included all the suggestions but I still get an error:
Project4.c: In function `main':
Project4.c:39: parse error before `{'
Project4.c:42: parse error before `else'
Project4.c:44: warning: control reaches end of non-void function
Code:
28 else if ((month==2) && (year%400==0) && (day<=29 || day>0)) {
29 printf("This date is valid");
30 }
31 else if ((month==2) && (year%400==0) && (day>29 || day>0)){
32 printf("This date is NOT valid");
33 }
34 else if ((month==2) && (year%100==0) && (day<=28 || day>0)) {
35 printf("This date is valid");
36 }
37 else if ((month==2) && (year%100==0) && (day>28 || day>0) {
38 printf("This date is NOT valid");
39 }
40 else if ((month==2) && (year%4==0) && (day<=29 || day>0) {
41 printf("This date is valid");
42 }
-
March 10th, 2008, 02:45 PM
#5
Re: errors
Looks like you're not closing the parenthesis on lines 37 and 40.
Use your eyes, they're a great tool...
-
March 10th, 2008, 02:46 PM
#6
Re: errors
Looks like you are missing a ')' character on lines 37 and 40.
-
March 10th, 2008, 02:48 PM
#7
Re: errors
Let's think about this one for a minute.
(day<=29 || day>0)
How would that expression evaluate 103?
-
March 10th, 2008, 02:49 PM
#8
Re: errors
 Originally Posted by Plasmator
Looks like you're not closing the parenthesis on lines 37 and 40.
Use your eyes, they're a great tool... 
And if your eyes fail you, ctrl+] works too, at least in MSVC.
Last edited by GCDEF; March 10th, 2008 at 02:58 PM.
-
March 10th, 2008, 02:50 PM
#9
Re: errors
 Originally Posted by GCDEF
And if your eyes fail you, ctrl+[ works too, at least in MSVC.
Neat! I wasn't aware of that, looks useful. =)
-
March 10th, 2008, 02:59 PM
#10
Re: errors
 Originally Posted by Plasmator
Neat! I wasn't aware of that, looks useful. =)
Just fixed my typo above. It's ctrl+].
-
March 10th, 2008, 03:19 PM
#11
Re: errors
 Originally Posted by GCDEF
Let's think about this one for a minute.
(day<=29 || day>0)
How would that expression evaluate 103?
Ya the program compiles and runs now ... but the statement you mentioned does seem incorrect even when i run it for any month ... it will always give me an output of :
"This date is valid"
when an input of 35 for days is pushed in ...
-
March 10th, 2008, 03:29 PM
#12
Re: errors
 Originally Posted by mhuneidi
Ya the program compiles and runs now ... but the statement you mentioned does seem incorrect even when i run it for any month ... it will always give me an output of :
"This date is valid"
when an input of 35 for days is pushed in ...
That is because "day" is greater than 0, and your if() statement clearly states that days > 0 are valid days.
Say it out loud:
"If day is less than or equal to 29 or day is greater than 0, then day is valid".
See the mistake?
Regards,
Paul McKenzie
-
March 10th, 2008, 03:53 PM
#13
Re: errors
oh so it should be an && statement?
-
March 10th, 2008, 03:55 PM
#14
Re: errors
 Originally Posted by mhuneidi
oh so it should be an && statement?
Try it and see.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|