传递数组并验证 [英] Passing an array , and validating

查看:65
本文介绍了传递数组并验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,我对程序功能有疑问。假设用户输入一个月和一天存储月份数组中的月份和日期。当用户输入每个日期的日期时,如何通过在number_days_of_months中查找给定月份的最大天数来验证它;如果大于最大值,它将打印错误消息并读取新的一天值,直到它在给定月份有效。

例如:

以mm / dd / yy格式输入日期(以月份输入0结束):

1/1

2/31

第2个月有28天。输入有效日期:30





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.

推荐答案

我不相信这里的任何人会为你编写代码,但很明显你不知道下一步该去哪里。根据你所拥有的,我假设你的老师希望你使用数组和程序,所以这里有一些建议和问题,可以帮助你编写代码。



想想看你想要做什么,并用短句写下来,如下:



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



现在问问自己为什么要将输入的月份或日期放入数组中?你只需要在循环中任何一次都有一个。



接下来,想想你将如何检查日子。一种方法是使用与您一样的数组(number_days_of_months),每个月包含一个元素,其中包含该月的最大天数。那么你想要的是比较dayEntered与monthEntered的number_days_of_months。您可以在if语句中轻松完成此操作,但由于您必须使用过程,因此这可能是使用过程的好地方。



现在,考虑如何使用该程序 - 它将在if语句中使用,如果Days为1到最大日,则应返回true输入的月份和否则为假。这告诉你返回类型不是void。



既然你知道它需要做什么,想一想它需要做什么来确定什么参数你需要传递给它。您需要每个月的日期,月份和天数。你拥有的是非常接近的,你现在应该能够弄清楚如何改变它。



最后,把它们放在一起你可能会感到惊讶它应该这样做。


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.


这篇关于传递数组并验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