Hello , I am a noob in C++ coding and wondered if anybody here can please help me, I would really appreiate it

I have to create a separate function within a program to check if the date entered by the user match the month he entered E.G= if he entered April that can only be 30 days and not 31 or if he entered February it can only be 29 ,etc

could please some one show me how to do it, I tried but failed miserable, could also show the function calling

thanks for the help

We love to help people with their problems, why don't you post what you tried and what you think needs fixed (or why it didn't work) and we will help you fix it.

OK thanks for the time

my code is
ps I wont put the whole code because is too big I will add only if needed
thanks for understanding


this is the part of the code that i think is relevant

if (buf.good())
{
	buf >> inday;
}

numofdays = daycheck(inday);

// Check if user input is corrected for day taking into
//account the month so the days (30, 31 or 28) match the month,
//also takes into account that February changes in a leap year


isday = inday > numofdays;

if (isday)
{
cout << "Only " << numofdays << " in  month " << inmonth << "." << endl;
}


	wrongdate = ((isyear) || (ismonth) || (isday));
}
while(wrongdate);



and next is my the function I called up the daycheck(int month)

//Checks if the for the month the user entered the amount of days is correct
//Again it takes into account if the year entered is a leap year so it makes the necessary corrections to February

int daycheck (int months)
{

	bool isleapyear;
	int daysofmonth,febs,years;


isleapyear = ((years%4==0)&&(years%100!=0)) || (years%400==0);

	if(isleapyear)
	{
		febs = 29;
	}

	else
	{
		febs = 28;
	}

	switch (months)
	{
		case 1:
		daysofmonth = 31;
		break;

		case 2:
		daysofmonth = febs;
		break;

		case 3:
		daysofmonth = 31;
		break;

		case 4:
		daysofmonth = 30;
		break;

		case 5:
		daysofmonth = 31;
		break;

		case 6:
		daysofmonth = 30;
		break;

		case 7:
		daysofmonth = 31;
		break;

		case 8:
		daysofmonth = 31;
		break;

		case 9:
		daysofmonth = 30;
		break;

		case 10:
		daysofmonth = 31;
		break;

		case 11:
		daysofmonth = 30;
		break;

		default:
		daysofmonth = 31;
		break;
	}
	return daysofmonth;

}

I checked and i declared all the variables, the thing is the program run and all my other checks are right but this one


again thanks for the time and I would appreciate so much if some one could help me

I think it's easier to use an array instead of a switch:

int days_in_month ( int month, int year )
{
  int month_days[] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
  int days = month_days[month - 1];

  if ( month == 2 && is_leap ( year ) )
    ++days;
  
  return days;
}

I think your problem is in the call numofdays = daycheck(inday); daycheck wants the month, not the day.

I think your problem is in the call numofdays = daycheck(inday); daycheck wants the month, not the day.

dude thank you so much it worked, just a simple change

really thanks, I reaaaaaaaaaaaaaaaalllllyy appretiate it


and thanks to give the array option , but since the change was simpler with the switch I used that, thanks anyway

This article has been dead for over six months. Start a new discussion instead.