Hellp all! Hoping to make new friends here:

Anyways,

I am new to C++ and to programming. I just started reading the book "Problem Solving with C++, 7th ed." by Walter Savitch. I am already having problems. One of the programs I am trying to figure out (not in the book) is how to figure for how many days a month has. The problem I am having is with leap year (Feb). Here is what I have so far:

#include <iostream>
using namespace std;
int main()
{
	int Month, Year;

	cout << "Enter a number matching the month \n";
	cout << "of the year, then press return\n";
	cin >> Month;
	cout << " \n";

	switch (Month)
	{
	case 1:
			cout << "There are 31 days in January.\n";
			break;
	case 3:
			cout << "There are 31 days in March.\n";
			break;
	case 4:
			cout << "There are 30 days in April.\n";
			break;
	case 5:
			cout << "There are 31 days in May.\n";
			break;
	case 6:
			cout << "There are 30 days in June.\n";
			break;
	case 7:
			cout << "There are 31 days in July.\n";
			break;
	case 8:
			cout << "There are 31 days in August.\n";
			break;
	case 9:
			cout << "There are 30 days in September.\n";
			break;
	case 10:
			cout << "There are 31 days in October.\n";
			break;
	case 11:
			cout << "There are 30 days in November.\n";
			break;
	case 12:
			cout << "There are 31 days in December.\n";
			break;

	case 2:
			cout << "Please enter the year.\n";
			cin >> Year;
			break;

	if (Year % 4 == 0) 
		cout << "February has 29 days. This is a Leap Year\n";
	else
		cout << "February has 28 days. \n";
	break;

	default:
		cout << "Please select from 1-12. There are only 12 months in the year. Thank you.\n";
	}

	cout << " \n";

	char letter;
	cout << "Enter a letter to end the program:\n";
	cin >> letter;

	cout << "Goodbye.\n";

	return 0;
}

Dani AI

Generated

Immediate cause of the original bug: the leap-year check never ran because a stray break forced the switch to exit right after reading the year. pointed this out; removing that early break (or moving the leap test ahead of it) fixes the runtime flow. correctly applied the century rule for leap years (century years are leap only when divisible by 400), which handles edge cases like 1900 vs 2000.

A concise, maintainable approach is to separate concerns: (1) validate the month, (2) handle the common months with grouped switch cases to avoid repetition, and (3) for February prompt for the year and call a small helper that implements the leap rule. Example pattern:

switch (month) {
  case 1: case 3: case 5: case 7: case 8: case 10: case 12:
    cout << "31 days\n"; break;
  case 4: case 6: case 9: case 11:
    cout << "30 days\n"; break;
  case 2: {
    int year;
    cout << "Enter year: ";
    cin >> year;
    cout << (isLeap(year) ? "February has 29 days\n" : "February has 28 days\n");
    break;
  }
  default:
    cout << "Please select from 1-12.\n";
}

Practical notes and troubleshooting tips: declare variables carefully inside case blocks (use braces) or declare them before the switch to avoid scope/compile issues. Prefer std::cin.get() or std::getline() to pause instead of system("pause") or nonstandard <conio.h> — those are platform-specific. Always validate input (check cin.fail() and the numeric range for month/year) and enable compiler warnings (e.g., -Wall -Wextra) while testing with edge years (1900, 2000, 2004) to confirm the leap logic behaves as expected.

Recommended Answers

All 6 Replies

Hellp all! Hoping to make new friends here:

Anyways,

I am new to C++ and to programming. I just started reading the book "Problem Solving with C++, 7th ed." by Walter Savitch. I am already having problems. One of the programs I am trying to figure out (not in the book) is how to figure for how many days a month has. The problem I am having is with leap year (Feb). Here is what I have so far:

