簡體   English   中英

輸入不傳遞給函數

[英]Input doesn't pass into the function

我嘗試編寫一個代碼來檢查文本文件中日期和日期的有效性。 因此,在將文本文件用作輸入之前,我嘗試編寫不帶文本文件的代碼作為輸入以查看是否有效,這是我的代碼。

#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;
} 

我想獲得顯示日期是否有效的輸出,但最終只顯示日期。 我編寫的函數不好還是我的輸出方式錯誤?

By the way, is that possible to check an input dates and days on the current time?

我知道這不是一個好的代碼,我仍在學習。 希望可以進一步獲得一些提示和建議。

函數isValidDate(dd,mm,yy)返回正確的值,但您不打印其結果。 您只是在打印您從用戶那里得到的任何輸入。 在您的代碼中使用它。

if (isValidDate(dd, mm, yy))
{
    cout<<dd<<mm<<yy<<" is valid Date"<<endl;
}
else
{
    cout<<dd<<mm<<yy<<" is invalid Date"<<endl;
}

在您的主要職能中,您可能會有一個if條件。

if (isValidDate(dd, mm, yy)){
    cout<<"Valid Date";
}
else
    cout<<"Invalid Date";

將其替換為主函數中的最后兩行,而不是最后兩行。

您的函數返回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;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM