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

looks right to me this one

The problem here is not the code, but the way we enter the date.

After running the program and being asked "Enter date (mm/dd/yyyy):

Enter the date all together and exactly this way: 10/20/1999

and not separated: 10
20

Hope that helps.

1 Like

heres my code that I’ve done, not sure if anyone still needs it but it’ll be here anyway for anyone who needs it in the future. you don’t need a switch case for the day, just need to print whatever that has been inputted by the user, this goes for the year as well.

#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

printf(" %d",day);

// Print the suffix for a given day

switch(day){
case 1: case 21: case 31:
printf(“st”);
break;
case 2: 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;
}

2 Likes

Thanks for the above …really helped. Query why it takes us to teach each other all this suddenly new stuff rather than CodeAcademy…is Udemy better? Anyway tips for my learning … and I am spelling out the basics / reminders from previous lessons because isn’t that what’s needed at this stage for us newbies?

  1. In the bash screen: $ gcc date.c [press enter] $ ./a.out [press enter] when you get asked in the instructions. This will run the code so that you get “Enter date (mm/dd/yyyy):”. This code is already given to you on the left hand side of the screen, and prompts the user to input the dates. Once inputted, the code then picks the 3 variables up and then puts them through the rest of the code that you are being asked to draft via the “pivot” part of the code - see 2. below - which links these variable to what you draft below. In previous lessons, we changed the variables in the left hand side of the screen - where the coding is - at the top after we declared our variables. In this lesson, you will test your coding by changing the variables (month, day, year) on the right hand (bash) screen.
  2. For your coding, ignore “scanf(”%d /%d /%d", &month, &day, &year);". My understanding is this (hopefully confirmed in a future lesson - I have never done any coding whatsoever before); this is the bit that captures what the user (you) has inputted on the bash side. So when prompted "Enter date (mm/dd/yyyy): … " and you type “01/01/2021”, the first 01 for the month is captured by the “scanf(”%d / …, &month … " part of that code, and this is what your coding then picks up later on i.e. what your coding will change into “January”. That’s why I called it the “pivot” section above. The variables are still “day”, “month” and “year” as declared at the top of the coding section as well as the sections you are being asked to draft. The variables to use in your code are NOT “&day”, “&month” or “&year”.
  3. You are being asked to start coding at the “// Print the month” section. The “switch (month) {
    case 1:
    printf(“January”);”
    The coding / computer will pick up that “case 1:” means '01" in the month section of the input. How it knows this I don’t know … underlying code I image. So “case 01:” doesn’t seem the way to go here or rather then 0 might be redundant.
  4. Similarly with the day, the computer/coding knows that “case 31:” means “31” input for the “day” section. And also, when you have “cases” you can lump them together in one line if the code that comes after is true for them all e.g. “case 1: case 21: case 31:”
  5. Check your colons and semi-colons (always):;
  6. Press “save” before you go and run things on bash…if you get lots of errors then the error lists looks really daunting at first but it pays to go through and ID things. If you need to refresh your bash screen, just press the x at the top where it says “x bash”, and start again. Your saved code will still be saved. I do this once I’ve corrected a few errors on the list and see what is left still to sort out.
  7. You do not need { } brackets for the (" %d",day); … or printf(“%d\n”,year).