-2

I should be getting a day of the week. 0 should be monday and 6 should be sunday.I don't know if it's run time error or what. I have tried everything that I can think of.

#include <iostream>

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!";
    }
}

int main()
{
  cout << getDayOfWeek(1);
    return 0;
}
dewaffled
  • 2,850
  • 2
  • 17
  • 30
Real G
  • 37
  • 2
  • 12
    You're not returning anything. Put `return dayName;` after your `switch` statement. – Blaze Aug 20 '19 at 14:46
  • 5
    Or just `return` in each `case` instead of assigning the result to a variable and `break`ing. – François Andrieux Aug 20 '19 at 14:46
  • 5
    btw this is the schoolbook example of why you should pay attention to compiler warnings – 463035818_is_not_an_ai Aug 20 '19 at 14:47
  • 1
    Python? octave? sql? – peeebeee Aug 20 '19 at 14:48
  • This duplicate is wrong. The duplicate is about attempting to switch on a string. But OP is correctly switching on an `int` and assigning to the string in the cases within, his mistake is a different one. – Blaze Aug 20 '19 at 14:52
  • @FrançoisAndrieux See https://stackoverflow.com/q/57532056/10957435: It may be better to return at the end of the function. –  Aug 20 '19 at 15:17
  • @Chipster I don't see how adding a `return` at the end changes whether you also `return` in the `case`s. In fact, that's what the accepted answer in the linked question does. – François Andrieux Aug 20 '19 at 15:23
  • @FrançoisAndrieux I must misunderstand. The accepted answer avoids UB by returning at the end of the function if none in the switch is found. That's all I'm trying to say. –  Aug 20 '19 at 17:01
  • @Chipster I'm assuming your first comment was in response to my first comment. To me, *"It may be better to return at the end of the function"* seems to imply that you are proposing a better alternative to what I propose. What I don't see is why the two are incompatible. It seems to me that the two are orthogonal and unrelated. Whether you `break` or `return` in each `case` doesn't change how you handle the scenario where no `case` is selected. Did you specifically mean to say that `return` after the `switch` is better than a `default`? – François Andrieux Aug 20 '19 at 17:08
  • @FrançoisAndrieux Looking back, I misread your first comment. That is exactly what I meant to say.. –  Aug 20 '19 at 17:10

3 Answers3

8

Your function std::string getDayOfWeek(int dayNum) needs to return a std::string.

You can do that right in your switch statement:

std::string getDayOfWeek(int dayNum){
    switch (dayNum){
    case 0:
        return "Sunday";
    case 1:
        return "Monday";
    case 2:
        return "Tuesday";
    case 3:
        return "Wednesday";
    case 4:
        return "Thursday";
    case 5:
        return "Friday";
    case 6:
        return "Saturday";
    default:
        return "Invalid Day Number!";
    }
}
Paul Evans
  • 27,315
  • 3
  • 37
  • 54
3

I think you want to print Monday in the above given example, but there is no return statement in the getDayOfWeek() function. Please add return dayName; and try again.

Ted Lyngmo
  • 93,841
  • 5
  • 60
  • 108
Neena
  • 51
  • 4
3

You're missing a return value for a non-void function. That's undefined behaviour. Plus your indexing is incorrect if you want 0 to be Monday which your question wording states.

Why not use simply

#include <algorithm> // for std::min
#include <string> // for std::string
const std::string& getDayOfWeek(unsigned dayNum){
    static std::string data[] = {
        "Monday",
        "Tuesday",
        "Wednesday",
        "Thursday",
        "Friday",
        "Saturday",
        "Sunday",
        "Invalid Day Number!"
    };
    return data[std::min(dayNum, 7u)];
}

noting that (i) the returned reference never dangles since the static array is valid for the lifetime of the program, and (ii) an implicit conversion to an unsigned type permits error handling on only one edge?

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • @Bathsheba Nevermind about the `const` thing I mentioned, I was confused. I was thinking `getDayOfWeek` returns a `std::string &` and `data` was `const`, which is the opposite. – François Andrieux Aug 20 '19 at 15:26
  • I'm guessing that it's starting with Sunday to be `struct tm` compatible. – Ted Lyngmo Aug 20 '19 at 15:31
  • `const_cast(getDayOfWeek(0)) = "Legal hack";` unless `data` is made `const`? – Ted Lyngmo Aug 20 '19 at 16:37
  • Thanks a lot everyone. I'm new to programming but I do notice that its such a schoolboy mistake. – Real G Aug 20 '19 at 19:57
  • @RealG when I wrote "schoolbook example" I didnt mean that it is a mistake only schoolboys make. Rather the opposite, it is a mistake I make all the time, I bet it is so common also for other coders that it could appear in a schoolbook as a good example for why warnings are important – 463035818_is_not_an_ai Aug 21 '19 at 12:52