C: "Dates and Switches" task; random numbers appear

Hello,

I am on the following task at the moment: https://www.codecademy.com/courses/learn-c/projects/dates-and-switches-c . The final goal: the user prints, for example: “01/01/2021” and the output is: “January 1st, 2021.”

But my code outputs: “January 326441024th, 32767.” I am not sure from where the numbers come from.

The code:

#include <stdio.h>

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

  // Standard date form
  printf("Enter date (mm/dd/yyyy): ");
  // Split the user input into 3 variables for the date
  // Don't worry about the `scanf()` below, you'll learn more about these later!
  scanf("0%d, /%d, /%d", &month, &day, &year);

  // Print the 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;
    default: 
    printf("Not a month");
    break;
  }

  // Print the day
 printf(" %d", day);

// Print the suffix for a given day

switch (day)
  { 
case 1: case 11: case 21: case 31:
    printf("st, ");
    break;

    case 2: case 22:
    printf("nd, ");
    break;
    case 3: 
    printf("rd, ");
    default: 
    printf("th, "); 
    break;

  }

  // Print the year
  printf("%d.\n", year);
  
  
  return 0;
}

I am not that familiar with scanf, so maybe the issue is something in there… could you give a hint or a suggestion?

Hello.

Format string in first argument of scanf() must be exactly in the same format which user will use for input.

With format 0%d, /%d, /%d and input 01/01/2021 it works like this:

  1. The first character of 01/01/2021 is zero 0? Ok, skip it. Remaining string is 1/01/2021.
  2. We want integer at the start of 1/01/2021. Put it into month variable. Ok, got 1. Remaining string is /01/2021.
  3. Next character of /01/2021 is comma ,? Oh, there is no comma! Stop processing.

So you will get only month from entered date. Other variables will contain random integers.

Try to fix your format string.

Additional info about scanf: https://www.ibm.com/docs/en/i/7.3?topic=functions-scanf-read-data

Hope, I could help.

2 Likes

Thank you, it helped. I have removed the zero and edited the string.

For the same question, my code was
#include <stdio.h>

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

// Standard date form
printf("Enter date (mm/dd/yyyy): ");
// Split the user input into 3 variables for the date

// Don’t worry about the scanf() below, you’ll learn more about these later!
scanf(" %d, %d, %d ", &month, &day, &year);

// Print the month
switch (month) {
case 1:
printf(“January\n”);
break;

case 2:
printf(“February\n”);
break;
case 3:
printf(“March\n”);
break;
case 4:
printf(“April\n”);
break;
case 5:
printf(“May\n”);
break;
case 6:
printf(“June\n”);
break;
case 7:
printf(“July\n”);
break;
case 8:
printf(“August\n”);
break;
case 9:
printf(“September\n”);
break;
case 10:
printf(“October\n”);
break;
case 11:
printf(“November\n”);
break;
case 12:
printf(“December\n”);
break;
default:
printf(“Not in the range”);
break;

}

// Print the day
printf(" %d",day);

// Print the suffix for a given day
switch (day) {
case 1: case 11: case 21: case 31:
printf(“st ,”);
break;
case 2: case 12: case 22:
printf(“nd ,”);
break;
case 3: case 23:
printf(“rd ,”);
break;
default:
printf(“th”);
break;

}

// Print the year
printf(“, %d.\n”,year);

return 0;
}

I am getting the output as January
904304256th, 32767.

can someone please tell me where did I go wrong?

1 Like

This is my solution. I found Step 6 odd and confusing, so I did it long hand.
I also opted to add the comma after the month print statement
That left the print statement for year a bit cleaner

#include <stdio.h>

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

// Standard date form
printf("Enter date (mm/dd/yyyy): ");
// Split the user input into 3 variables for the date

// Don’t worry about the scanf() below, you’ll learn more about these later!
scanf(“%d /%d /%d”, &month, &day, &year);

// Print the 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;

}

// Print the day

// Print the suffix for a given day
switch (day) {
case 1:
printf(" 1st, “);
break;
case 2:
printf(” 2nd, “);
break;
case 3:
printf(” 3rd, “);
break;
case 4:
printf(” 4th, “);
break;
case 5:
printf(” 5th, “);
break;
case 6:
printf(” 6th, “);
break;
case 7:
printf(” 7th, “);
break;
case 8:
printf(” 8th, “);
break;
case 9:
printf(” 9th, “);
break;
case 10:
printf(” 10th, “);
break;
case 11:
printf(” 11th, “);
break;
case 12:
printf(” 12th, “);
break;
case 13:
printf(” 13th, “);
break;
case 14:
printf(” 14th, “);
break;
case 15:
printf(” 15th, “);
break;
case 16:
printf(” 16th, “);
break;
case 17:
printf(” 17th, “);
break;
case 18:
printf(” 18th, “);
break;
case 19:
printf(” 19th, “);
break;
case 20:
printf(” 20th, “);
break;
case 21:
printf(” 21st, “);
break;
case 22:
printf(” 22nd, “);
break;
case 23:
printf(” 23rd, “);
break;
case 24:
printf(” 24th, “);
break;
case 25:
printf(” 25th, “);
break;
case 26:
printf(” 26th, “);
break;
case 27:
printf(” 27th, “);
break;
case 28:
printf(” 28th, “);
break;
case 29:
printf(” 29th, “);
break;
case 30:
printf(” 30th, “);
break;
case 31:
printf(” 31st, ");
break;
}

// Print the year

printf(“%d.\n”, year);

return 0;
}

1 Like

Here the solution of the problem of the day who don’t finish by “st”, “nd” or “rd”

default:
  printf("%dth ", day);
  break;
1 Like

1 Like

remove the commas and spaces from the scanf(), it worked for me

1 Like

switch(day) {
case 1: case 21: case 31:
printf(" %dst, “, day);
break;
case 2: case 22:
printf(” %dnd, “, day);
break;
case 3: case 23:
printf(” %drd, “, day);
break;
default:
printf(” %dth, ", day);
break;

this way is easier

1 Like