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
#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
#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
elsestatic_cast<Days>(Obj + 1); //add one to enum
}
if(Type == 2)
{
if(Obj == MONDAY)
Obj = SUNDAY; //if out of bounds...
elsestatic_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:
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.
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.