so im a new comp sci major in college i did good last semester and now im in a new semester with a different teacher, well apparently in the first semester we should have learned how to make classes pretty well but we didn't... and now my first assignment in my new class is to create a class in c++ that keeps track of the day this is what i have so far
(i honestly have no idea where to go from here... here are the program instructions.
Write the definition of the class dayType the implements the day of the week in a program. The class dayType should store the day, such as sunday for Sunday. The program should be able to perform the following operations on an object of type dayType:
1. set the day
2. print the day
3. return the day
4. return the next day
5. return the previous day
6. calculate the day when adding n amount of days.)
#include <iomanip>
#include <iostream>
using namespace std;
What about a constructor? What if I want to say dayType fav("Sunday"); or dayType myfunday(0);
Make it so that your program has a constructor with default arguments. Accept either the day as a number or a std::string.
Your enum is fine, but you'll Still need to std::cout the strings.
1 2 3
if (date == Sunday)
std::cout << "Sunday" << std::endl;
// more statements to print here.
What about if statements to catch if I set the date outside of your week? Or will you just %7 me back in the right date?
myday.setDate(3495); // You're gonna need to handle this.
EDIT: Why do you have semicolons after each {} in your functions. remove the {}.
#include <iostream>
using std::cout;
#include <limits>
#include <string>
using std::string;
enum days_e {Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday};
string returnDay(days_e days);
int main(int argc, char* argv[])
{
days_e days;
days = Monday;
cout << "Today is: " << returnDay(days) << std::endl;
days = (days_e)(days + 1);
cout << "Today is: " << returnDay(days) << std::endl;
cout << "Press <ENTER> to continue...";
std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
return 0;
}
string returnDay(days_e days)
{
switch(days)
{
case Monday:
return"Monday";
case Tuesday:
return"Tuesday";
case Wednesday:
return"Wednesday";
case Thursday:
return"Thursday";
case Friday:
return"Friday";
case Saturday:
return"Saturday";
case Sunday:
return"Sunday";
}
}
The only problem is that if you increment the days var beyond the number of members in the enum, your program will crash. Someone want to give a solution beyond that?
ik i need constructors and what not, i just dont know where to start to make them nor do i know what i need to put in the private area. and the day date; i put in as some kind of way of pulling the number out of the enum but i have no idea if i did it right. and as far as your question about modding from my understanding %7 should break any number down small enough so that the remainder will keep you with in a 7 day week
so this is what i have so far and what i have changed given the info you all have given me and thank you by the way. am i using it in the right context how ever? and as far as constructors go i have no idea where to start i know u named some but they dont really deal with the instruction for my project that i stated in my original post. any way here is what i have so far
#include <iomanip>
#include <iostream>
usingnamespace std;
enum day{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
class dayType
{
public:
void daySet()//set the day
string returnDay(day date)
{
switch(date)
{
case Monday:
return"Monday";
break;
case Tuesday:
return"Tuesday";
break;
case Wednesday:
return"Wednesday";
break;
case Thursday:
return"Thursday";
break;
case Friday:
return"Friday";
break;
case Saturday:
return"Saturday";
break;
case Sunday:
return"Sunday";
break;
}
}
string dayPrint(day date); //print out the current day
int tomorrow()//return what the next day will be
{
return (date+1)%7;
}
int yesterday()// return what the day before was
{
return (date-1)%7;
}
int add_to_day(int n)//return what the day would be plus n
{
return (date+n)%7;
}
private:
day date;
}
int main()
{
}
-- INSERT --
#include <iomanip>
#include <iostream>
usingnamespace std;
enum day_orig{Sunday,Monday,Tuesday,Wednesday,Thursday,Friday,Saturday};
string returnDay(day_orig day);
class dayType
{
public:
void daySet();//set the day
string returnDay(day_orig day)
{
switch(day)
{
case Monday:
return"Monday";
break;
case Tuesday:
return"Tuesday";
break;
case Wednesday:
return"Wednesday";
break;
case Thursday:
return"Thursday";
break;
case Friday:
return"Friday";
break;
case Saturday:
return"Saturday";
break;
case Sunday:
return"Sunday";
break;
}
}
string dayPrint(day_orig day); //print out the current day
int tomorrow()//return what the next day will be
{
return (day+1)%7;
}
int yesterday()// return what the day before was
{
return (day-1)%7;
}
int add_to_day(int n)//return what the day would be plus n
{
return (day+n)%7;
}
private:
day_orig day;
};
// constructors for my class
int main()
{
day_orig day;
day=Monday;//d is the day you want to start on
cout<<"Today is "<<returnDay(day)<<endl;
day = (day_orig)((day+1)%7);
cout<<"Today is "<<returnDay(day)<<endl;
return 0;
}
#include <iostream>
#include<string>
usingnamespace std;
string my_Day[7] = { "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday" };
class day //initialize the class
{
public:
day();
void setDay(string);
void printDay();
string getDay();
void addDay();
private:
// set an integer to represent the day
int my_Day2;
};
day::day() //set the day to a defult
{
my_Day2 = 0;
}
void day::addDay()
{
my_Day2++;
if (my_Day2 > 7)
my_Day2 = 1;
}
string day::getDay() //return the day
{
return my_Day[my_Day2];
}
void day::setDay(string newDay) // reset the day to a new one
{
// set the day integer variable to the co-insiding day
for (int i = 0; i < 7; i++)
{
if (newDay.compare(my_Day[i]) == 0)
{
my_Day2 == i;
return;
}
}
// If this code is reached then newDay was not viable,
// so set the day to 0.
my_Day2 = 0;
}
void day::printDay() //this code prints out the day
{
cout << "The day is " <<getDay()<< endl;
}
int main()
{
return 0;
}