Croset Croset - 2 months ago 9
C++ Question

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;
};
}

Answer

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.

Comments