1a). Design and implement a class dayType that implements the day of the week in a program. The class dayType should store the day, such as Sun for Sunday. The program should be able to perform the following operations on an object of type dayType:
a. Set the day
b. Print the day
c. Return the day
d. Return the next day
e. Return the previous day
f. Calculate and return the day by adding certain days to the current day. For example if the current day is Monday and we add 4 days, the day to be returned is Friday. Similarly, if today is Tuesday and we add 13 days, the day to be returned is Monday.
g. Add the appropriate constructors.
1b). Then, write the definitions of the functions to implement the operations for the class dayType as defined in Programming above. Also, write a program to test various operations on this class.
I've finish all the required part but sadly I've encountered errors on the (Sunday and Saturday part) during the run. * I was so close to the final result, can anybody guide/help me on my errors. Thank you and God Bless you.
The errors said *segmentation fault. The following are my Header file, Implementation file and Main file.
//Header file
#include<iostream>
#include<string>
using namespace std;
class dayType
{
public:
dayType();
string day[8];
int dayNumber;
int tempDay;
void setDay(int day);
void printDay();
void returnDay(int &day);
void returnNextDay();
void returnPreviousDay();
void calculateDay(int changeDay);
};
//Implementation file
#include <iostream>
#include <string>
#include "dayType.h"
using namespace std;
void dayType::printDay()
{
cout << "Today is: " << day[dayNumber] << "day" << endl;};
void dayType::setDay(int day)
{
dayNumber=day;};
void dayType::returnDay(int &day)
{
day=dayNumber;};
void dayType::returnNextDay()
{
dayNumber++;};
void dayType::returnPreviousDay()
{
dayNumber--;};
void dayType::calculateDay(int changeDay)
{
tempDay=(dayNumber+changeDay);
dayNumber=(tempDay%7);};
//Main file
#include <iostream>
#include "dayType.h"
using namespace std;
int main()
{
dayType today;
int day;
int changeDay;
cout << "------------------------------------------------------------------" << endl;
cout << "Default day is Sunday " << endl;
cout << "Type in a number corresponding to set the day" << endl
<< "1: Monday" << endl
<< "2: Tuesday" << endl
<< "3: Wednesday" << endl
<< "4: Thursday" << endl
<< "5: Friday" << endl
<< "6: Saturday" << endl
<< "7: Sunday" << endl;
while (day<0 || day>7)
cin >> day;
today.setDay(day);
today.printDay();
today.returnDay(day);
today.printDay();
cout << "-----------------------------------------------------------------" << endl;
cout << "If it were tomorrow, then the following statement would be true: " << endl;
today.returnNextDay();
today.printDay();
today.dayNumber--;
cout << "-----------------------------------------------------------------" << endl;
cout << "If it were yesterday, then the following statement would be true:" << endl;
today.returnPreviousDay();
today.printDay();
today.dayNumber++;
cout << "-----------------------------------------------------------------" << endl;
cout << "Add a number of days to today and see what day it will be: " << endl;
cin >> changeDay;
today.calculateDay(changeDay);
today.printDay();
return 0;
};
dayType::dayType()
{
dayNumber=1;
day[1]="Mon";
day[2]="Tues";
day[3]="Wednes";
day[4]="Thurs";
day[5]="Fri";
day[6]="Satur";
day[7]="Sun";
};

New Topic/Question
Reply




MultiQuote





|