Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7610493
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T01:25:51+00:00 2026-05-31T01:25:51+00:00

I have the user input a date, e.g. ‘January 10 12’ signifying the month,

  • 0

I have the user input a date, e.g. ‘January 10 12’ signifying the month, day and year. It works fine if the month is a two digit number, however if the user enters ‘January 12 01’ or any number with 0 in the front of it I just get ‘January 12 1’ if the month was 01 or ‘January 12 0’ if the month was 00 instead of what I input. Is there some form of formatting going wrong?

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

typedef int (*compfn)(const void*, const void*);

struct date
{
    int month;
    int day; //The day of the month (e.g. 18)
    int year; //The year of the date    
};

char* months[]= {
   "January", "February",
   "March", "April",
   "May", "June",
   "July", "August",
   "September", "October",
   "November", "December"};


int getMonth(char tempMonth[])
{
    if(strcmp(tempMonth, months[0]) == 0) return 0;
    if(strcmp(tempMonth, months[1]) == 0) return 1;
    if(strcmp(tempMonth, months[2]) == 0) return 2;
    if(strcmp(tempMonth, months[3]) == 0) return 3;
    if(strcmp(tempMonth, months[4]) == 0) return 4;
    if(strcmp(tempMonth, months[5]) == 0) return 5;
    if(strcmp(tempMonth, months[6]) == 0) return 6;
    if(strcmp(tempMonth, months[7]) == 0) return 7;
    if(strcmp(tempMonth, months[8]) == 0) return 8;
    if(strcmp(tempMonth, months[9]) == 0) return 9;
    if(strcmp(tempMonth, months[10]) == 0) return 10;
    if(strcmp(tempMonth, months[11]) == 0) return 11;
}

int sortDates(struct date *elem1, struct date *elem2)
{
    if ( elem1->year < elem2->year)
        return -1;
    else if ( elem1->year > elem2->year )
        return 1;


    /* here you are sure the years are equal, so go on comparing the months */

    if ( elem1->month < elem2->month )
        return -1;
    else if ( elem1->month > elem2->month )
        return 1; 

    /* here you are sure the months are equal, so go on comparing the days */

    if ( elem1->day < elem2->day )
        return -1;
    else if ( elem1->day > elem2->day )
        return 1; 
    else
        return 0;

}

main()
{
    int n;
    int i;
    char tempMonth[255]; //Used to store the month until checked

    scanf("%d", &n);

    struct date *list;

    list = (struct date *)malloc((n * sizeof(struct date)));

    for(i = 0; i < n; i++)
    {
        scanf("%s %d %d", tempMonth, &list[i].day, &list[i].year);
        list[i].month = getMonth(tempMonth);
    }

    qsort(list, n, sizeof(struct date), (compfn)sortDates);

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

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T01:25:53+00:00Added an answer on May 31, 2026 at 1:25 am

    If you want to always output at least two digits, with zeros in front, use the %02d format string instead of %d.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a dataset with a column containing date in DAY-MONTH-YEAR TIME format. That
I have 3 date / time input boxes on a form and the user
I have a homework assignment which asks to have the user input a date
I have a form which captures a date that the user input in a
Hi I have a user input form which takes date in 2011-05-12 format. I
I would like to have the user input a date, something like: date =
I have a form containing two text boxes for user input. Both text boxes
What I'm trying to do is have the user input a number, and then
We have a PHP/MYSQL application that collects user input, including special characters like ø,ü,ñ,
I have loop designed to validate the user input on a question, it was

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.