#include <iostream>
using namespace std;
int main()
{
int Month, Year;

cout << "Enter a number matching the month \n";
cout << "of the year, then press return\n";
cin >> Month;
cout << " \n";

switch (Month)
{
case 1:
cout << "There are 31 days in January.\n";
break;
case 3:
cout << "There are 31 days in March.\n";
break;
case 4:
cout << "There are 30 days in April.\n";
break;
case 5:
cout << "There are 31 days in May.\n";
break;
case 6:
cout << "There are 30 days in June.\n";
break;
case 7:
cout << "There are 31 days in July.\n";
break;
case 8:
cout << "There are 31 days in August.\n";
break;
case 9:
cout << "There are 30 days in September.\n";
break;
case 10:
cout << "There are 31 days in October.\n";
break;
case 11:
cout << "There are 30 days in November.\n";
break;
case 12:
cout << "There are 31 days in December.\n";
break;

case 2:
cout << "Please enter the year.\n";
cin >> Year;
break;

if (Year % 4 == 0)
cout << "February has 29 days. This is a Leap Year\n";
else
cout << "February has 28 days. \n";
break;

default:
cout << "Please select from 1-12. There are only 12 months in the year. Thank you.\n";
}

cout << " \n";

char letter;
cout << "Enter a letter to end the program:\n";
cin >> letter;

cout << "Goodbye.\n";

return 0;
}

case 2:
			cout << "Please enter the year.\n";
			cin >> Year;
			break;   // delete this line

	if (Year % 4 == 0) 
		cout << "February has 29 days. This is a Leap Year\n";
	else
		cout << "February has 28 days. \n";
	break;

You are breaking out of the code to be executed for February too soon. The code after the first break is not executed, so the if statement is never executed. You already have a break at the end, so delete the first one.

1) Remember to wrap the code with tag code next time.
2) Your algorithm isn't right. Please visit here ^^ http://en.wikipedia.org/wiki/Leap_year

#include <iostream>
using namespace std;
int main()
{
	int month, year;

	cout << "Enter a number matching the month \n";
	cout << "of the year, then press return\n";
	cin >> month;
	cout << " \n";

	switch (month)
	{
		case 1:
			cout << "There are 31 days in January.\n";
			break;

		case 2:
			cout << "Please enter the year.\n";
			cin >> year;		

			if ( (year % 400 == 0) || (year % 4 == 0 && year % 100 != 0) )
				cout << "February has 29 days. This is a Leap year\n";
			else
				cout << "February has 28 days. \n";
			break;

		case 3:
			cout << "There are 31 days in March.\n";
			break;

		case 4:
			cout << "There are 30 days in April.\n";
			break;

		case 5:
			cout << "There are 31 days in May.\n";
			break;

		case 6:
			cout << "There are 30 days in June.\n";
			break;

		case 7:
			cout << "There are 31 days in July.\n";
			break;

		case 8:
			cout << "There are 31 days in August.\n";
			break;

		case 9:
			cout << "There are 30 days in September.\n";
			break;

		case 10:
			cout << "There are 31 days in October.\n";
			break;

		case 11:
			cout << "There are 30 days in November.\n";
			break;

		case 12:
			cout << "There are 31 days in December.\n";
			break;
		default:
			cout << "Please select from 1-12. There are only 12 months in the year. Thank you.\n";
	}

	cout << " \n";

	char letter;
	cout << "Enter a letter to end the program:\n";
	cin >> letter;

	cout << "Goodbye.\n";

	return 0;
}
cout << " \n"; 
char letter;	

cout << "Enter a letter to end the program:\n";
cin >> letter;
cout << "Goodbye.\n"; 
return 0;
}

I know this probably isn't going to be much help to you, but it's way easier and simpler to, instead of having to type a random letter and press enter, you can either use:

system("pause");

This will print the line, 'Press any key to continue...', so the user doesn't have to press enter.

And by using

#include <conio.h>

and, for example,

cout << "Press any key to do something";
_getch();

That allows you to enter your own custom message to the user, before instantly exiting the program on the key press.

cout << " \n"; 
char letter;	

cout << "Enter a letter to end the program:\n";
cin >> letter;
cout << "Goodbye.\n"; 
return 0;
}

I know this probably isn't going to be much help to you, but it's way easier and simpler to, instead of having to type a random letter and press enter, you can either use:

system("pause");

This will print the line, 'Press any key to continue...', so the user doesn't have to press enter.

And by using

#include <conio.h>

and, for example,

cout << "Press any key to do something";
_getch();

That allows you to enter your own custom message to the user, before instantly exiting the program on the key press.

cin.get() works. it doesn't disrupt the program like system("PAUSE"); and you only need to #include <iostream>

AWESOME!!! Thanks! Thanks!!! Don't know how much I was starting to bust my head with this!!!!! Gonna make some good friends here!!! Glad I joined

Glad we could help :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.