Croset
Croset

Reputation: 37

Simple C++ Calculating days of the week, a bit stuck?

So this is what I am trying to achieve

enter image description here

This is my code. I am completely stuck now, what am I missing here that's right in front of my face? I feel I have all functions in place but I dont think it's going to calculate it correctly. I have some errors in here that I can't fix to even attempt to run it. Thank you for any help;-

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

class dayType
{
public:
    void setDay(int dayNum);
    // set the day with the dayNum as parameter
    void printd(dayNum);
    // print the dayNum
    int returnDay();
    // return the day of the week
    void dayAfter();
    // return next day
    void dayBefore();
    // return previous day
    void randomDay(int dayNum);
    // function to return a day after a certain number of days


    dayType(int dayNum);
    // Constructor with parameters setting dayNum according to parameters
    dayType();
    //Default constructor

    private:
        int today;
        int yest;
        int tom;
        int dayN;

    };




    void dayType::printd(int dayNum)
    {
        if (dayNum == 1)
            cout << "Monday" << endl;

        if (dayNum == 2)
            cout << "Tuesday" << endl;

        if (dayNum == 3)
            cout << "Wednesday" << endl;

        if (dayNum == 4)
            cout << "Thursday" << endl;

        if (dayNum == 5)
            cout << "Friday" << endl;

        if (dayNum == 6)
            cout << "Saturday" << endl;

        if (dayNum == 7)
            cout << "Sunday" << endl;

    }


    void dayType::setDay(int dayNum)
    {
    today = dayNum;

    };

    int dayType::returnDay()
    {
        return today;

    };

    void dayType::printd(<#int dayNum#>);
    {
    cout << "The current day is: " << today << endl;
    }


    void dayType::dayBefore()
    {
        if(today == 0)
            yest = 6;
        else today--;
    };


    void dayType::dayAfter()
    {
        if(today == 6)
        tom = 0;

    };

    void dayType::randomDay(int dayNum)
    {
        dayN=(today+dayNum);
        today =(dayN%7);

    };

    dayType::dayType()
    {
        today = 0;
    }


    dayType::dayType(int daynum)
    {
    today = daynum;
    }
    // do I need these constructors here doing this?



            int main()

            {
                int dayWeek;

                cout << "Please enter a number for the day of the week: " << endl;
                cout << "1 - Monday" << endl;
                cout << "2 - Tuesday" << endl;
                cout << "3 - Wednesday" << endl;
                cout << "4 - Thursday" << endl;
                cout << "5 - Friday" << endl;
                cout << "6 - Saturday" << endl;
                cout << "7 - Sunday" << endl;

                while (dayWeek<= 7)

                    cin >> dayWeek;

                dayType thisDay;

                cout << "Today is: ";
                thisDay.returnDay();
                thisDay.printd(int dayNum);


                cout << "Yesterday was: ";
                thisDay.dayBefore();
                thisDay.printd(int dayNum);


                cout << "Tomorrow is: ";
                thisDay.dayAfter();
                thisDay.printd(int dayNum);

                cout << "Type a number of days from today and it will be: ";
                thisDay.randomDay(dayNum);

                return 0;
            };
        }

Upvotes: 0

Views: 972

Answers (2)

Personally, I would suggest using an enum instead of int to keep track of the day, along with the necessary operators to allow you to cycle through the days. This would allow you to more easily define and interact with dayType, easily fixing the errors you encountered.

#include <iostream>
#include <cmath>
#include <string>
#include <limits>

using namespace std;

class dayType
{
public:
    // Days enum:
    enum Days {
        D_MONDAY = 1,
        D_TUESDAY,
        D_WEDNESDAY,
        D_THURSDAY,
        D_FRIDAY,
        D_SATURDAY,
        D_SUNDAY
    };

    void setDay(int dayNum);
    // set the day with the dayNum as parameter

    void printd(int dayNum);
    // print the dayNum
    void printd();
    // Prints the current day.

    Days returnDay();
    // return the day of the week
    Days dayAfter();
    // return next day
    Days dayBefore();
    // return previous day
    Days randomDay(int dayNum);
    // function to return a day after a certain number of days


    dayType(int dayNum);
    // Constructor with parameters setting dayNum according to parameters
    dayType();
    //Default constructor

private:
    Days today;
};

