increment months? help plz

I can't figure out how to manipulate the month argument inside the function so that when it gets to the last day of the month, it will return a new month value, (e.g. 1/30 1/31 2/1 2/2 etc.) the if statement I used holds up for the day of the month but doesn't actually change ( my line "arg = 2" or it could be arg++ arg + 1, etc. does nothing for me)

Any suggestions? do you think a switch statement is the wrong approach? I'm just writing a program that prompts a date to be entered, then lists the next 10 calendar dates, I know February and December is going to get tricky but if I could just get the basic months down I'd be a happy camper. Any help is greatly appreciated!

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
32
33
34
35
36
37
38
39
40
41
42
43
44

#include <iostream>
using namespace std;

int NEWDAY(int, int, int );

int main()
{
	int m, d, y;

	cout << "Enter month: ";
	cin >> m;
	cout << "Enter day: ";
	cin >> d;
	cout << "Enter year: ";
	cin >> y;

	
	for (int j = 0; j < 10; j++)
	{
		d = NEWDAY(m, d, y);
		cout << m << "/" << d << "/" << y << endl;
	}

	system("pause");
	return 0;
}

int NEWDAY(int arg, int arg2, int arg3)
{
	switch (arg)
	{
	case 1:

		for (int i = 0; i < 10; i++)
		{
			arg2 = arg2 + 1;
			if (arg2 > 31)
				arg2 = 1;
				arg = 2;
			return arg, arg2;
		}
	}
}
You can't return 2 values. Why dont you pass them by reference? This way, there values will change within main as well.

Also, you haven't taken into account the year in NEWDAY function. What if the user enters in "12 31 1999"? The year should change to 2000, but you do not do that in your function.
that's what I've been missing, "passing by reference" I was just googling "how to return multiple values c++ function etc" but no good threads or tutorials were coming up.. I must learn this "pass by reference"!

anyone out there have the time & energy to drop a quick example or link? I'm going to check the sites tutorials/references but I have a hard time learning by reading alone (I know that's not good lol) but I want to get my hands dirty and sometimes it's hard to even start by just trying to conceptually understand something

would it look something like this?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#include <iostream>
using namespace std;

void NEWDAY(int &, int &, int &)

int main()
{
        int m, d, y;
        .......       
        NEWDAY(m, d, y);
        .....
}

void NEWDAY (int &arg, int &arg2, int &arg3)
{....}


I mean, that simple for the most part? are there some things I should keep in consideration?

simple example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#include<iostream>

void doSomething(int& x)
{
	// increment input reference by one
	x++;
}


int main() 
{
	int myNumber = 4;

	doSomething(myNumber);

	std::cout << "My number after function call: " << myNumber << std::endl;

	return 0;
}


run this, then run again after taking out the '&' on line 3.

edit:
void NEWDAY (int &arg, int &arg2, int &arg3)

I would also give your function parameters sensible names. It'll make the logic inside your function easier to follow.
Last edited on
awesome, thanks guys! I'm almost through the bulk of it now, at least I have something running, now to tackle the end of the year... then the dreaded leap year (but I have some math to help me there = divisible by 4, but not if they're divisible by 100, unless they're also divisible by 400 lol screwy leap years) but with the december to january of a new year, I keep getting..

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Enter month: 12
Enter day: 29
Enter year: 1999
The next ten dates are:
12/30/1999
12/31/1999
1/1636876/2000
2/1/2000
2/2/2000
2/3/2000
2/4/2000
2/5/2000
2/6/2000
2/7/2000
Press any key to continue . . .


from my code..

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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#include <iostream>
using namespace std;

int NEWDAY(int &months, int &days, int &years);//function prototype

int main()
{
	int m, d, y;				//declare variables

	cout << "Enter month: ";	//prompt inputs
	cin >> m;
	cout << "Enter day: ";
	cin >> d;
	cout << "Enter year: ";
	cin >> y;

	cout << "The next ten dates are:\n";
	for (int j = 0; j < 10; j++)
	{
		d = NEWDAY(m, d, y);
		cout << m << "/" << d << "/" << y << endl;
	}

	system("pause");
	return 0;
}

int NEWDAY(int &arg, int &arg2, int &arg3)
{
	switch (arg)
	{
	case 1:
		while (arg2 < 31)
		{
			for (int i = 0; i < 10; i++)
			{
				arg2 = arg2 + 1;
				return arg2;
			}
		}
		arg2 = 0;
		arg = 2;

//	case 2: ..... etc etc etc all the way to 12

        case 12:
		while (arg2 < 31)
		{
			for (int i = 0; i < 10; i++)
			{
				arg2 = arg2 + 1;
				return arg2;
			}
		}
		arg2 = 0;//day 
		arg = 1;//month
		arg3++;//year, should I be returning something here
	}
}


obviously I'm missing some know how here, my month variable is incrementing twice, my day variable changes value to the year for one increment then begins the day loop at 1 again (28, 29, 30, 31, 2001, 1, 2, 3).. and my year seems alright somehow.

I'm going to keep messing with it but I can't seem to figure out why I'm having such a hard time manipulating all 3 variables at once at the end of the year
Last edited on
I'm just looking at it like that
switch(arg)
is it something like this only takes into account arg(month)? and so when I change the year, it's not actually resetting the switch statement outside of the month change? Idk I feel like I'm just missing a technicality here maybe an entire loop outside of the switch statement for the years? i might be getting weird here lol I'll keep trying
The reason you are getting a garbage value for day in month 12 is because you are not returning anything. In main you are assigning the return value to d, but since you dont return anything (in case 12), d just prints the garbage that its holding.

You need to get rid of all these return statements. And if you are passing by reference, there is no need to return anything. Simply call the function, manipulate the variables, and then print them out in main.

Having for loops and while loops to increment arg2 doesn't make sense if you are just returning after the first iteration. Replace the for loops with a simple increment and the while loop with an if(arg2<31).

Also, you need to have break statements after each case so multiple cases aren't executed.

Last edited on
In the main function, your function call inside the for-loop should look like this:
1
2
3
4
5
	for (int j = 0; j < 10; j++)
	{
		NEWDAY(m, d, y); // notice the change. No assignment to d, just a simple functon call
		cout << m << "/" << d << "/" << y << endl;
	}



The function will be called 10 times. For each call, manipulate the variables in the NEWDAY function for each case using if and else statements (no while or for-loops needed), and then break. No need to return anything. When the break statement is reached, program will break out of the switch statement and return to main. The modified variables will then be printed out in main.
Last edited on
Topic archived. No new replies allowed.