1

This code is used to get n dates and sort them in the ascending order. I think the getdates function is not working properly.

main function implementation:

#include <stdio.h>
#define max 30
int leapyear(int year);/*to check whether it's leap year or not*/
int dater(int x);/*to find the date from a month begin with the beginning of the year */
void getdates(int f);/*get the date in put*/
int *caldays(int p);
int n, i, q;
int t, d, leap;
int day[30];
int month[30];
int year[30];
char ca[30];
char cb[30];
int dd[30];
int da[30];
int j, a, x;
char c1, c2;
int dayn, monthn, yearn;

int main()
{
     printf("Please enter the number of dates and Press ENTER to finish\n");
     /*get the numbers of dates*/
     scanf("%i", &n);
     printf("You have entered %i \n", n);
     getdates(n);
     printf("end");

    for (i = 0; i <= n-1; i++)
    {
        printf("end");
        while (day[i] < 1 || day[i] > 31 || month[i] < 1 ||
            month[i] > 12 || year[i] < 1 || year[i] > 10000)
        {
            fprintf( stderr, "The date entered is not right, please enter again\n");
            printf("Please enter the number of dates and Press ENTER to finish\n");
            /*get the numbers of dates*/
            scanf("%i", &n);
            printf("You have entered %i \n", n);
            getdates(n);
        }

        while (month[i] == 2 && day[i] > 29)
        {
            fprintf( stderr, "The date entered is not right, please enter again\n");
            printf("Please enter the number of dates and Press ENTER to finish\n");
            /*get the numbers of dates*/
            scanf("%i", &n);
            printf("You have entered %i \n", n);
            getdates(n);
        }

        while ((month[i] == 4 ||month[i] == 6 || 
                month[i] == 9 ||month[i] == 11) && day[i] > 30)
        {
            fprintf( stderr, "The date entered is not right, please enter again\n");
            printf("Please enter the number of dates and Press ENTER to finish\n");
            /*get the numbers of dates*/
            scanf("%i", &n);
            printf("You have entered %i \n", n);
            getdates(n);
        }
    }
    /*3 while loops are used to give msg and re-enter again when get an error input*/

    caldays(n);
    for (i = 0; x < n; ++i)
    {
        for (j = i + 1; j < n; ++j)
        {
            if (dd[i] > dd[j])
            {
                a =  dd[i];
                dd[i] = dd[j];
                dd[j] = a;
            }
            /*sort the days in asending order in days array*/
        }
    }
    printf("The %i dates in ascending order are\n", n);
    for (i = 0; i < n; ++i)
        printf("%d%c%d%c%d\n", day[i], ca[i], month[i], cb[i], year[i]);
    /*print all the date in ascending order*/
}


 /*find out wheter it's leap year or not*/
int leapyear(int year)
{
    if (year % 4000 == 0)
    {
        return 1;
    } 
    else if (year % 400 == 0)
    {
       return 1;
    } 
    else if (year % 40 == 0)
    {
       return 1;
    } 
    else if (year % 4 == 0)
    {
       return 1;
    } 
    else
    {
       return 0;
    }
}


/*find out the days for the month input*/
int dater(x)
{ 
    int y=0;
    switch(x)
    {
        case 1: y=0; break;
        case 2: y=31; break;
        case 3: y=59; break;
        case 4: y=90; break;
        case 5: y=120; break;
        case 6: y=151; break;
        case 7: y=181; break;
        case 8: y=212; break;
        case 9: y=243; break;
        case 10:y=273; break;
        case 11:y=304; break;
        case 12:y=334; break;
        default: fprintf( stderr, "the value entered is not right\n");
    }
    return y;
}


void getdates(int f)
{ 

    for(i=0;i<f;i++)
    {
        q = i+1;
        printf("Please Enter %i dates (DD/MM/YYYY or DD-MM-YYYY) and
                Press ENTER to finish each one\n", n);
        scanf("%d%c%d%c%d", &dayn, &c1, &monthn, &c2, &yearn);
        printf("You have entered %i date %i%c%i%c%i\n", q, dayn, c1, monthn, c2, yearn);
        day[i] = dayn;
        month[i] = monthn;
        year[i] = yearn;
        ca[i] = c1;
        cb[i] = c2;
    }
    return;
}


int *caldays(int p)
{
    for (i=0; i < p-1; i++)
    {   
        leap = leapyear(year[i]);
        t = 0;
        if(leap == 1)
        {
            t++;
        }
        /*if there is a leap add one day */
        /*find for the days for month entered*/
        d = dater(month[i]);
        /* find out the total days from the date entered begin with 0 days*/
        d = d + dayn + t + (yearn * 365);
      dd[i] = d;/*put all the days in an array*/
    }  
    return dd;
}
Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
Savitor
  • 23
  • 6
  • if possible, could you provide a minimal example that displays the problem? This will help you understand it, as well as others who may want to help. –  Mar 22 '17 at 00:14
  • I see you are a beginner. So please read https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work. Also you should not edit the question after it has been answered satisfactorily. – Shridhar R Kulkarni Mar 22 '17 at 10:25

1 Answers1

2

The mistake is instead of

caldays(n);
for (i = 0; x < n; ++i)

in main function, it should be

caldays(n);
for (i = 0; i < n; ++i)

In getdates() and wherever required, use "%d %c %d %c %d" instead of %d%c%d%c%d.

As soon as you get the program working,I suggest you to post it for review on https://codereview.stackexchange.com/. The experienced people there will help you tear apart your code and make your life easier for better understanding of readability of code.

I recommend you to read Difference between format specifiers %i and %d in printf.

While posting question do follow MCVE from next time if possible.

Community
  • 1
  • 1
Shridhar R Kulkarni
  • 6,653
  • 3
  • 37
  • 57
  • Thank you so much for your time, just one more question, the logic part is still not working, it just give the order which i entered. i'd like it to give me ascending order of the dates. Could help me fix the problem please? thank you – Savitor Mar 22 '17 at 09:17
  • http://stackoverflow.com/help/someone-answers. And Instead of editing the same question add it as a different question please. Editing the same question might invalidate the comments and answers by the community . – Shridhar R Kulkarni Mar 22 '17 at 10:14