Help in date and switches project in c skill path course

I’m stuck in date and switches project on c skill path course is anyone who finished this project if so please share it so that i can see it for my reference and solve the issue for my self.

Much easier for members to help if they can see the actual exercise or project. Please post a link to the landing page so we can read the narrative and instructions and try our own hand at a solution.

1 Like

Link to the project page as you asked man https://www.codecademy.com/paths/c/tracks/c-control-flow-sp/modules/conditionals-c-sp/projects/dates-and-switches-c

1 Like

This is the output we’re going after:

Enter date (mm/dd/yyyy): 1/2/2024
January 2nd, 2024

We will need a switch for the month names, and a switch for the day suffixes. Below I’ve written an if else for you to model your switch from. The month names I’ll leave for you to fashion. If you need help getting started, let us know.

  if (day % 10 == 1) {
    printf("%s, ", "st");
  }
  else if (day % 10 == 2) {
    printf("%s, ", "nd");
  }
  else if (day % 10 == 3) {
    printf("%s, ", "rd");
  }
  else {
    printf("%s, ", "th");
  }

Note this is not entirely correct. There is no 11st, 12nd or 13rd. We want output like this:

Enter date (mm/dd/yyyy): 4/11/2024
April 11th, 2024

6 hours later

Don’t leave us if you’re stuck. Should you succeed all the steps, give us a thumbs up that all is well, else feel free to post code that is giving you trouble. This is a fun project once we get into it. Switches galore.

The fun part for me is the validation once the basic mechanics are in place. This project is coming together from the very basement with minimal logic and quite a bit of spaghetti, but that is not a bad thing. It is, imho, the best place to start: Scratch code, naïve as possible. For me, it is the only way I can work in C++ since it is a foreign language to me. I’ve been leveraging my JS kit on this project. When you’re ready to see some of what is down the validation rabbit hole, give us a bell.

Enter date (mm/dd/yyyy): 13/33/1700
unknown 0th, 1973

Thanks for the help man

1 Like
default: printf("%s ", "unknown"); break;

Using only INT case parameters, what would cause the switch to default, and what would be in candidate range?

How much code do you need to see, to set your clock by? Is the following in your wheelhouse? len is defined initially as INT

  switch(month) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12: len = 31; break;
    case 2: len = year % 4 == 0 && year % 100 != 0 || year % 400 == 0 ? 29 : 28; break;
    case 4:
    case 6:
    case 9:
    case 11: len = 30; break;
    default: len = 0;
  }
  if (day > len) {
    day = 0;
  }
Enter date (mm/dd/yyyy): 2/29/2000
February 29th, 2000

Examine the validation for case 2: There are three questions being asked. Can you discern what the net outcome should be, and why? This is something to dig into, rather than shy away. It’s times like this when we get to just play with stuff. Who knows what we learn from that? And, it’s fun.

This code i think i created it by following instructions but i’m facing an issue here as you can see the issue is how to link the suffix to day?

#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(”%d nd", day);
case 3: case 23:
printf(“%d rd”, day);
break;
case 4: case 5: case 6: case 7: case 8: case 9: case 10: case 11: case 12: case 13: case 14: case 15: case 16: case 17: case 18: case 19: case 20: case 24: case 25: case 27: case 28: case 29: case 30:
printf(“%d th”, day);
break;
default:
printf(“Invalid”);
}

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

return 0;
}

Output:
Enter date (mm/dd/yyyy): 10/24/2003
October 24, 24 th2003

Clearly, day is printing twice. In your suffix switch, only print the suffix, not day, and make sure there is no space between the number and the suffix, and that the comma follows the suffix, followed by a space, then the year.

Thanks again man the final output is:
Enter date (mm/dd/yyyy): 6/12/2024
June 12th, 2024

1 Like

Excellent! Before moving on from this project, have you given any thought to how we might validate the inputs, even passively keep? We can use my example posted above,

Enter date (mm/dd/yyyy): 13/33/1700
unknown 0th, 1973

and,

Enter date (mm/dd/yyyy): 2/29/2000
February 29th, 2000

The switch to detect the correct month length passively returns 0 as a default, rather than raising an alarm. The displayed results are a clear indication that invalid data was inputted. Similarly the month switch can passively return a default, such as above, ‘unknown’.

Note above how the logic even validates the length of February. 2000 was a leap year, even though most century beginning years are not leap years. As long as the data input is valid, the code goes completely along with it.

The last form of passive validation is the year and what arbitrary limits we want to place on that input. We haven’t always used the same calendar so don’t want to go too far back historically, and what of the future beyond our lifetime? Seems rather pointless to explore. My code is arbitrarily set at roughly 50 years before (1973) and 50 years after (2073) and my conditional yields 1973 by default.

