Adding year

question solved! ty.

Last edited on
What kind of error are you getting? I think it has to do with the fact that your size of the array is not initialized with a constant or something. I took everything out relating to the year and this runs perfectly fine. If you want to add your year function, I can take a look at that.

In my mind this is what this should look like.

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

using namespace std;

int main ()
{
// Declarations of variables and arrays with sizes.
int num;
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string month[12] = {"Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};

string choice;

do
{

cout << "Please enter a numerical month number (1-12) ";
cin >> num;

if(num > 0 && num <= 12)
{
// Decrement by 1.
num--;
// Display month and the number of days.
cout << month[num] << " has " << days[num] << endl;
// Prompt for continuance.
cout << "Would you like to run program again? (yes/no) ";
cin >> choice;
}
else
{
// Error message
cout << "Please enter a numerical month number 1-12"<< endl << endl;
// Prompt
cout << "Would you like to continue? (yes/no) ";
cin >> choice;
}
cout << endl;
} while ( choice != "no");

return 0;
}
Last edited on
Thank you so much for the help. I have the year in because the original code needed to request a year from the user if they entered the number (2). If the year is entered then the program needs to check to see if it is a leap year and if that is a true statement then one will be added to the day...so 29 rather than 28 for the display of Feb.
This is the code I had originally:

if (num ==2)
{
cout <<"Please enter a year (xxxx);
cin>>year;
}
if (year %4==0)
{
if (year %100==0)
{
if (year %400==0)
cin>>days +=1;

return 0;


Hopefully the comments I wrote in the source code explain to you what is going on. It completely works, but I want to make sure that you understand the code itself.
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
60
61
62
63
64
#include <iostream>
#include <string>

using namespace std;

int main ()
{
// Declarations of variables and arrays with sizes.
int num,
	leapYear,
	year;
int days[12] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
string month[12] = {"Jan", "Feb", "March", "Apr", "May", "June", "July", "Aug", "Sept", "Oct", "Nov", "Dec"};

string choice;

do
{

cout << "Please enter a numerical month number (1-12) ";
cin >> num;

if(num > 0 && num <= 12)
{
	// Nested decision statements.
	if (num == 2)
	{
	// Decrement by 1.
	num--;
	// Prompt
	cout << "Please enter a year ";
	cin >> year;
	// Determines the year entered is a leap year by modular division.
	leapYear = year % 4;
	// Determines how many days is in the month and outputs.
	if (leapYear == 0)
		cout << month[num] << " has " << days[num] + 1 << endl;
	else
		cout << month[num] << " has " << days[num] << endl;
	}
	else
	{
	// Decrement by 1.
	num--;
	// Display month and the number of days.
	cout << month[num] << " has " << days[num] << endl;
	}
// Prompt for continuance.
cout << "Would you like to run program again? (yes/no) ";
cin >> choice;
}
else
{
// Error message
cout << "Please enter a numerical month number 1-12"<< endl << endl;
// Prompt
cout << "Would you like to continue? (yes/no) ";
cin >> choice;
}
cout << endl;
} while ( choice != "no");

return 0;
}
Last edited on
Topic archived. No new replies allowed.