简体   繁体   中英

Enum type Days of the week

I need create program when user send "monday". Program send "1". "Tuesday" = 2 etc

my code:

#include <iostream>
#include <string>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */
using namespace std;


enum DAY {monday = 1, Tuesday, Wednesday, cThursday, Thursday};

int main(int argc, char** argv) {
    string x;
    cout << "Day: " << endl;
    cin >> x;
    DAY neww;
    neww = x;
    cout << neww << endl;
    return 0;
}

The problem with your code is that the enums are just numbers while you're trying to pass a string, try this:

#include <iostream>
#include <string>
#include <algorithm>
using namespace std;

enum DAY {
    Monday = 1,
    Tuesday,
    Wednesday,
    Thursday,
    Friday,
    Saturday,
    Sunday,
    INCORRECTDATEFORMAT
};

DAY getDay(string& d)
{
    std::transform(d.begin(), d.end(), d.begin(), ::tolower);
    if(d == "monday")
        return DAY::Monday;
    else if(d == "tuesday")
        return DAY::Tuesday;
    else if(d == "wednesday")
        return DAY::Wednesday;
    else if(d == "thursday")
        return DAY::Thursday;
    else if(d == "friday")
        return DAY::Friday;
    else if(d == "saturday")
        return DAY::Saturday;
    else if(d == "sunday")
        return DAY::Sunday;
    else
        return DAY::INCORRECTDATEFORMAT;
}
int main()
{
    string d;
    cout << "Day : ";
    cin >> d;
    cout << getDay(d) << endl;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM