Debugging problems with Class program

this program keeps printing Sun (sunday) for some odd reason and then stops to function. I was running this in Visual Studio 2010.

here is the code:
#include <iostream>
#include <string>

using namespace std;

class dayType{ // constructor
public:
void setDay(string day);
void printDay();
string getDay();
string getNextDay();
string getPrevDay();
string addDays(int numDays);
dayType();
private:
string currDay;
string weekDay[7];
};

int main(){
int nDays;

dayType myDay;

myDay.setDay("Sun");
myDay.getDay();
cout << myDay.getDay() << endl; //testing getDay
myDay.printDay();
myDay.getNextDay();
cout << myDay.getNextDay() << endl; //testing getNextDay
myDay.getPrevDay();
cout << myDay.getPrevDay() << endl; // testing getPrevDay

cout << "Please enter the number of days: ";
cin >> nDays;
cout << myDay.addDays(nDays) << endl;


return 0;
}

void dayType::setDay(string day){ //From this point on, all of these will be members of the function dayType
currDay = day;
}

string dayType::getDay(){
return currDay;
}

void dayType::printDay(){
cout << currDay << endl;
}

string dayType::getNextDay(){
int x;
string nxtDay;

for(x = 0; x < 7; x++)
if(currDay == weekDay[x] && currDay != weekDay[6])
nxtDay = weekDay[x + 1];
else
nxtDay = weekDay[0];

return nxtDay;
}

string dayType::getPrevDay(){
int x;
string prevDay;

for(x = 7; x > 0; x--)
if(currDay == weekDay[x] && currDay != weekDay[0])
prevDay = weekDay[x - 1];
else
prevDay = weekDay[6];

return prevDay;
}

string dayType::addDays(int numDays){
int newDay;

newDay = numDays % 7;

return weekDay[newDay];

} //End of all the members for the function dayType

dayType::dayType() { //Constructor definition
currDay = "Sun";
weekDay[0] = "Sun";
weekDay[1] = "Mon";
weekDay[2] = "Tue";
weekDay[3] = "Wed";
weekDay[4] = "Thu";
weekDay[5] = "Fri";
weekDay[6] = "Sat";
}//end of constructor definition

would someone please help? thank you!
Hi, welcome to cplusplus.com

Please post your code inside of the code tags( [ code] and [ /code] without the space)


Well, the getNextDay() function isn't working, and when I ran it the program crashed on getPrevDay() with an access violation.


in the function getNextDay() you have a simple logic error, you have as the else for the if() to set it equal to sunday, now, when you find a match and set it to the index+1 you should probably return that value from inside of the for loop instead of it getting over-written by the else on the next iteration of the loop.

Same problem with getPrevDay()
except with getPrevDay you also create an access violation starting at x=7 instead instead of x=6, which is the actual end of the array, and you want to loop while x > (-1) instead of x is greater than 0. 0 is a valid index.


also I will suggest that you use the curly braces '{' and '}' for those two for loops because it makes the code clearer.

Last edited on
thank you! sorry for my horrid code format, I'm rushing to finish this as this is my finals week for college
I have it working just fine after making the corrections I suggested.
do you mind if you post the code up?
Topic archived. No new replies allowed.