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;
}

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.