For the record, 1973 was chosen as the lower year because the clock in our computer has that as the starting date and time. Many built-in date/time objects can return the number of milliseconds since 00:00:00.001 of January 1st, 1973, or maybe a millisecond before. Who’s counting, at this point?

Bottom line, passive validation is a really good place to start when examining the range of inputs we wish our program to accept. From there we move up to either notifying the user, or more aggressively, rejecting the input from the scanf() before we feed the inputs to our logic, and looping the user through that phase as long as their inputs don’t jive. Such a loop would need an escape of some sort. The empty string would suffice.

This is a perfect project for delving into this stuff, and at the same time looking for ways to improve our logic or shorten our code or even refactor. Use this opportunity to make it easier going forward having taken the pause to explore where you are, rather than pushing on. It will pay off big time as you progress.

Definitely I was thinking myself that my code is not that good even though it gives the output i want, it has it’s flaws like the code is very long and also the cases are used like many times it consumes time and it is not a good program. Maybe I need to implement other logics and validate the input before processing it, then the program will be short and it will perfectly.

Make incremental changes, not major ones. Other logic is for down the road on a new project around the same concern, but starting from a different perspective. This is good logic to study and really familiarize yourself with. Get comfortable with what you are coming up with and give yourself some credit if it works, and have confidence enough you will make it work, if it doesn’t. That is what makes this sort of waystation a perfect place to pause.

Bottom line, if your code works, make sure you have a good reckoning on how and why it works. That is where we get the blueprints for change. With every change, expect the same results you got from the earlier iteration.

We’re not wanting to throw the baby with the bathwater, or even toss the bathwater. Just look at it closer. Can you explain the code to yourself, or better still, to me, a co-learner? It is in trying to explain code to one’s self that we stumble across any cracks or weaknesses, or ways to simplify, reduce and improve the code. We never set out to write perfect code. Like the stuff we’ve just been working on, we set out to write code that works. The real work begins at that point. We don’t seek perfection, as much as reliability.

 if (day > len) {
    day = 0;        //  passive validation
  }
  printf("%d", day);
  switch(day) {
    case 1:
    case 21:
    case 31: printf("%s, ", "st"); break;
    case 2:
    case 22: printf("%s, ", "nd"); break;
    case 3:
    case 23: printf("%s, ", "rd"); break;
    default: printf("%s, ", "th");
  }
  printf("%d\n", year);  

Seven days are special cases; the rest are default and do not need to be cases, at all.

Thanks for the clarification that there is no perfect code rather we write the code that works.

1 Like

This has already reached a sort of leveling off place, hence the zealousness toward using it as a pause point. It can also serve as mini-platform to explore more ideas, such as including the day of the week in the results.

It won’t be easy, but will be helped along by two basic structures, each the inverse of the other. Below is code written in Python, which I leave you to port over to C++. for fun and for learning/practice.

>>> wd = 'Sunday Monday Tuesday Wednesday Thursday Friday Saturday'
>>> wt = dict(enumerate(wd.split()))
>>> wt
{0: 'Sunday', 1: 'Monday', 2: 'Tuesday', 3: 'Wednesday', 4: 'Thursday', 5: 'Friday', 6: 'Saturday'}
>>> wl = {wt[k]:k for k in wt}
>>> wl
{'Sunday': 0, 'Monday': 1, 'Tuesday': 2, 'Wednesday': 3, 'Thursday': 4, 'Friday': 5, 'Saturday': 6}
>>> wl['Thursday']
4
>>> wt[4]
'Thursday'
>>> 

Once you port these two objects over to C, we’ll be able to explore how to calculate the number of days since January 1st, 1973. We’ll need to know that. In our hip pocket is that it was a Monday.

See you on the flip side? This is fun stuff to explore, and the right time to do it, imho. Take this date stuff very seriously. It factors into a lot of code scenarios, including encryption. This is a great chance to build a foundation around the concept, while the pressure is off.

1 Like

Funnily enough, when I did a quick examination of the modulo it turns out the first day of the date counting system is modulo = 0 which means the week doesn’t start on Sunday, but on Monday, by this arithmetic.

>>> from datetime import date
>>> a = date(1973, 1, 1)
>>> b = date(2024, 6, 17)
>>> (b - a).days % 7
0
>>> 

Both dates are a Monday, we may conclude given that Jan. 1st, 1973 was a Monday.

That means our data above is incorrect and needs to be adjusted so that Sunday is at the end of the week, not the beginning. My mistake, my bad.

You have the structures to play with directly in C. It may cross my radar to pursue this further, but with your interest, that is likely to happen sooner, rather than later, or not at all, which would be more the pity.

1 Like

Thanks for the help man i appreciate it, I was moved forward in the course also doing the full stack engineer career path but I will finish this task as soon as I can because of the motivation you gave me and also If I were to stuck somewhere in the process you are here to help.

1 Like