CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 14 of 14

Thread: errors

  1. #1
    Join Date
    Feb 2008
    Posts
    24

    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!

  2. #2
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: errors

    It's immediately apparent that you're missing a bunch of semicolons -- mostly after your calls to printf.

  3. #3
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    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.

  4. #4
    Join Date
    Feb 2008
    Posts
    24

    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 }

  5. #5
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: errors

    Looks like you're not closing the parenthesis on lines 37 and 40.

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

  6. #6
    Join Date
    Jun 2005
    Posts
    315

    Re: errors

    Looks like you are missing a ')' character on lines 37 and 40.

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: errors

    Let's think about this one for a minute.

    (day<=29 || day>0)

    How would that expression evaluate 103?

  8. #8
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: errors

    Quote 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.

  9. #9
    Join Date
    Jun 2006
    Location
    M31
    Posts
    885

    Re: errors

    Quote 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. =)

  10. #10
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: errors

    Quote Originally Posted by Plasmator
    Neat! I wasn't aware of that, looks useful. =)
    Just fixed my typo above. It's ctrl+].

  11. #11
    Join Date
    Feb 2008
    Posts
    24

    Re: errors

    Quote 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 ...

  12. #12
    Join Date
    Apr 1999
    Posts
    27,449

    Re: errors

    Quote 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.
    Code:
    (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

  13. #13
    Join Date
    Feb 2008
    Posts
    24

    Re: errors

    oh so it should be an && statement?

  14. #14
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,637

    Re: errors

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured