Click here to Skip to main content
15,909,437 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
Hi guys, I have a question on procedure function. Let's say the user enter a month and a day storing the month in the array of months and day on days array. As the user enters the day of each date, how do I validate it by looking up in number_days_of_months for the maximum number of days in the given month; if larger than the maximum, it will print an error message and read a new day value until it's valid for the given month.
Example:
Enter dates in mm/dd/yy format (end by entering 0 for month):
1/1
2/31
Month 2 has 28 days. Enter a valid day: 30


#include <iostream>
using namespace std;
void prodedure(int number_days_of_months[], int days_month, int days[], int size_days, int months[], int months_size);

int main()
{

	int months[10]; //stores the month enter by the user 
	int days[10];   //stores the days enter by the user
	int number_days_of_months[12] = { 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31 }; //representing each of the 12 months of the year 




	prodedure(number_days_of_months, 12, days, 10, months, 10);






	return 0;
}



void prodedure(int number_days_of_months[], int days_month, int days[], int size_days, int months[], int months_size)
{

	for (int i = 0; i < months_size; i++)
	{

		cout << "Enter dates in mm/dd format Example 5 31 (end by entering 0 for month):" << endl;
		cin >> months[i] >> days[i];
		
		



	}
	


		
}

		









void  display()
{







}


What I have tried:

I have try comparing them , but its not working.
Posted
Updated 6-May-17 17:27pm
Comments
Patrice T 6-May-17 22:44pm    
'I have try comparing them , but its not working.'
You have problem in code you didn't showed.
[no name] 6-May-17 22:57pm    
I tried reading a question but it's not working.

1 solution

I don't believe anyone here is going to write your code for you, but it's clear you don't know where to go next. From what you have I assume your teacher wants you to use an array and a procedure so here are some suggestions and questions that might help you write the code.

Think about what you are trying to do and write it down in short sentences, something like this:

input a month.
if the month is 0, exit the program
if the month is not in the range of 1 to 12, print an error and go back and ask for another month
input a day.
if the day is greater than the number of days in the month print an error and ask for day again otherwise print month/day.
go back and start over

Now ask yourself why would you put the entered month or day into an array? You are only ever going to have one of each at any one time through the loop.

Next, think about how you would check the days. One way is to use an array like you did(number_days_of_months) with an element for each month that contains the maximum days for that month. Then what you want is to compare the "dayEntered to number_days_of_months for the monthEntered". You could easily do this in the if statement but since you have to use a procedure this might be a good place to use one.

So now, think about how that procedure will be used - it will be used in an if statement and should return "true" if Days is from 1 to max days for the month entered and "false" otherwise. That tells you the return type is something other than void.

Now that you know what it needs to do, think about what it needs to do that to determine what parameters you need to pass to it. You need the day, the month and the number of days in each month. What you have is pretty close and you should now be able to figure out how to change it.

Finally, put it all together and you may be surprised just how easy it is to do.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900