// Compound assignment addition:
dayType::Days& operator+=(dayType::Days& left, int right)
{
    typedef dayType::Days Days;

    int temp = static_cast<int>(left) + right;

    // In case of adding negative numbers.
    while (temp < dayType::D_MONDAY)
        temp += dayType::D_SUNDAY;

    while (temp > dayType::D_SUNDAY)
        temp -= dayType::D_SUNDAY;

    left = static_cast<Days>(temp);

    return left;
}

// Compound assignment subtraction:
dayType::Days& operator-=(dayType::Days& left, int right)
{
    typedef dayType::Days Days;

    int temp = static_cast<int>(left) - right;

    while (temp < dayType::D_MONDAY)
        temp += dayType::D_SUNDAY;

    // In case of subtracting negative numbers.
    while (temp > dayType::D_SUNDAY)
        temp -= dayType::D_SUNDAY;

    left = static_cast<Days>(temp);

    return left;
}


// Addition.  Uses compound assignment addition internally.
dayType::Days  operator+(dayType::Days left, int right)
{
    return left += right;
}

// Subtraction.  Uses compound assignment subtraction internally.
dayType::Days  operator-(dayType::Days left, int right)
{
    return left -= right;
}


// Prefix increment (++day):
dayType::Days  operator++(dayType::Days& day)
{
    return day += 1;
}

// Postfix increment (day++):
dayType::Days  operator++(dayType::Days& day, int)
{
    typedef dayType::Days Days;

    Days temp = day;
    ++day;
    return temp;
}


// Prefix decrement (--day):
dayType::Days  operator--(dayType::Days& day)
{
    return day -= 1;
}

// Postfix decrement (day--):
dayType::Days  operator--(dayType::Days& day, int)
{
    typedef dayType::Days Days;

    Days temp = day;
    --day;
    return temp;
}


void dayType::printd(int dayNum)
{
    if (dayNum == D_MONDAY)
        cout << "Monday" << endl;

    if (dayNum == D_TUESDAY)
        cout << "Tuesday" << endl;

    if (dayNum == D_WEDNESDAY)
        cout << "Wednesday" << endl;

    if (dayNum == D_THURSDAY)
        cout << "Thursday" << endl;

    if (dayNum == D_FRIDAY)
        cout << "Friday" << endl;

    if (dayNum == D_SATURDAY)
        cout << "Saturday" << endl;

    if (dayNum == D_SUNDAY)
        cout << "Sunday" << endl;
}

void dayType::printd()
{
    cout << "The current day is: ";
    printd(today);
}


void dayType::setDay(int dayNum)
{
    today = static_cast<Days>(dayNum);
};

dayType::Days dayType::returnDay()
{
    return today;
};

dayType::Days dayType::dayBefore()
{
    return today - 1;
};


dayType::Days dayType::dayAfter()
{
    return today + 1;
};

dayType::Days dayType::randomDay(int dayNum)
{
    return today + dayNum;
};

dayType::dayType()
{
    today = D_SUNDAY;
}


dayType::dayType(int daynum)
{
    today = static_cast<Days>(daynum);
}



int main()
{
    int dayWeek = 0;

    cout << "Please enter a number for the day of the week: " << endl;
    cout << "1 - Monday" << endl;
    cout << "2 - Tuesday" << endl;
    cout << "3 - Wednesday" << endl;
    cout << "4 - Thursday" << endl;
    cout << "5 - Friday" << endl;
    cout << "6 - Saturday" << endl;
    cout << "7 - Sunday" << endl;

    while (!((dayWeek > 0) && (dayWeek < 8)))
        cin >> dayWeek;

    dayType thisDay(dayWeek);

    // -----

    cout << "Today is: ";
    thisDay.printd(thisDay.returnDay());

    // -----

    cout << "Yesterday was: ";
    thisDay.printd(thisDay.dayBefore());

    // -----

    cout << "Tomorrow is: ";
    thisDay.printd(thisDay.dayAfter());

    // -----

    cout << "Type a number of days: ";

    int random;
    cin >> random;
    cin.ignore(std::numeric_limits<streamsize>::max(), '\n');

    cout << "In " << random << " days, it will be: ";
    thisDay.printd(thisDay.randomDay(random));
};

