I could use help with an assignment I'm trying to complete. The goal is to get an output like this
Enter a month (1-12): 2
Enter a year: 2016
29 days
however, I'm new t programming and I can't piece together what exactly I'm missing or did wrong. Any and all assistance is appreciated.
#include<iostream>
using namespace std;
int main()
{
cout <<"Enter a month and a year " << endl;
int year = 0;
int month = 0;
int days;
cout << "Enter a month: ";
cin >> month;
cout << "Enter a year: ";
cin >> year;
if (month == 4) || month == 6 || month == 9 || month == 11)
days = 30;
else if (month == 02)
{
bool leapyear = ((year%400==0) || ((year%100!=0)&&(year%4==0)));
if leapyear==0
days = 28;
else
days = 29;
}
else
days = 31;
#include <iostream>
int main()
{
// get the month from the user
int month = 0;
std::cout << "Enter a month[1-12]: " ;
std::cin >> month;
// sanity check that we have a valid month
if( month < 1 || month > 12 )
{
std::cout << "invalid month\n" ;
return 1 ; // return without doing anything more
}
int days = 31; // all months have 31 days
// except that these months have 30 days
if( (month == 4) || (month == 6) || (month == 9) || (month == 11) ) days = 30 ;
// and february has either 28 or 29 days
elseif( month == 2 )
{
// february, so we need to check if it is a leap year
int year ;
std::cout << "Enter the year: " ;
std::cin >> year ;
constbool leapyear = ( (year%400==0) || ( (year%100!=0) && (year%4==0) ) );
if(leapyear) days = 29 ;
else days = 28 ;
}
// print out the computed number of days
std::cout << days << " days.\n" ;
}