a program to store date with structure and print

i wrote this code can somebody check it i am not getting output please help me
program
#include<stdio.h>
#include<conio.h>
struct date
{
int day;
int month;
int year;
};
void main()
{
int i,j,k;
struct date d[356];

for (i=1;i<=1;i++)
{ {
printf("\n enter year");
scanf("%d",&d[i].year);
}
for(j=1;j<=12;j++)
{
if((j%2==0)&&(j!=2))
{
for (k=1;k<=31;k++)
scanf("%d%d",&d[j].month,&d[i].day);
}
if(j==2)
{
for (k=1;k<=28;k++)
scanf("%d%d",&d[j].month,&d[i].day);
}
if((j%2)!=0)
{
for (k=1;k<=30;k++)
scanf("%d%d",&d[j].month,&d[i].day);
}
}
}

for(j=1;j<=3;j++)
{
if((j%2==0)&&(j!=2))
{
for (k=1;k<=31;k++)
printf("%2d%2d",&d[j].month,&d[i].day);
}
}
}
getch();
}

First of all, can you please use code tags for formatted code? It's really hard to read your code as is.

Second, main returns an int, not void.

Third, you should use the cin and cout streams in C++. They're much safer to use.

Fourth, please don't ask users to input the date 366 times.
Last edited on
closed account (G30GNwbp)
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
#include <cstdio>

struct date
{
    int day;
    int month;
    int year;
};

int main()
{

    date d[356];
     
    for ( int i = 0; i < 3; i++)
    {
        printf("\n enter year: ");
        scanf("%d",&d[i].year);

        printf("\n enter month: ");
        scanf("%d",&d[i].month);

        printf("\n enter day: ");
        scanf("%d",&d[i].day);
    }

    for ( int i = 0; i < 3; i++)
    {
        printf("%d/%d/%d\n", d[i].day, d[i].month, d[i].year);
    }

    return 0;
}


Here is some example code that actually produces output. I am sorry I can't help more but it is hard to figure out what you are trying to do looking at the code you posted.
Last edited on
thanks for your help
i was trying to get a calender program
to store all dates in a year
and check whether the entered date is valid or not
for eg:31/2/13 is invalid
Last edited on
closed account (G30GNwbp)
This doesn't check if a date is valid, but it does print out a year of dates.
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#include <iostream>

struct date
{
    date (int y = 2013) : year (y) {}

    int day;
    int month;
    int year;

    bool is_leap(); // returns true for a leap year.
    bool is_valid_yr(); // checks to see if years is between 1900 and 2013;

};

void populate_year_with_valid_dates ( date* d, int year);

int main()
{

    const int MAX_DAYS = 366;
    date* d = new date[MAX_DAYS];
    int year {0};

    std::cout << "enter a year: ";
    std::cin >> year;

    populate_year_with_valid_dates (d, year);
     

    for ( int i = 0; i < MAX_DAYS; i++)
    {
        std::cout <<  d[i].month << '/' << d[i].day << '/' << d[i].year << std::endl;
    }

    return 0;
}

bool date::is_valid_yr()
{
    return ( 1899 < year && year < 2014);
}

bool date::is_leap()
{
    return (year % 4 == 0 && year != 2000); 
}
        

void populate_year_with_valid_dates ( date* d, int year)
{
    for(int i = 0; (d[i].month != 12 && d[i].day != 31);)
    {
        d[i].year = year;
        if (!d[i].is_valid_yr ())
            return; // ugly should throw an error
        if (d[i].is_valid_yr() && d[i].is_leap())
        {
            for (int month = 1; month < 13; ++month)
            {
                switch (month)
                {
                    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                        for (int day = 1; day < 32; ++day)
                        {
                            d[i].month = month;
                            d[i].year = year;
                            d[i++].day = day;
                        }
                        break;
                    case 4: case 6: case 9: case 11: 
                        for (int day = 1; day < 31; ++day)
                        {
                            d[i].year = year;
                            d[i].month = month;
                            d[i++].day = day;
                        }
                        break;
                    case 2:
                        for (int day = 1; day < 30; ++day)
                        {
                            d[i].year = year;
                            d[i].month = month;
                            d[i++].day = day;
                        } 
                        break;
                }
            }
        }else
        {
            for (int month = 1; month < 13; ++month)
            {
                switch (month)
                {
                    case 1: case 3: case 5: case 7: case 8: case 10: case 12:
                        for (int day = 1; day < 32; ++day)
                        {
                            d[i].year = year;
                            d[i].month = month;
                            d[i++].day = day;
                        }
                        break;
                    case 4: case 6: case 9: case 11: 
                        for (int day = 1; day < 31; ++day)
                        {
                            d[i].year = year;
                            d[i].month = month;
                            d[i++].day = day;
                        }
                        break;
                    case 2:
                        for (int day = 1; day < 29; ++day)
                        {
                            d[i].year = year;
                            d[i].month = month;
                            d[i++].day = day;
                        } 
                        break;
                }
            }
        }
        if( i > 366) break;
    }
}
Last edited on
Topic archived. No new replies allowed.