I do not expect anyone to do all of my homework, After seeing some of the other forums, I noticed that there is alot of people that want others to do their work for them. You cant learn if you dont understand what to do. I will post the assignment definition and what I have so far, and the problems I am having. Any help would be greatly appreciated.
Write a program that contains a class that implements the days of the week. The program should be able to perform the following on an object of the class.
1.Set the day.
2.Print the day.
3.Return the day.
4.Return the next day.
5.Return the previous day.
6.Calculate and return the day by adding a certain amount of days to the current day. For example, if you add five days to Saturday, the day to be returned is Thursday. Likewise, if we add 12 days to Wednesday, the day returned will be Monday.
An example of the program is as follows:
The value of the monday object is Monday.
The day after monday is Tuesday.
The day before monday is Sunday.
Monday + 3 = Thursday.
The Value of Monday object is still Monday.
Monday - 3 = Friday.
Ok, I haven't had a programming class in a while, but this is what I got so far with no errors, the only thing I can't seem to figure out is how to come up with the formulation for the adding and subtracting the days.
#include "stdafx.h"
#include <iostream>
#include <string>
using namespace std;
class DayOfTheWeek
{
public:
void setDay(string );
// setDay(string) takes a string parameter
// and stores the value in the day attribute.
void printDay() const;
// printDay() prints the value of the day attribute
// on console output (cout).
string getDay() const;
// returns the value of the day attribute.
private:
string day; // This is where the value of the day attribute is stored.
};
// Set the values of the objects
monday.setDay("Mon");
tuesday.setDay("Tues");
// Get the value of the monday object and print it out
string currentDay = monday.getDay();
cout << "The value of the monday object is " << currentDay << "." << endl;
// Print out the value of the tuesday object
cout << "The value of the tuesday object is ";
tuesday.printDay();
cout << "." << endl;;
enum Weekday
{
INVALID,
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
};
Weekday operator+(Weekday w, int n)
{
if (w == INVALID) return w;
n %= 7;
if (n < 0) n += 7;
return Weekday((n + (w - 1)) % 7 + 1);
}
Weekday operator+(int n, Weekday w) {return w + n;}
Weekday operator-(Weekday w, int n) { return w + -n;}
Weekday operator-(int n, Weekday w);
std::string str(Weekday d)
{
assert(INVALID <= d && d <= SUNDAY);
static char const* const days[] =
{
"INVALID",
"MONDAY", "TUESDAY", "WEDNESDAY", "THURSDAY", "FRIDAY", "SATURDAY", "SUNDAY"
};
return days[d];
}
Weekday Weekday_from_num(int n, Weekday zero=MONDAY)
{
return zero + n;
}
return 0;
}
If there is anyone that could give me a hint on what I am doing wrong, I would greatly appreciate it.
Wow, there really are a number of ways you could go about this. This feels like one of those assignments that are meant to give you the oppurtunity to show off.
First I would add another private member to your DayOfTheWeek class that holds an integer. Then overload a constructor that creates the day based on an input of either an integer or a string, something like this:
//code code blah blah...
class DayOfTheWeek
{
private:
std::string day;
int nday;
public:
DayOfTheWeek(); //Default Nothing Constructor
DayOfTheWeek(std::string); //Constructor if passed a string
DayOfTheWeek(int); //Constructor if passed an int
std::string day(); //Returns the current string for that variable
int nday(); //Returns the current int for that variable
std::string day(int); //Returns the string associated with the int
int nday(std::string); //Returns the int associated with the string
// The rest of your class seems to be going in the right direction
}; //End of our class
DayOfTheWeek::DayOfTheWeek();
{
nday = 0;
day = day(nday); //Calling your own member function from within the constructor
}
/* Continue like this with your other two Constructors,
calling your member functions from within like this is lazy and therefore good */
int DayOfTheWeek::day(std::string day)
{
if(day == "Sunday" || day == "Sun")
{return 0;}
if(day == "Monday" || day == "Mon")
{return 1;}
if(day == "Tuesday" || day == "Tues")
{return 2;}
//Etc for the rest of the week
}//End of memeber function
std::string DayOfTheWeek::day(int day)
{
switch(day) //Since the input is an int we can show off the switch case statement
{
case 0: return"Sunday"; break;
case 1: return"Monday"; break;
//Etc for the rest of the week
}//Don't forget to end your switch case
} //End of member function
//Continue with your code as you see fit
Overloading member functions like this takes some getting used to but after that working with your classes involves less thinking, "The function to get the day if passed the number is X but what was it to get the number from the day? Oh Yeah! it's X!" Don't forget the rule of three, you're declaring a constuctor so you need to declare the otherwise default class functions too. If you have any questions about this stuff I'll be watching this thread for a while.
EDIT: You should note that if you plan on copy pasting the work above that it is not complete. Two of the constructors are not defined, there is no destructor, a few of the member functions aren't defined and I didn't even touch on the int main(...) {...}
REEDIT: BAH! I made a rookie mistake! for your int DayOfTheWeek::day(std::string day) function you should test all uppercase XOR all lower case letters and use either toupper(...) or tolower(...) to format the users input. This cuts your work in half and saves you from stubborn users who will not read any instuctions you provide.