Number of days per year (including leap years)

Hey guys! I've been working on this code to figure out the days in a year, example, 50 days have passed, but I cannot seem to get it to work.
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stdio.h>
#include <math.h>
int main()
{
   int d, m, year, x, date;
   char more;
   {
   do
   {
        printf("\n\t\t\tInput date (MM/DD/YYYY): ");
        scanf ("%d/%d/%d", &m, &d, &year);
        {
        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
        {
           x = 29;
           printf("\n\t\t\tImput mm/dd/yy is wrong!"); //only display if the day is wrong, ex, 2/29/2001 or 4/31/2000 (only 30 days in the month)
        }
        else
        {
        x = 28;
        }
        switch (m)
        {
           case 1:
                date = (d);
                break;
           case 2:
                date = (31 + d);
                break;
           case 3:
                date = (31 + x + d);
                break;
           case 4:
                date = ((2 * 31) + x + d);
                break;
           case 5:
                date = ((2 * 31) + 30 + x + d);
                break;
           case 6:
                date = ((3 * 31) + 30 + x + d);
                break;
           case 7:
                date = ((3 * 31) + (2 * 30) + x + d);
                break;
           case 8:
                date = ((4 * 31) + (2 * 30) + x + d);
                break;
           case 9:
                date = ((5 * 31) + (2 * 30) + x + d);
                break;
           case 10:
                date = ((5 * 31) + (3 * 30) + x + d);
                break;
           case 11:
                date = ((6 * 31) + (3 * 30) + x + d);
                break;
           case 12:
                date = ((6 * 31) + (4 * 30) + x + d);
                break;
     
     }
     printf("\n\t\t\tThere are %d days past in the year", date);
     
     printf ("\n\t\t\tDo more (Y/N) ? ");
     scanf  ("%s", &more);
     }    
    } while (more == 'y' || more == 'Y');
      
  }
}

Everything else works, I just can't seem to get the "Input mm/dd/yy" to display in the right place.
Thanks
What do you mean by "display in the right place"?
At line 10, you're outputting 3 tabs, so the output should be spaced over 24 positions. Where do you think it should be?

At line 16, you're doing a printf that the date is wrong if you enter a leap year.
I mean, if you input, 4/31/2001, it should say, "Input mm/dd/yy is wrong" because there are only 30 days in that month. So it should only display if the wrong date is put in.

I like the look when it is 3 tabs over, makes it more centered.

And yes, but I don't know how to make it only display when the wrong date is entered.
Ooh, a C program! Haven't seen one in ages!

Okay, I am just a student too, but the fact that you are telling the program to say the date is wrong any time the user puts in a leap year might be your problem. I would make a function called checkDate(int month, int day, int year, int daysInFeb) that would check. Then take your "Date does not exist" code, pop it in another if statement, this time one like this:
1
2
3
4
if(!checkDate(m,d,year,x))
{
     printf("\n\t\t\tInput%d/%d/%d is wrong!", m,d,year);//notice that it now kicks out their date
}


And then declare your checkDate function like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
bool checkDate(int month, int day, int year, int daysInFeb)
{
     int daysinMonth;
     if(year <= 0)//there is no year 0.
          return false;
     if(month < 1 || month > 12)
          return false;
     if(month == 2 && day > daysInFeb)
          return false;
     else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
          daysinMonth = 31;
     else if(month == 4 || month == 6 || month == 9 || month == 11)
          daysinMonth = 30;
     if(day > daysinMonth)
          return false;
     else
          return true;
}


