-5

For the following code I want to enter day and get cout value. Currently it gives cout value correctly if i enter 0,1,2,3 Exp result:C++ program asks for a day and outputs the label of the day in a week.Please advise how to fix it.

#include <iostream>

using namespace std;

int main()
{

enum days { Sun, Mon, Tue, Wed, Thu,Fri,Sat };
int day;
cout << " Enter a day ";
cin >> day;

switch (day)
{
case 0:
    cout << "Weekend" << endl;
    break;

case 1:
    cout << "Start of work week " << endl;
    break;

case 2:
    cout << "Midweek" << endl;
    break;

case 3:
    cout << "Midweek" << endl;
    break;

case 4:
    cout << "Midweek" << endl;
    break;

case 5:
    cout << "End of work  week" << endl;
    break;

case 6:
    cout << "Weekend" << endl;
    break;

default:
   cout << "Invalid day of the week" << endl;
    return 0;

}
}
Shakil
  • 4,520
  • 3
  • 26
  • 36
xavier
  • 1
  • 1
  • 4
  • 2
    You should be using `switch (day) case Sun:` `case Mon;`, `case Tue:`, shouldn't you? – Ken White Feb 14 '19 at 03:24
  • i tried using case Mon;, case Tue: but its throwing me error for case Mon -Fri as weeknd ,Its good for sat and sun.I am not sure why its showing weeknd for mon -fri?Any help ? – xavier Feb 14 '19 at 03:29
  • Can't give you any help, because I can't see what you tried from where I'm sitting, and I can't see what *throwing me error* means. – Ken White Feb 14 '19 at 03:35
  • I told you how to fix it in my first comment. I don't understand your difficulty. In your `switch`, replace `0` with `Sun`, `1` with `Mon`, `2` with `Tue`, and so forth. It doesn't *throw* anything, and it doesn't break anything. – Ken White Feb 14 '19 at 03:44
  • Ok lets keep it simple .For Mon-Fri also the ouput display is still "Weeknd".I replaced all case 0 to case 7 with case Sun to case Sat.Am I clear ? – xavier Feb 14 '19 at 03:46
  • Yes, except what you say can't happen, and you've still not explained what *throwing me error* means. If it works for `Sun` and `Sat`, it works for the other days also. If it doesn't, then either that's not your real code in the question or you've managed to break it when you made the changes I suggested. – Ken White Feb 14 '19 at 03:48
  • @xavier clarify your purpose please, what do you want specifically – AAEM Feb 14 '19 at 03:50
  • I am sorry.There is no "throwing me error".My bad,Its first time i am using stack.When I enter Mon,-Fri in console.Output is "weeknd" .It shouldnt be right ? – xavier Feb 14 '19 at 03:52
  • attched is screenshot http://prntscr.com/mkumcv – xavier Feb 14 '19 at 03:57
  • Seems fine to me. *shrug* https://ideone.com/zTmgPj If you're expecting to be able to enter the enum value as text you have more work to do to parse that. – Retired Ninja Feb 14 '19 at 04:02
  • I'm voting to close this question as off-topic because it's asking how to do something that cannot be done. – Sid S Feb 14 '19 at 04:03
  • Based on your restrictions I'd guess this is homework. This may fit the restrictions, but it does have a bug in it that I'll leave you to find. Good luck! https://ideone.com/cRybDi – Retired Ninja Feb 14 '19 at 04:29

3 Answers3

0

Please note that:

Each switch action is associated with the value of an integral constant expression (i.e., any combination of character and integer constants that evaluates to a constant integer value).

From the previous statement we see that it OK to use the enum values inside the switch cases because the enum values are treated as numbers.

So in your case, it is not possible to make the user enter a string input and then you check that input in a switch case. Before using the input with switch case, you should convert the user input (whatever it was) to an integral constant expression so that you can use inside the switch case.
You may also refer to this, I think it is helpful for you.

AAEM
  • 1,837
  • 2
  • 18
  • 26
  • The code says `int day; cin >> day;`. That reads the input into an `int`, so there is no issue of trying to use a `string`. Further, the expression in `switch(expr)` does not have to be a constant expression. Indeed, if it was, there would be no point in using a `switch` statement. You're right that the `case` labels require an integral constant expression, but that's not an issue here, because they all are. – Pete Becker Feb 14 '19 at 14:24
0

Switches are used for checking against integral constants so you can actually switch on the enumerated values you have, such as with:

switch (day) {
    case Mon: case Tue: case Wed: case Thu: case Fri:
        cout << "Weekday"; break;
    case Sun: case Sat:
        cout << "Weekend"; break;
    default:
        cout << "??"; break;
}
cout << '\n';

However, you can't enter the enumerated values in the way you expect, entering the word Tue and having that magically converted to the value 2. If you have the following code:

int x = 42;
std::cin >> x;

and you enter a non-numeric like Tue, x will not be any useful value that you can use. That's almost certainly the reason why, as you mention in a comment, it tells you it's the weekend no matter which textual day you enter. There's a good chance they all set x to zero (Sunday) because they cannot be immediately interpreted as an integer.


What you can do to allow textual input is to provide a function which does that conversion work for you, something like the one shown in this complete program:

#include <iostream>
#include <string>
#include <algorithm>

int getDayOfWeek(std::string textDay) {
    // Only use up to three characters, and lower-case.

    std::string day = textDay.substr(0,3);
    std::transform(day.begin(), day.end(), day.begin(), ::tolower);

    // Search through collection until found then return index.

    int dayOfWeek = 0;
    for (std::string chk: {"sun", "mon", "tue", "wed", "thu", "fri", "sat"}) {
        if (day == chk) return dayOfWeek;
        ++dayOfWeek;
    }

    // Not found, return sentinel value.

    return -1;
}

// Test harness to allow you to enter arbitrary lines and
// convert them to day indexes. Hit ENTER on its own to stop.

int main() {
    std::string day;
    std::cout << "Enter day: "; std::getline(std::cin, day);
    while (!day.empty()) {
        std::cout << day << " --> " << getDayOfWeek(day) << '\n';
        std::cout << "Enter day: "; std::getline(std::cin, day);
    }
    return 0;
}

The "meat" of this is the getDayOfWeek() function which, given a string, can tell you what day of the week that string represents (if any). Once you have it as an integer, it's a simple matter to use a switch statement on it, such as:

std::string getDayClass(std::string day) {
    switch getDayOfWeek(day) {
        case 0: case 6:
            return "weekend";
        case 1: case 2: case 3: case 4: case 5:
            return "weekday";
    }
    return "unknown";
}

Keep in mind I've used specific rules for decoding the textual day into an integer, checking only the first three characters, and in lowercase,

so WEDDED BLISS as input will see it thinking it's a Wednesday. Obviously you can make the rules more (or less) restrictive as the situation requires.

paxdiablo
  • 854,327
  • 234
  • 1,573
  • 1,953
-1

You want something like this:

int main()
{
    string day;
    cout << " Enter a day ";
    cin >> day;
    if (day == "Sun")
    {
        cout << "Weekend" << endl;
    }
    else if (day == "Mon")
    {
        cout << "Start of work week " << endl;
    }
    ...
    return 0;
}
Sid S
  • 6,037
  • 2
  • 18
  • 24