Difficulty with classes and changing variables

Hi all, I'm trying to set this up so that you can put a number in, positive or negative, and it will add or subtract form the array index so the correct day will show up (like subtracting past sunday goes to saturday and beyond, adding past saturday goes to sunday and beyond.

I thought I would try for loops so I could add the number one at a time and be able to control the overlap. However, I'm having trouble getting it to read out properly.

1
2
3
4
cout << endl << "    Add days to " << dayNameArray[day.dayChoice] << "? (Enter a number to add, or a negative number to subtract): " << endl;
	cin >> day.plusDays;
	cout << endl << "    Now the day is " << day.addDays(day.plusDays) << ". ";
//I've also tried dayNameArray[day.addDays(day.plusDays)] here and it still returns it as 0 


That's the statement I'm trying to pull from. Here is the method.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
int DayOfTheWeek::addDays(int plusDays)
{
	if(plusDays < 0)
	{
		for(dayChoice; plusDays = 0; plusDays++)
			dayChoice--;
			while(dayChoice < 0)
			{
				dayChoice += 7;
			}
			plusDays++;
	}
	else
	{	
		for(dayChoice; plusDays = 0; plusDays--)
			dayChoice++;
			if(dayChoice > 6)
			{
				dayChoice = (dayChoice % 7);
			}
			plusDays--;
	}
	return dayChoice;
}


How can I return the dayChoice for the array and still call the method with the class?
Last edited on
I've also tried:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
string DayOfTheWeek::addDays(int plusDays)
{
	if(plusDays < 0)
	{
		for(dayChoice; plusDays = 0; plusDays++)
		{
			dayChoice--;
			while(dayChoice < 0)
			{
				dayChoice += 7;
			}
			plusDays++;
		}
		return dayNameArray[dayChoice];
	}
	else
	{	
		for(dayChoice; plusDays = 0; plusDays--)
		{
			dayChoice++;
			if(dayChoice > 6)
			{
				dayChoice = (dayChoice % 7);
			}
			plusDays--;
		}
		return dayNameArray[dayChoice];
	}

	
}


and it still doesn't make any changes tot he day when it outputs.
Check this out:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
#include <iostream>
#include <string>

using namespace std;

const string DAYS[]={"Sunday","Monday","Tuesday",
    "Wednesday","Thursday","Friday","Saturday"};

int main()
{
    int cur_day=4;

    cout << "cur day: " << DAYS[cur_day] << endl;

    cout << "add/sub how many days? ";
    int days;
    cin >> days;

    cur_day+=days;
    cur_day%=7;
    cur_day+=7;
    cur_day%=7;

    cout << "cur day: " << DAYS[cur_day] << endl;

    cout << "\nhit enter to quit...";
    cin.get(); cin.get();
    return 0;
}
Thanks roshi, that works but I have to make it work with a class. I have several other calls before this one, but this is the only one that doesn't work. I don't totally understand what's going on in the middle with all the cur_days's
Last edited on
I rewrote it to:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
string DayOfTheWeek::addDays(int plusDays)
{
	if(plusDays < 0)
	{
		for(dayChoice; plusDays = 0; plusDays++)
		{
			dayChoice--;
			while(dayChoice < 0)
			{
				dayChoice += 7;
			}
		}
		return dayNameArray[dayChoice];
	}
	else
	{	
		for(dayChoice; plusDays = 0; plusDays--)
		{
			dayChoice++;
			while(dayChoice > 6)
			{
				dayChoice = (dayChoice % 7);
			}
		}
		return dayNameArray[dayChoice];
	}

	
}


But no matter what it always comes up as the original day that was selected. Why isn't the dayChoice value changing?
Ok, try this:

1
2
3
4
5
6
7
8
9
string DayOfTheWeek::addDays(int plusDays)
{
    dayChoice+=plusDays;
    dayChoice%=7;
    dayChoice+=7;
    dayChoice%=7;

    return dayNameArray[dayChoice];
}
that works like a charm roshi! I will use that, thank you sensei.

so, just so I know what's going on,

we add plusDays to dayChoice, divide day choice by 7, then add 7 to it, then divide it by 7 again?

I have never used %= before, I'm unsure of the phrasing to use to describe what it does.
Suppose dayChoice is 1 and plusDays is 17=7+7+3=7*2+3. We want the resulting dayChoice to be 4. We don't care about how many sevens we have in 17, because when you add seven days you have the same day as the one you had in the beginning. We only care about that 3, and the way to get it is 17%7. So, what we do is:

dayChoice=dayChoice+plusDays%7; //(or dayChoice+=plusDays%7)

But now, dayChoice may be more than 7. We have to add this line to correct it:

dayChoice=dayChoice%7; //(or dayChoice%=7)

But since we perform the modulo operation here, we can skip it in the first step.
So, we can simply replace the above with:

1
2
dayChoice+=plusDays;
dayChoice%=7;

The following two lines are there to take care of the case that plusDays is negative.
awesome, thanks again.
roshi's code is much simplier .. thanks for the help
Topic archived. No new replies allowed.