Hi, I'm trying to make a class called dayType. As a private member, I declared and initialized a string array to hold the days of the week and for some reason, my compiler doesn't like it. Here are the errors.
(17) syntax error : '{'
(17) unexpected token(s) preceding '{'; skipping apparent function body
error C2065: 'dayList' : undeclared identifier... (got 8 of these errors)
Is it because I'm including a class (string) in my own class? I am not sure what to do about that problem however. Here is my code.
Now that I consider it, that array probably should be static and constant anyway. The values of the days of the week shouldn't change and ought to be the same for all classes, I would think.
#include <iostream>
#include <string>
usingnamespace std;
class dayType
{
public:
void setDay(string dayOfWeek);
void printDay() const;
string returnDay() const;
string returnNextDay() const;
string returnPreviousDay() const;
string returnDayByAdding(int num);
dayType();
private:
string dayList[7];
int numberOfDay; //variable to hold the index of array dayList
};
int main()
{
dayType myWeek;
string weekDay;
int increaseDay;
cout << "Today is ";
myWeek.printDay();
cout << "Enter the day you would like to begin with.";
cin >> weekDay;
myWeek.setDay(weekDay);
cout << "After setting the day to your desired day, the day is ";
myWeek.printDay();
cout << "The day of the week is " << myWeek.returnDay() << endl;
cout << "The next day is " << myWeek.returnNextDay() << endl;
cout << "Yesterday was " << myWeek.returnPreviousDay() << endl;
cout << "Enter the number of days to increase.";
cin >> increaseDay;
cout << "After " << increaseDay << " days, the day of the week is " << myWeek.returnDayByAdding(increaseDay) << endl;
return 0;
}
void dayType::setDay(string dayOfWeek)
{
for(int index=0;index<7;index++)
if(dayList[index]==dayOfWeek)
{
numberOfDay=index;
break;
}
}
void dayType::printDay() const
{
cout << dayList[numberOfDay] << endl;
}
string dayType::returnDay() const
{
return dayList[numberOfDay];
}
string dayType::returnNextDay() const
{
return dayList[numberOfDay+1];
}
string dayType::returnPreviousDay() const
{
return dayList[numberOfDay-1];
}
string dayType::returnDayByAdding(int num)
{
int tempDay=numberOfDay+num;
numberOfDay=tempDay%7;
return dayList[numberOfDay];
}
dayType::dayType()
{
dayList[0]="Sunday";
dayList[1]="Monday";
dayList[2]="Tuesday";
dayList[3]="Wednesday";
dayList[4]="Thursday";
dayList[5]="Friday";
dayList[6]="Saturday";
numberOfDay=0;
}