I checked it before I published it. Oh, and I suggest initializing x to 28 days, since that is the default number of days in February.
I'm having a hard time figuring out where to put them. Like this?
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
#include <stdio.h>
#include <math.h>
#define true 1;
#define false 0;
int main()
{
   int d, m, year, x = 28, date;
   char more;
   {
   do
   {
        printf("\n\t\t\tInput date (MM/DD/YYYY): ");
        scanf ("%d/%d/%d", &m, &d, &year);
        {
        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
        {
           if(!checkDate(m,d,year,x))
           
           printf("\n\t\t\tInput%d/%d/%d is wrong!", m,d,year);//notice that it now kicks out their date
           

        }

        switch (m)
        {
           case 1:
                date = (d);
                break;
           case 2:
                date = (31 + d);
                break;
           case 3:
                date = (31 + x + d);
                break;
           case 4:
                date = ((2 * 31) + x + d);
                break;
           case 5:
                date = ((2 * 31) + 30 + x + d);
                break;
           case 6:
                date = ((3 * 31) + 30 + x + d);
                break;
           case 7:
                date = ((3 * 31) + (2 * 30) + x + d);
                break;
           case 8:
                date = ((4 * 31) + (2 * 30) + x + d);
                break;
           case 9:
                date = ((5 * 31) + (2 * 30) + x + d);
                break;
           case 10:
                date = ((5 * 31) + (3 * 30) + x + d);
                break;
           case 11:
                date = ((6 * 31) + (3 * 30) + x + d);
                break;
           case 12:
                date = ((6 * 31) + (4 * 30) + x + d);
                break;
     
     }
     printf("\n\t\t\tThere are %d days past in the year", date);
     
     printf ("\n\t\t\tDo more (Y/N) ? ");
     scanf  ("%s", &more);
     }    
    } while (more == 'y' || more == 'Y');
      
  }
}
int checkDate(int month, int day, int year, int daysInFeb)
{
     int daysinMonth;
     if(year <= 0)//there is no year 0.
          return false;
     if(month < 1 || month > 12)
          return false;
     if(month == 2 && day > daysInFeb)
          return false;
   else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12);
          daysinMonth = 31;  //this else statement gets the error//
     else if(month == 4 || month == 6 || month == 9 || month == 11)
          daysinMonth = 30;
     if(day > daysinMonth)
          return false;
     else
          return true;
}

And even with this, it gets an error "In function checkDate" and "syntax error before else"
Okay, that looks better, but now you're not setting x (daysInFeb).
You also need a forward declaration for checkDate.
Line 82, you have a semicolon after your if condition.

it gets an error "In function checkDate" and "syntax error before else"

You have an extraneous ; in lines 81, 87.
Remove the ; from lines 3 and 4.

If you're going to use true and false, you should really not #define them, but use bool instead.
 
bool checkDate(int month, int day, int year, int daysInFeb)

edit: The bool type may not be supported in your compiler if you're using an older C compiler.

