Click to See Complete Forum and Search --> : errors


mhuneidi
March 10th, 2008, 02:08 PM
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

#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!

Plasmator
March 10th, 2008, 02:18 PM
It's immediately apparent that you're missing a bunch of semicolons -- mostly after your calls to printf.

GCDEF
March 10th, 2008, 02:23 PM
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.

mhuneidi
March 10th, 2008, 02:43 PM
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





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 }

Plasmator
March 10th, 2008, 02:45 PM
Looks like you're not closing the parenthesis on lines 37 and 40.

Use your eyes, they're a great tool... :wave:

jeron
March 10th, 2008, 02:46 PM
Looks like you are missing a ')' character on lines 37 and 40.

GCDEF
March 10th, 2008, 02:48 PM
Let's think about this one for a minute.

(day<=29 || day>0)

How would that expression evaluate 103?

GCDEF
March 10th, 2008, 02:49 PM
Looks like you're not closing the parenthesis on lines 37 and 40.

Use your eyes, they're a great tool... :wave:

And if your eyes fail you, ctrl+] works too, at least in MSVC.

Plasmator
March 10th, 2008, 02:50 PM
And if your eyes fail you, ctrl+[ works too, at least in MSVC.Neat! I wasn't aware of that, looks useful. =)

GCDEF
March 10th, 2008, 02:59 PM
Neat! I wasn't aware of that, looks useful. =)

Just fixed my typo above. It's ctrl+].

mhuneidi
March 10th, 2008, 03:19 PM
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 ...

Paul McKenzie
March 10th, 2008, 03:29 PM
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.

(day<=29 || day>0)

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

mhuneidi
March 10th, 2008, 03:53 PM
oh so it should be an && statement?

GCDEF
March 10th, 2008, 03:55 PM
oh so it should be an && statement?

Try it and see. ;)