C++ 输入不传递到函数中
我试图编写一个代码来检查文本文件中日期和天数的有效性。因此,在使用文本文件作为输入之前,我尝试编写代码,不使用文本文件作为输入,以查看是否有效。下面是我的代码C++ 输入不传递到函数中,c++,function,date,C++,Function,Date,我试图编写一个代码来检查文本文件中日期和天数的有效性。因此,在使用文本文件作为输入之前,我尝试编写代码,不使用文本文件作为输入,以查看是否有效。下面是我的代码 #include<iostream> using namespace std; bool isLeap(int year) { const int MAX_VALID_YR = 2100; const int MIN_VALID_YR = 1800; return (((year%4==0) &
#include<iostream>
using namespace std;
bool isLeap(int year)
{
const int MAX_VALID_YR = 2100;
const int MIN_VALID_YR = 1800;
return (((year%4==0) && (year%100!=0)) || (year%400==0));
}
bool isValidDate(int d, int m, int y)
{
const int MAX_VALID_YR = 2100;
const int MIN_VALID_YR = 1800;
if (y > MAX_VALID_YR || y < MIN_VALID_YR)
return false;
if (m < 1 || m > 12)
return false;
if (d < 1 || d > 31)
return false;
if (m == 2)
{
if (isLeap(y))
return (d <= 29);
else
return (d <= 28);
}
if (m==4 || m==6 || m==9 || m==11)
return (d <= 30);
return true;
}
bool isLeap(int);
bool isValidDate(int,int,int);
int main()
{
int date, dd, mm, yy, years;
const int MAX_VALID_YR = 2100;
const int MIN_VALID_YR = 1800;
yy=years;
cout<<"Welcome, You Can Use This To Check Valid Date"<<endl;
cout<<"Insert Date"<<endl;
cin>>dd;
cout<<"Insert Month"<<endl;
cin>>mm;
cout<<"Insert Year"<<endl;
cin>>yy;
isLeap(years);
isValidDate(dd, mm, yy);
cout<<"This Is the Result:\t"<<dd<<mm<<yy<<endl;
}
我知道这不是一个好代码,我还在学习。希望您能进一步获得一些提示和建议。在您的主要功能中,您可能有if情况
if (isValidDate(dd, mm, yy)){
cout<<"Valid Date";
}
else
cout<<"Invalid Date";
替换此项,而不是主函数中的最后两行。函数有效。d,mm,yy返回正确的值,但您没有打印其结果。您只需打印从用户处获得的任何输入。在你的代码中使用这个
if (isValidDate(dd, mm, yy))
{
cout<<dd<<mm<<yy<<" is valid Date"<<endl;
}
else
{
cout<<dd<<mm<<yy<<" is invalid Date"<<endl;
}
您的函数返回true或false,但您还没有编写一个方法来执行这些函数返回true/false时发生的操作。您只调用了main方法中的函数。对布尔结果使用if条件和cout。祝你好运:改变这个
cout<<"This Is the Result:\t"<<dd<<mm<<yy<<endl;
对此
cout<<"This Is the Result:\t"<<isValidDate(dd, mm, yy) ? "Input is Valid":"Input is Invalid"<<endl;
当你把一天、一个月、一年放在一行中,用空格隔开时,它能起作用吗?返回isValidDate后,你不会做任何事情。您期望的输出是什么?拼写;语法降噪。@Mathieu de Lorimier我想输出日期是否有效。BTW谢谢你的回答:“史塔克还没有尝试,但我认为这是另一个提示,谢谢:谢谢你的回答:谢谢你的帮助:谢谢你的提示:谢谢你回答这个问题!”
cout<<"This Is the Result:\t"<<isValidDate(dd, mm, yy) ? "Input is Valid":"Input is Invalid"<<endl;