JPKrouse
Jan 14, 2011, 10:31 PM
I am trying to write a program that contains a class that implements the days of the week.
I got to a point and now I am a bit stuck.
I have changed it so many time I am starting to confuse myself.. ANY help would be appreciated!
my code is:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class DayOfTheWeek
{
private:
string dayName;
int result;
public:
int toNum(string day)
{
if (dayName == "Monday") return 0;
if (dayName == "Tuesday") return 1;
if (dayName == "Wednesday") return 2;
if (dayName == "Thursday") return 3;
if (dayName == "Friday") return 4;
if (dayName == "Saturday") return 5;
return 6;
}
string toName(int I)
{
switch (I)
{
case 0: return "Monday";break;
case 1: return "Tuesday"; break;
case 2: return "Wednesday"; break;
case 3: return "Thursday"; break;
case 4: return "Friday";break;
case 5: return "Saturday";break;
default: return "Sunday";break;
}
}
void setDay (string day)
{
dayName = day;
}
string printDay()
{
cout << dayName;
}
string getDay()
{
return dayName;
}
string plusOneDay(string day)
{
/*char Monday[] = "0";
int result;
result = atoi("Monday");
*/
return toNum();
}
string minusOneDay()
{
}
string addDays()
{
}
};
int main()
{
DayOfTheWeek monday;
monday.setDay("Monday");
monday.toNum(dayName);
cout << Monday.toNum();
cout << "monday object is set to " <<monday.getDay() <<endl;
//cout << "The day after monday is " <<monday.plusOneDay() << endl;
//cout << "The day before monday is" <<monday.minusOneDay() << endl;
cout << "The value for monday object is still " <<monday.getDay() <<endl;
//cout << "Monday plus 3 = " <<monday.addDays(3) <<endl;
//cout << "Monday minus 3 = " <<monday.addDays(-3) <<endl;
cin.ignore(2);
return 0;
}
essentially, I want it to set the day as Monday and be able to add a day, and subtract a day and have the right day come to the output (ie Tuesday for add a day,etc.. )
Also, I need to be able to add a number to the day and have the output come out correct for the right day of the week.
I was trying to make the Monday object into an int using the toNum() function, and then back again to string to display the name with the toName() function.
In the plusOneDay() function I was trying to make the value from ToNum() add with the number for the current day of the week (monday).
I deleted the code from the other functions,so this is very incomplete.
can anyone give me any guideance if I'm going about this all wrong?
I got to a point and now I am a bit stuck.
I have changed it so many time I am starting to confuse myself.. ANY help would be appreciated!
my code is:
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class DayOfTheWeek
{
private:
string dayName;
int result;
public:
int toNum(string day)
{
if (dayName == "Monday") return 0;
if (dayName == "Tuesday") return 1;
if (dayName == "Wednesday") return 2;
if (dayName == "Thursday") return 3;
if (dayName == "Friday") return 4;
if (dayName == "Saturday") return 5;
return 6;
}
string toName(int I)
{
switch (I)
{
case 0: return "Monday";break;
case 1: return "Tuesday"; break;
case 2: return "Wednesday"; break;
case 3: return "Thursday"; break;
case 4: return "Friday";break;
case 5: return "Saturday";break;
default: return "Sunday";break;
}
}
void setDay (string day)
{
dayName = day;
}
string printDay()
{
cout << dayName;
}
string getDay()
{
return dayName;
}
string plusOneDay(string day)
{
/*char Monday[] = "0";
int result;
result = atoi("Monday");
*/
return toNum();
}
string minusOneDay()
{
}
string addDays()
{
}
};
int main()
{
DayOfTheWeek monday;
monday.setDay("Monday");
monday.toNum(dayName);
cout << Monday.toNum();
cout << "monday object is set to " <<monday.getDay() <<endl;
//cout << "The day after monday is " <<monday.plusOneDay() << endl;
//cout << "The day before monday is" <<monday.minusOneDay() << endl;
cout << "The value for monday object is still " <<monday.getDay() <<endl;
//cout << "Monday plus 3 = " <<monday.addDays(3) <<endl;
//cout << "Monday minus 3 = " <<monday.addDays(-3) <<endl;
cin.ignore(2);
return 0;
}
essentially, I want it to set the day as Monday and be able to add a day, and subtract a day and have the right day come to the output (ie Tuesday for add a day,etc.. )
Also, I need to be able to add a number to the day and have the output come out correct for the right day of the week.
I was trying to make the Monday object into an int using the toNum() function, and then back again to string to display the name with the toName() function.
In the plusOneDay() function I was trying to make the value from ToNum() add with the number for the current day of the week (monday).
I deleted the code from the other functions,so this is very incomplete.
can anyone give me any guideance if I'm going about this all wrong?