1

Switch statement only works when I change the value between the brackets to 0 in cout << getDayOfWeek(0);. Any number between 1-6 selects the default value "Invalid Day Number". There isn't any issues (no issues found). I tried retyping it from scratch and nothing worked. The code is supposed to print out the day when I enter its number, but it only works with 0. If I put other numbers, it selects the default value.

#include <iostream>

#include <cmath>

#include <string>

#include <cstring>
using namespace std;

    string getDayOfWeek(int dayNum) {
    string dayName;

    switch (dayNum) {
    case 0:
        dayName = "Sunday";
        break;

        switch (dayNum)
    case 1:
        dayName = "Monday";
        break;

       switch (dayNum)
    case 2:
        dayName = "Tuesday";
        break;

        switch (dayNum)
    case 3:
        dayName = "Wednesday";
        break;

        switch (dayNum)
    case 4:
        dayName = "Thursday";
        break;

        switch (dayNum)
    case 5:
        dayName = "Friday";
        break;

        switch (dayNum)
    case 6:
        dayName = "Saturday";
        break;

    default:
        dayName = "Invalid Day Number";
    } 

    return dayName;
}

int main()
{
    cout << getDayOfWeek(5);
    return 0;
}
JVApen
  • 11,008
  • 5
  • 31
  • 67
Aloysius
  • 19
  • 2
  • 3
    Don't repeat `switch (dayNum)` over and over; keep only the first one, remove all the others. As written, your code doesn't do what you think it does. – Igor Tandetnik Jun 28 '20 at 15:30
  • 1
    also don't use [using namespace std](https://stackoverflow.com/questions/1452721/why-is-using-namespace-std-considered-bad-practice) – yaodav Jun 28 '20 at 15:34

3 Answers3

3

You're using the switch statement incorrectly. The code should look like this:

switch(dayNum){
    case 0:
        // do something
        break;
    case 1:
        // do something
        break;
    case 2:
        // do something
        break;
    case 3:
        // do something
        break;
    case 4:
        // do something
        break;
    case 5:
        // do something
        break;
    case 6:
        // do something
        break;
    default:
        // do something
        break;
}

You shouldn't repeat the switch statement over and over again; just keep the first one.

Telescope
  • 2,068
  • 1
  • 5
  • 22
2

You won't need switch (dayNum) inside the switch statement. They will create nested switch statements and leave only case 0 and default in the first switch statement.

Try this:

#include <iostream>

#include <cmath>

#include <string>

#include <cstring>
using namespace std;

    string getDayOfWeek(int dayNum) {
    string dayName;

    switch (dayNum) {
    case 0:
        dayName = "Sunday";
        break;

    case 1:
        dayName = "Monday";
        break;

    case 2:
        dayName = "Tuesday";
        break;

    case 3:
        dayName = "Wednesday";
        break;

    case 4:
        dayName = "Thursday";
        break;

    case 5:
        dayName = "Friday";
        break;

    case 6:
        dayName = "Saturday";
        break;

    default:
        dayName = "Invalid Day Number";
    } 

    return dayName;
}

int main()
{
    cout << getDayOfWeek(5);
    return 0;
}
MikeCAT
  • 73,922
  • 11
  • 45
  • 70
0

You could avoid the switch statement by using an array:

static const char day_names[] =
{
    "Sunday", "Monday", "Tuesday", "Wednesday",
    "Thursday", "Friday", "Saturday"
};

const std::string day_of_week = day_names[dayNum];

The switch statement adds more lines of code to your project, which increases complexity and the probability of defects.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154