Last edited on
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include <stdio.h>
#include <math.h>
#define false 0 //I have to define it, bool doesn't work for Dev-C
#define true 1
int main()
{
   int d, m, year, x = 28, date, daysinFeb = 28;
   char more;
  {
   do
   {
        printf("\n\t\t\tInput date (MM/DD/YYYY): ");
        scanf ("%d/%d/%d", &m, &d, &year);
        {
        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
          {
           if(!checkDate(m,d,year,x)) 
           
           printf("\n\t\t\tInput mm/dd/yy is wrong!", m,d,year); //everything else works, but it won't just print this. 
//It prints "Input is wrong" and "There are x days past". I just need to have it print "Input is wrong" when the date is wrong.
          }
        }
        switch (m)
        {
           case 1:
                date = (d);
                break;
           case 2:
                date = (31 + d);
                break;
           case 3:
                date = (31 + x + d);
                break;
           case 4:
                date = ((2 * 31) + x + d);
                break;
           case 5:
                date = ((2 * 31) + 30 + x + d);
                break;
           case 6:
                date = ((3 * 31) + 30 + x + d);
                break;
           case 7:
                date = ((3 * 31) + (2 * 30) + x + d);
                break;
           case 8:
                date = ((4 * 31) + (2 * 30) + x + d);
                break;
           case 9:
                date = ((5 * 31) + (2 * 30) + x + d);
                break;
           case 10:
                date = ((5 * 31) + (3 * 30) + x + d);
                break;
           case 11:
                date = ((6 * 31) + (3 * 30) + x + d);
                break;
           case 12:
                date = ((6 * 31) + (4 * 30) + x + d);
                break;
     
        }
     printf("\n\t\t\tThere are %d days past in the year", date);
     
     printf ("\n\t\t\tDo more (Y/N) ? ");
     scanf  ("%s", &more);    
    } while (more == 'y' || more == 'Y');
      
  }
}
checkDate(int month, int day, int year, int daysInFeb)
{
     int daysinMonth;
     if(year <= 0) 
          return false;
     if(month < 1 || month > 12)
          return false;
     if(month == 2 && day > daysInFeb)
          return false;
     else if(month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
          daysinMonth = 31;
     else if(month == 4 || month == 6 || month == 9 || month == 11)
          daysinMonth = 30;
     if(day > daysinMonth)
          return false;
     else
          return true;
}
Your check for a leap year and your check for a valid date should not be nested. You want to check the validity of the date regardless of whether it is a leap year or not.

1
2
3
4
5
6
    if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
        x = 29;   // Adjust # of days in Feb
    if (!checkDate(m,d,year,x)) 
    {    printf("\n\t\t\tInput mm/dd/yy is wrong!", m,d,year); 
          continue;  // Go to the next iteration of the do-while loop 
    }

Be sure to initialize more to 'Y', or you will fall through the loop.
Got it to work
1
2
3
4
5
6
7
8
9
printf("\n\t\t\tInput date (MM/DD/YYYY): ");
        scanf ("%d/%d/%d", &m, &d, &year);
        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
           {
           printf("\n\t\t\tImput mm/dd/yy is wrong!");
           break;
           }
           else
           if (d < 32)



but now I need a way to make it jump to the
1
2
3
4
5
printf ("\n\t\t\tDo more (Y/N) ? ");
     scanf  ("%s", &more);
   }
    while (more == 'y' || more == 'Y');  
}

right now, it gives the "wrong input" if the wrong date is put in, but it exits the program without asking the user "do more"

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
int d, m, year, x, date;
   char more = 'y';
   
   do
   {
        printf("\n\t\t\tInput date (MM/DD/YYYY): ");
        scanf ("%d/%d/%d", &m, &d, &year);
        if (year % 4 == 0 && !(year % 100 == 0 && year % 400 != 0))
           {
           printf("\n\t\t\tImput mm/dd/yy is wrong!");
           break;
           }
           else
           if (d < 32)
           
       
        switch (m)
        {
           case 1:
                date = (d);
                break;
           case 2:
                date = (31 + d);
                break;
           case 3:
                date = (31 + x + d);
                break;
           case 4:
                date = ((2 * 31) + x + d);
                break;
           case 5:
                date = ((2 * 31) + 30 + x + d);
                break;
           case 6:
                date = ((3 * 31) + 30 + x + d);
                break;
           case 7:
                date = ((3 * 31) + (2 * 30) + x + d);
                break;
           case 8:
                date = ((4 * 31) + (2 * 30) + x + d);
                break;
           case 9:
                date = ((5 * 31) + (2 * 30) + x + d);
                break;
           case 10:
                date = ((5 * 31) + (3 * 30) + x + d);
                break;
           case 11:
                date = ((6 * 31) + (3 * 30) + x + d);
                break;
           case 12:
                date = ((6 * 31) + (4 * 30) + x + d);
                break;
     
     }
     printf("\n\t\t\tThere are %d days past in the year", date);
     
     printf ("\n\t\t\tDo more (Y/N) ? ");
     scanf  ("%s", &more);
   }
    while (more == 'y' || more == 'Y');  
} 
You're still not setting x if it's leap year.

In the code I showed you above, I used a continue, not a break. Break will take you out of the loop, while continue will take you to the top of the loop where you prompt the user for input.

Your else/if at lines 13-14 are unrelated to the if testing for leap year.

You should put back the check_date function you had earlier.
I got it. Thank you very much!
Topic archived. No new replies allowed.