C++ Switch Statement Compiling Errors

Hey everyone, I’m still working on a school project that’s due tomorrow. Basically making a day of the year calculator using C++. The only input is the date (mm-dd-yyyy). I managed to figure out how to calculate leap years, but now I’m having a hard time with my switch statement. It’s basically just assigning however many days to each month. I’m probably formatting it wrong, but I don’t understand the compiler errors?! I’d appreciate any assistance and or guidance!

Here’s my code:

#include <iostream>

using namespace std;

bool isLeapYear(int);
int main() {
    // Write your main here
    int mo, day, yr, days, dayNo = 1;
    int feb = 28;
    
    cout << "Enter the date: " << endl;
    cin >> mo >> day >> yr;
    cout << mo << endl;
    cout << day << endl;
    cout << yr << endl;
    
    switch(mo){
        case '1' :
        case '3' :
        case '5' :
        case '7' :
        case '8' :
        case "10" :
        case "12" :
            int days = 31;
            cout << days;
            break;
        case '4' :
        case '6' :
        case '9' :
        case "11" :
            int days = 30;
            cout << days;
        case '2' :
            if (isLeapYear(yr) > 0)
                feb++;
            break;
    }
    cout << days;
    /*if (isLeapYear(yr) > 0)
        feb++;*/
    cout << dayNo;
    return 0;
}

bool isLeapYear(int yr)
{
    if ((yr % 4 == 0 && yr % 100 != 0) || yr % 400 == 0)
        return true;
    else
        return false;
}

If switches are new to you then don’t use start using them at the same time as you’re doing other unfamiliar things.
Write less code before you compile if you’re not willing and able to deal with all errors at once. Write a very small part, compile it, check its behavior.

Switches are also something that people love to show beginners even though they provide nothing new (aside from a bunch of things that they probably don’t think you should be using anyway)
So if you use if-statements instead then you have one less thing to worry about.

If you have example code for what you’re doing then you can also start by running the example and modifying it incrementally until it suits your needs.

Also wanna point out that you’re saying “the compiler errors” but not showing them or asking anything about them which leaves the problem description at around “did you read them?” (not convincing that you’re stuck, impossible to tell what information you’re missing)

In any case, the answer to it all is probably that you should leverage things that you do know to build what you need instead of trying to debug something that you don’t have control over.

Okay, I’m back from work and I managed to fix it just before I left, turns out the main problem was that I wasn’t using brackets for all the statements in my switch statement (duh) and I forgot some break statements, and I added a default statement. If I remember correctly I think you can get away without brackets if you only have one statement to execute but I could be wrong so…:sweat_smile:

    switch(mo){
        case 1 :
        case 3 :
        case 5 :
        case 7 :
        case 8 :
        case 10 :
        case 12 :
            {
            int days = 31;
            cout << days;
            break;
            }
        case 4 :
        case 6 :
        case 9 :
        case 11 :
            {
            int days = 30;
            cout << days;
            break;
            }
        case 2 :
            {
            if (isLeapYear(yr) > 0)
                days = 29;
            else
                days = 28;
            cout << days;
            break;
            }
        default:
            {
                cout << "Invalid Month." << endl;
            }
    }

Cases may have multiple statements (you wouldn’t be able to break and do something else if you could only have one statement so you’ve probably already shown this to yourself)
The cases are labels that program control can jump to, and then execution continues as usual, it is a slightly more structured (less than you might think) version of goto. Breaks merely jump to after the switch. Can you declare a variable twice in the same scope? That was likely one of your error messages.

Another was probably about mixing up types.

yeah i was going to post the error messages but it wouldn’t let me copy/paste so that was fun but yeah i think those were the errors:joy:

I’d be inclined to do this instead of your switch:

int daysOfMonth(int month, int year) {
  unordered_set<int> months31 = {1, 3, 5, 7, 8, 10, 12};
  if (month == 2) {
    return isLeapYear(year) ? 29 : 28;
  }
  return months31.count(month) ? 31 : 30;
}

Or populate an array with the days of each one:
(and by array I mean vector, it makes no difference here but I’ll generally prefer it as a saner default if I have no reason to care about the difference)

vector<int> months = { 31, 28, 31, 30, ... }