At the moment, this should work for what you want, with the caveat that if you want to use an int as a Days (such as to initialise today in dayType::dayType(int daynum)), you have to explicitly cast it into one. (Conversely, a Days can implicitly be used as an int, as demonstrated by passing the return value of dayType::returnDay() to dayType::printd(), or if you pass a Days to cout.)

If you want to use Days outside of dayType, you can use it as dayType::Days (such as in the operators' return types). You can also use typedef dayType::Days Days; to bring it into the current scope, allowing you to use it as simply Days (such as in dayType::Days operator++(dayType::Days& day, int) or dayType::Days operator--(dayType::Days& day, int)). Similarly, to use the enumeration values themselves, prefix their name with dayType::.

As you're using this exercise, and your question here, as a learning opportunity, I left a few places where things could be improved. It currently works properly, but has room for modifications that would make it work better.

Upvotes: 1

Dylan
Dylan

Reputation: 107

Your code should be more like this:

#include <iostream>
#include <cmath>
#include <string>

using namespace std;

class dayType
{
public:
    void setDay(int dayNum);
    void printd();
    // set the day with the dayNum as parameter
    void printd(int dayNum);
    // print the dayNum
    int returnDay();
    // return the day of the week
    int dayAfter();
    // return next day
    int dayBefore();
    // return previous day
    int randomDay(int dayNum);
    // function to return a day after a certain number of days


    dayType(int dayNum);
    // Constructor with parameters setting dayNum according to parameters
    dayType();
    //Default constructor

    private:
        int today;

};




void dayType::printd(int dayNum)
{
    if (dayNum == 1)
        cout << "Monday" << endl;

    if (dayNum == 2)
        cout << "Tuesday" << endl;

    if (dayNum == 3)
        cout << "Wednesday" << endl;

    if (dayNum == 4)
        cout << "Thursday" << endl;

    if (dayNum == 5)
        cout << "Friday" << endl;

    if (dayNum == 6)
        cout << "Saturday" << endl;

    if (dayNum == 7)
        cout << "Sunday" << endl;

}


void dayType::setDay(int dayNum)
{
    today = dayNum;

}

int dayType::returnDay()
{
   return today;

}

void dayType::printd()
{
cout << "The current day is: " << today << endl;
}


int dayType::dayBefore()
{
    int yest;
    if(today == 0)
        yest = 6;
    else
        yest = today - 1;
    return yest;
};


int dayType::dayAfter()
{
    int tom;

    if(today == 6)
        tom = 0;
    else 
        tom = today + 1;
    return tom;

};

int dayType::randomDay(int dayNum)
{
    int dayN = (today+dayNum);
    return (dayN % 7)

};

dayType::dayType()
{
    today = 0;
}


dayType::dayType(int daynum)
{
    today = daynum;
}
// do I need these constructors here doing this?



int main()

{
    int dayWeek = -1;
    int random = -1;

    cout << "Please enter a number for the day of the week: " << endl;
    cout << "1 - Monday" << endl;
    cout << "2 - Tuesday" << endl;
    cout << "3 - Wednesday" << endl;
    cout << "4 - Thursday" << endl;
    cout << "5 - Friday" << endl;
    cout << "6 - Saturday" << endl;
    cout << "7 - Sunday" << endl;

    while (dayWeek >= 7 || dayWeek < 0)

        cin >> dayWeek;

    dayType thisDay;
    thisDay.today = dayWeek;

    thisDay.printd();


    cout << "Yesterday was: ";
    thisDay.printd(thisDay.dayBefore());


    cout << "Tomorrow is: ";
    thisDay.printd(thisDay.dayAfter());

    cout << "Type a number of days : ";

    while (random < 0)

        cin >> random;

    cout << "Now we are  ";
    thisDay.printd(thisDay.randomDay(random));

    return 0;
}

When you call a function like this : void fonction(int a, int b), you can call it like this : fonction(1, 2);. You don't have to write the type of the arguments. In your class, declare only the value you need for your functions. In your previous code, you declared tom, yesand dayN but you don't need to keep their values.

I didn't test the code, so if you have errors, write them and I'll edit this post.

Upvotes: 1

Related Questions