Home > Back-end >  What is the reason of wrong output?
What is the reason of wrong output?

Time:02-08

I was trying to write a code in c which converts a day month year format to dayRD day of Month Year

For example: 1 12 2020 must be converted like 1st day of december 2020.

I wrote the code as ı write down the page but the output is not the output that ı want.

#include<stdio.h>
int main() {
int day,month,year;
printf("Please enter date in a format of day month year");
scanf("%d%d%d",&day,&month,&year);

int dayconvert(day) {
    switch (day) {
        case 1:printf("%dst day of",day);
        break;
        case 2:printf("%dnd day of",day);
        break;
        case 3:printf("%drd day of",day);
        break;
        case 4:printf("%dth day of",day);
        break;
        case 5:printf("%dth day of",day);
        break;
        case 6:printf("%dth day of",day);
        break;
        case 7:printf("%dth day of",day);
        break;
        case 8:printf("%dth day of",day);
        break;
        case 9:printf("%dth day of",day);
        break;
        case 10:printf("%dth day of",day);
        break;
        case 11:printf("%dst day of",day);
        break;
        case 12:printf("%dnd day of",day);
        break;
        case 13:printf("%drd day of",day);
        break;
        case 14:printf("%dth day of",day);
        break;
        case 15:printf("%dth day of",day);
        break;
        case 16:printf("%dth day of",day);
        break;
        case 17:printf("%dth day of",day);
        break;
        case 18:printf("%dth day of",day);
        break;
        case 19:printf("%dth day of",day);
        break;
        case 20:printf("%dth day of",day);
        break;
        case 21:printf("%dst day of",day);
        break;
        case 22:printf("%dnd day of",day);
        break;
        case 23:printf("%drd day of",day);
        break;
        case 24:printf("%dth day of",day);
        break;
        case 25:printf("%dth day of",day);
        break;
        case 26:printf("%dth day of",day);
        break;
        case 27:printf("%dth day of",day);
        break;
        case 28:printf("%dth day of",day);
        break;
        case 29:printf("%dth day of",day);
        break;
        case 30:printf("%dth day of",day);
        break;
        
            
    }
    
}

int monthconvert(month) {
    switch (month) {
        case 1:printf("January");
        break;
        case 2:printf("February");
        break;
        case 3:printf("March");
        break;
        case 4:printf("April");
        break;
        case 5:printf("May");
        break;
        case 6:printf("June");
        break;
        case 7:printf("July");
        break;
        case 8:printf("August");
        break;
        case 9:printf("September");
        break;
        case 10:printf("October");
        break;
        case 11:printf("November");
        break;
        case 12:printf("December");
        break;
        
    }
}

printf("%d %d %d",dayconvert(day),monthconvert(month),year);

}

The output is January1st day of10 2020 when we enter 1 12 2020 values to day,month,year

CodePudding user response:

at the end you do this

 printf("%d %d %d",dayconvert(day),monthconvert(month),year);

there is no guarantee what order these function calls are made. In fact you have shown that printf calls monthconvert, then dayconvert.

Monthconvert prints out the month name, then dayconvert prints out the day name.

CodePudding user response:

Use this (updated version):

#include <stdio.h>

void dayconvert(unsigned int input)
{
    static const char* prefixes[3] = {"st", "nd", "rd", "th"};
    static const char selection[32] = {0,0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,0,1,2,3,3,3,3,3,3,3,0};

    printf("%u", input);
    printf("%s", prefixes[selection[input]]);
    printf(" day of ");
}

void monthconvert(unsigned int input)
{
    static const char* month_names[12] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

    printf("%s", month_names[input-1]);
}

int main()
{
    unsigned int day, month, year;

    printf("Please enter date in a format of day month year:\n");
    scanf("%u%u%u", &day, &month, &year);

    dayconvert(day);
    monthconvert(month);
    printf(" %u", year);
}

CodePudding user response:

#include <stdio.h>

int main(void)
{
  int day,month,year;
  printf("enter date:");
  scanf("%d %d %d",&day,&month,&year);
  printf(day == 1
         ? "%dst%s"
         day == 21
         ? "%dst%s"
         day == 31
         ? "%dst%s"
         : day == 2
         ? "%dnd%s"
         : day == 22
         ? "%dnd%s"
         : day == 3
         ? "%drd%s"
         : "%dth%s",
         day,
         " day of ...\n");
  /* here print the month   year */
  return 0;
}

There are a few special cases. For the rest, the pattern is the same.

When you call printf, the first argument you pass is the format string. The format is precomputed by the conditional operator, depending on the input. The common format string is " DAY SPECIAL CONTINUATION ". This format string is decided based on the day. Next, you pass the format arguments to printf, that will use them to fill in the string. My common format uses 2 arguments "%d SPECIAL %s".

Other way you can do the very same thing is so:

  printf("%d%s day of ...\n",
         day,
         day == 1
         ? "st"
         day == 21
         ? "st"
         day == 31
         ? "st"
         : day == 2
         ? "nd"
         : day == 22
         ? "nd"
         : day == 3
         ? "rd"
         : day == 23
         ? "rd"
         : "th");

You can follow the same principle to print the name of the month given an integer. For brevity, I do not repeat the pattern.

  •  Tags:  
  • Related