Completely lost. Function program

Here is the problem I have:
Write a void function called SeasonPrint that take int parameters representing a month and a day and that outputs to cout the name of the season. For the purposes of this exercise, spring begins on March 21, summer begins June 21, fall begins September 21, and winter begins December 21. Note that the year begins and ends during winter. The function can assume that the value in the month and day parameters have been validated before it is called.


Alright, this is the program I started writing.

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
#include <iostream>

using namespace std;

void SeasonPrint (int, int); //prototype

void main ()

{
//Variable declarations
int month;

int day;

int s;

cout << "Enter a month (ex. Jan = 1)." << endl;

cin >> month;

cout << "Enter a day (ex. 1-31)." << endl;

cin >> day;

SeasonPrint (month, day);
{	
	if (month <=3 && day <= 20)
	cout << "Season is Winter." << endl;
	else (month > 3 && month <= 6) && (day <= 20)
	 {
			cout << "Season is Spring."
	}
}

//function definition

void SeasonPrint (int month, int day) // double a, double b are parameters.

{

return;

}


I'm just completely confused and lost. I don't know if I'm heading in the right direction or not. I read the book a million times and it's just not making sense to me. Any help?
Your on the right track. a void function doesnt return anything at all. so your function declaration would be something like this

1
2
3
4
5
6
7
8
void SeasonPrint (int month, int day) // double a, double b are parameters.
{

 /*in here you can do if else statements like you had above to check to see if the month and  day are spring etc. then just cout whatever it does like you did above. all the stuff you did above should be inside the declaration*/

return; // delete this line. you will get an error.

}


in the main you would do someting like this

SeasonPrint(1,10); //jan 10th

this should print to the screen winter

\
hope this helped
Ok, lines 26 onward look like you are confused.

Look at your other post, the one with the min, max, and middle. In there you've correctly declared functions and called them.

The real challenge in this program is coming up with an algorithm that correctly determines the season. If you are having trouble doing that, then here's some advice:

Forget programming for the moment. Pretend like you were explaining to a friend how to figure out the season. Explain it using words and phrases like "if", "is less than", and "is between".
Write down your explanation, and then try to re-write the explanation in C++ code.

Example:

Problem: Given three integers (call them A, B, and C), determine which one is the largest.

Explanation:
If A is greater than B and A is greater than C, then the largest is A;
Otherwise, if B is greater than A and B is greater than C, then the largest is B;
Otherwise, C must be the largest.

C++ code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
int max3( int A, int B, int C ) {
   // If A is greater than B and A is greater than C, then...
   if( A > B && A > C )
       // The largest is A
       return A;
   // Otherwise, if B is greater than A and B is greater than C, then...
   else if( B > A && B > C )
       // The largest is B
       return B;
   // Otherwise, ...
   else
       //  C must be the largest
       return C;
}

First of all, thank you both for helping me. I appreciate it so much.

This is the new code I wrote. Many errors though. I'm going to proof read it some more, but any input would be great.

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
#include <iostream>

using namespace std;

void SeasonPrint (int, int); //prototype

void main ()

{
//Variable declarations
int month;

int day;

int s;

cout << "Enter a month (ex. Jan = 1)." << endl;

cin >> month;

cout << "Enter a day (ex. 1-31)." << endl;

cin >> day;

//function definition
void SeasonPrint (int month, int day);
{
   
   if( month <= 3 && day <= 20)
       
       return "The season is Winter.";

   else if (month >= 3 && <=6 && day <= 20)
	   return "The season is Spring.";
   
   else if (month >=6 && <=9 && day <= 20)
	   return "The season is Summer.";

   else if (month >=9 && <=12 && day <=20)
	   return "The season is Autumn.";

   else
       return "Enter a month in between 1-12 and a day inbetween 1-31.";
}



return;

}
Okay, i know i'm probably doing something wrong with the else if part. There's no way there can be that much information without something going wrong.
replace every "return" inside the function with "cout <<" because you are still trying to return a string in this case. so just erase the return part and cout << .


also since you are using void main at the top you dont have to return at the bottom. your code should look like this

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
#include <iostream>

using namespace std;

void SeasonPrint (int, int); //prototype

void main ()

{
//Variable declarations
int month;

int day;

int s;

cout << "Enter a month (ex. Jan = 1)." << endl;

cin >> month;

cout << "Enter a day (ex. 1-31)." << endl;

cin >> day;

SeasonPrint(month,day);

system("pause");
}

//function definition
void SeasonPrint (int month, int day);
{
   
   if( month <= 3 && day <= 20)
       
       cout <<"The season is Winter.";

   else if (month >= 3 && <=6 && day <= 20)
	   cout <<"The season is Spring.";
   
   else if (month >=6 && <=9 && day <= 20)
	   cout <<"The season is Summer.";

   else if (month >=9 && <=12 && day <=20)
	   cout <<"The season is Autumn.";

   else
       cout <<"Enter a month in between 1-12 and a day inbetween 1-31.";
}


you also didnt call the function u just created it.
Ah, okay I see! It's still giving me some sort of error though. Something with the bracket under the function definition. It's saying it is missing a function header. I tried switching the one, but then a whole bunch of other errors come up.
ahh i see it. remove the semi-colon on line 31
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
#include <iostream>

using namespace std;

void SeasonPrint (int, int); //prototype

void main ()

{
//Variable declarations
int month;

int day;

int s;

cout << "Enter a month (ex. Jan = 1)." << endl;

cin >> month;

cout << "Enter a day (ex. 1-31)." << endl;

cin >> day;

SeasonPrint(month,day);

system("pause");
}

//function definition
void SeasonPrint (int month, int day); // <-------------------------------------
{
   
   if( month <= 3 && day <= 20)
       
       cout <<"The season is Winter.";

   else if (month >= 3 && <=6 && day <= 20)
	   cout <<"The season is Spring.";
   
   else if (month >=6 && <=9 && day <= 20)
	   cout <<"The season is Summer.";

   else if (month >=9 && <=12 && day <=20)
	   cout <<"The season is Autumn.";

   else
       cout <<"Enter a month in between 1-12 and a day inbetween 1-31.";
}

oh okay So semi colons can do that? It makes the compiler think that there is an open bracket?
Once I remove that semi-colon, I get all of these errors regarding the '<='. Should I be placing the month before that?

Edit: yeah if you add the month <= 12 and so on, it works perfectly! Thank you so much. you're a life saver, haha.
Last edited on
ok your if statements are wrong.

month >= 3 && <=6 && day <= 20 is wrong

month >= 3 && month <=6 && day <= 20 this is correct

you forgot to compare the month. I know it sounded right

month greater or equal to 3 and less than or = to 6

but it should be month >= 3 and "month" <= 6 etc

heres the code corrected

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
#include <iostream>

using namespace std;

void SeasonPrint(int, int); //prototype

void main ()

{
//Variable declarations
int month;

int day;

int s;

cout << "Enter a month (ex. Jan = 1)." << endl;

cin >> month;

cout << "Enter a day (ex. 1-31)." << endl;

cin >> day;

SeasonPrint(month,day);

system("pause");
}

//function definition
void SeasonPrint(int month, int day) // <-------------------------------------
{ 
   if( month <= 3 && day <= 20)
       cout <<"The season is Winter.";

   else if(month >= 3 && month <= 6 && day <= 20)
	   cout <<"The season is Spring.";
   
   else if(month >= 6 && month <= 9 && day <= 20)
	   cout <<"The season is Summer.";

   else if(month >= 9 && month <=12 && day <=20)
	   cout <<"The season is Autumn.";
   else
	   cout <<"Enter a month in between 1-12 and a day inbetween 1-31.";
}





lol your welcome.
Last edited on
this is the new code I have now:

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
#include <iostream>

using namespace std;

void SeasonPrint (int, int); //prototype

void main ()

{
//Variable declarations
int month;
int day;

cout << "Enter a month (ex. Jan = 1)." << endl;

cin >> month;

cout << "Enter a day (ex. 1-31)." << endl;

cin >> day;

SeasonPrint(month,day);
}

//function definition
void SeasonPrint (int month, int day)
{
   if( month <= 3 && day <= 31)
	   if (month ==3 && day >= 21)
		   cout << "The season is Spring." << endl;
	   else       
       cout <<"The season is Winter." << endl;

   else if (month >= 3 && month <=6 && day <= 20)
	   cout <<"The season is Spring." << endl;
   
   else if (month == 6 && day >= 21)
	   cout << "The season is Summer." << endl;
   
   else if (month >=6 && month <=9 && day <= 20)
	   cout <<"The season is Summer." << endl;

   else if (month == 9 && day >=21)
	   cout << "The season is Autumn." << endl;

   else if (month >=9 && month <=12 && day <=20)
	   cout <<"The season is Autumn.";

   else if (month == 12 && day >= 2)
	   cout << "The season is Winter.";

   else
       cout <<"Enter a month in between 1-12 and a day inbetween 1-31.";
}


I added the other ones because if it's September 21 or more it's supposed to say it's the next season. So I added those. It works exactly how it's supposed to now :)
BTW, don't use void main(), use int main().
FYI... You have a typo on line 49.
Topic archived. No new replies allowed.