Help with Enums(Program)

Running into trouble with enumerations, they just seem so confusing. please take into account this is not a school assignment, i self teach myself through books and online sites at the moment. Heres the exercise im stuck on:

Design and implement a class daytype that implements the day of the week in a program. the class daytype should store the day, such as sun for sunday. the program should be able to perform the following operations on an object of type daytype:
set the day
print the day
return the day
return the next day
return the previous day
calculate and return the day by adding days

I want to use enums for this program( i could easily use arrays to sole this, but thats not fun!). I apologize in advanced if the program makes no sense, ive never worked with enums before. Here's my code:
Daytype.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#ifndef DAYTYPE_H
#define DAYTYPE_H
#include <string>

class dayType
{
    public:
        enum Days {MONDAY = 0,TUESDAY,WEDNSDAY,THURSDAY,FRIDAY,SATURDAY,SUNDAY}; //should this be private?
        dayType();
        std::string _ConvertEnum(Days Obj, int Type); //print data
        void SetDay(std::string day){Day = day;}; //set day for program
        void _ReturnNext(Days Obj, std::string& Copy);//takes object and string to copy over
        void _ReturnPrev(Days Obj, std::string& Copy);
        void _Return(Days Obj, std::string& Copy);
    protected:
    private:
        std::string Day; //holds day of the week
};

#endif // DAYTYPE_H

Daytype.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#include "dayType.h"
#include <string>
#include <iostream>
dayType::dayType()
{

}
void dayType::_Return(Days Obj, std::string& Copy)
{
    Day = _ConvertEnum(Obj,0); //call function
    Copy = Day; //set equal to each other
}

void dayType::_ReturnNext(Days Obj, std::string& Copy)
{
    Day = _ConvertEnum(Obj,1);
    Copy = Day;
}
void dayType::_ReturnPrev(Days Obj, std::string& Copy)
{
    Day = _ConvertEnum(Obj,2);
    Copy = Day;
}

std::string dayType::_ConvertEnum(Days Obj,int Type) //1: Next, 2:prev 0:return
{
    if(Type == 1){
        if(Obj == SUNDAY)
            Obj = MONDAY; //if out of bounds, set the cylce again
        else
            static_cast<Days>(Obj + 1); //add one to enum
    }
    if(Type == 2)
    {
        if(Obj == MONDAY)
            Obj = SUNDAY; //if out of bounds...
        else
            static_cast<Days>(Obj - 1); //subtract one to enum
    }
    std::string temp; //returns this string
    switch(Obj) //never seems to work for me, why?
    {
        case MONDAY:
            temp = "Monday\n";
            break;
        case TUESDAY:
            temp = "Tuesday\n";
            break;
        case WEDNSDAY:
            temp = "Wednsday\n";
            break;
        case THURSDAY:
            temp = "Thursday\n";
            break;
        case FRIDAY:
            temp = "Friday\n";
            break;
        case SATURDAY:
            temp = "Saturday\n";
            break;
        case SUNDAY:
            temp = "Sunday\n";
            break;
    }
    std::cout << temp; //prints nothing out
    return temp; //seems to do nothing
}


main.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <string>
#include "dayType.h"

int main()
{
    dayType Obj; //create object
    std::string DAY; //create a string to hold the DAY
    dayType::Days Enumtest; //create an object for the enum days
    Obj.SetDay("MONDAY"); //set day(didnt work)
    Obj._Return(Enumtest,DAY); //doesnt work
    std::cout << "The current day is: " << DAY << std::endl;
    Obj._ReturnNext(Enumtest,DAY); //doesnt work
    std::cout << "Tommorow will be: " << DAY << std::endl;
    Obj._ReturnPrev(Enumtest,DAY); //doesnt work
    std::cout << "Yesterday was: " << DAY << std::endl;
}

There are a number of things wrong here, but I only have time to mention a few.

First of all, your ConvertEnum function is doing lots of work, while the _Return functions just call it. I think you kind of got this backwards. The calculation of the previous and next days should be in their own functions. The ConvertEnum function should only convert from enum to sting.

You should probably be storing an enum value in the class and then converting to string when you want to print. Something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
class DayType
{
public:
    enum Days { MONDAY, ...};
    DayType(Days d) : day(d) {}
    void setDay(Days d) { day = d; }

    std::string toString() const;

    std::string _Return() const { return toString(); }
    std::string _ReturnNext() const { return _getNext().toString(); }

private:
    DayType _getNext() const;
    Days day;

};


std::string DayType::toString() const
{
    std::string temp; //returns this string
    switch(day)
    {
        case MONDAY:
            temp = "Monday\n";
            break;

            ...
    }

    return temp;
}

DayType DayType::_getNext() const
{
    Days Obj = day;

    if(Obj == SUNDAY)
            Obj = MONDAY; //if out of bounds, set the cylce again
        else
            static_cast<Days>(Obj + 1); //add one to enum

    return (DayType next(Obj));
}


There is no guarantee this will compile, but it will probably get you pointed in the right direction.
Thanks for the help! ill try this and post if i have any more problems.
I tried the option above, but it doesnt seem to work. Could anyone else help me? Thanks!
Still stuck!
I just subscribed to your thread. If I wake up and no one has replied, I'll try to talk you through enums. I need to implement them in my menu at the moment just too tired.
This dayType::Days Enumtest; creates an uninitialzed variable with the enum type.

Neither _Return() or _ReturnNext() will change the value. Hence you will never get a valid result.

Do you understand what doug4 is trying to say?
I looked over the code again really quick, but wouldn't it be easier to define enum days in your main() and then pass an int value to your class? I think this is a lot of code for something that seems fairly simple. You can use PrintDay() to print out the string and use the rest of the class to handle the math with int. just my 2 cents.
Topic archived. No new replies allowed.