2012-11-27 37 views
0

嗨,我希望有人可以幫我解決我做錯了什麼。這是我要指出的作業,但需要有人告訴我我是一個什麼樣的白癡,因爲我沒有看到我做錯了什麼。C++驗證日期而不使用類

我應該讓用戶輸入一個月,一天和一年來確定它是否是一個神奇的日期。我已經爲另一個項目完成了這部分工作。我應該回去使用名爲isDateValid的函數驗證日期。我很確定我錯過了一些明顯的東西,可以使用任何人的幫助。謝謝!

問題是當我運行它我總是得到它是一個無效的日期!

#include <iostream> 
using namespace std; 

bool leapYear(int); 
bool isDateValid(int, int, int); 

int main() 
{ 
int month, day, year; 

cout << "Is the date you are thinking of a magic one? Let's see...\n"; 
cout << "What is the month in numeric form only? "; cin >> month; 
cout << "What is the day of the month? "; cin >> day; 
cout << "What is the four digit year? "; cin >> year; 

if (isDateValid(month, day, year) == false) 
{ 
cout << "The date you entered, "<< month << "/" << day << "/" << 
year << " is NOT a VALID Date.\n"; 
cout << "If you think I am wrong, please feel free to run me again"; 
} 

else if (month * day == year % 100) // if true then do this 
{ 
cout << "The date you entered, " << 
month << "/" << day << "/" << year << " is a magic date!!\n"; 
} 
else // if false then do this 
{ 
cout << "The date you entered, " << month << "/" << day << "/" << 
year << " is NOT a magic date!!\n"; 
} 

                  //Program ID 
cout <<"\n" << "L5P1LB.cpp\n" << "11-26-12\n" << endl; 
system ("pause"); 
return 0; 
}//end main 

// isDateValid function to validate date entered 
bool isDateValid(int month, int day, int year) 
{ 
bool validation = true; 
if(!(year >= 1600 && year <=2100)) 
    validation = false;  
if(leapYear(year)) 
{ 
    if ((month == 2) && (day > 29)) 
     validation = false; 
    } 
else if(!leapYear(year)) 
{ 
     if ((month == 2) && (day > 28)) 
      validation = false; 
      }  
if((month < 1 && month > 12) || (day < 1)) 
    validation = false; 
if((month == 1) || (month ==3) || (month == 5) || (month == 7) || 
    (month == 8) || (month == 10) || (month == 12) && (day > 31)) 
    validation = false; 
else if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && 
     (day > 30)) 
     validation = false; 
else 
     validation == true; 
return validation; 
} 
// leapYear function to determine if the year is a leap year for validation 
bool leapYear(int year) 
{ 
return(year % 100 != 0 && year % 4 == 0) || (year % 400 == 0); 
} // end leapYear 

編輯

好了,現在我的問題是,我沒有得到,這是不是有效的日期,當它是。它一起跳過驗證,只是運行它的「魔法日期」部分!

這裏是重寫加我的完整代碼,希望在這裏適當縮進,因爲它是在流血。

Description: This program determines if when the user enters a month, 
day, and four digit year which will then be stripped down to its 
two digit year and determined to be a magic date if the day entered 
multiplied by the month entered equals the years two digit format. 
Design: 
    begin main 
     input data 
      Ask user for a month in number form 
      Ask user for a day of the month 
      Ask user for four digit month 
     Validate 
      begin isDateValid 
       if year is between 1600 and 2100, inclusive then return 
        true 
       if month is between 1 and 12, inclusive return true 
       if month is Jan, Mar, May, July, Aug, Oct, Dec 
        then if day is between 1 and 31 inclusive return true 
       if month is Apr, June, Sept, Nov 
        then if day is between 1 and 30 inclusive return true 
       if month is Feb 
        then determine if leap year by running function 
        leapYear provided for us. 
        if leap year is true 
        then if day is between 1 and 29, inclusive return true 
        if not leap year then if day is between 1 and 28, 
        inclusive then return true 
      end isDateValid 
     if isDateValid is true then 
     Calculate 
      if the month times the year is equal to the year modulus 100 
      then output it is a magic date 
      if it does not equal the year in 2 digit form 
      then output that it is not a magic date  
     else output error message 

     Output Program ID 
    end main 

    The output needs to include the books data of 6/10/1960 */ 
#include <iostream> 
using namespace std; 

bool leapYear(int); 
bool isDateValid(int, int, int); 

int main() 
{ 
    int month, day, year; 

    cout << "Is the date you are thinking of a magic one? Let's see...\n"; 
    cout << "What is the month in numeric form only? "; cin >> month; 
    cout << "What is the day of the month? "; cin >> day; 
    cout << "What is the four digit year? "; cin >> year; 

if (isDateValid(month, day, year) == false) 
{ 
    cout << "The date you entered, "<< month << "/" << day << "/" << 
     year << " is NOT a VALID Date.\n"; 
    cout << "If you think I am wrong, please feel free to run me again"; 
} 

else if (month * day == year % 100) // if true then do this 
{ 
    cout << "The date you entered, " << 
     month << "/" << day << "/" << year << " is a magic date!!\n"; 
} 
else // if false then do this 
{ 
    cout << "The date you entered, " << month << "/" << day << "/" << 
     year << " is NOT a magic date!!\n"; 
} 

                  //Program ID 
cout <<"\n" << "L5P1LB.cpp\n" << "11-26-12\n" << endl; 
system ("pause"); 
return 0; 
} //end main 

// isDateValid function to validate date entered 
bool isDateValid(int month, int day, int year) 
{ 
    bool validation = true; // validation set to true 
    // if it is not between 1600 and 2100 then set to false 
    if(!(year >= 1600 && year <=2100)) 
     validation = false; 
    // call leapYear function 
    if(leapYear(year)) 
{ 
    // if February and day is greater than 29 then set to false 
    if ((month == 2) && (day > 29)) 
     validation = false; 
} 
    // else if NOT leapYear 
    else if(!leapYear(year)) 
{ 
    // if February and day is greater then 28 then set to false 
    if ((month == 2) && (day > 28)) 
      validation = false; 
}  
    // if month is less then 1 and over 12 then set to false 
    if((month < 1 && month > 12)) 
      validation = false; 
    // if day is less then 1 then set to false  
    if(day < 1)  
      validation = false; 
    // if month is 1 (Jan), 3 (Mar), 5 (May), 7 (July), 8 (Aug), 10 (Oct), 
    // or 12 (Dec) and day is greater then 31 set to false  
    if((month == 1) || (month ==3) || (month == 5) || (month == 7) || 
     (month == 8) || (month == 10) || (month == 12) && (day > 31)) 
     validation = false; 
    // if month is 4 (Apr), 6 (June), 9 (Sept), or 11 (Nov) and day is 
    // greater then 30 set to false 
    if (((month == 4) || (month == 6) || (month == 9) || (month == 11)) && 
     (day > 30)) 
     validation = false; 
    // else everything that is left set validation to true 
    else 
     validation = true; 
return validation; 
} // End isDateValid 

// leapYear function to determine if the year is a leap year for validation 
bool leapYear(int year) 
{ 
    return(year % 100 != 0 && year % 4 == 0) || (year % 400 == 0); 
} // end leapYear 

對不起,如果我把錯誤的地方或任何東西!

感謝您的幫助。這是真正的讚賞。

+0

你能解決您的代碼的縮進?這很難讀 – emartel

+1

一個顯而易見的事情是聲明'validation == true;'是'=='而不是'='。 – Naveen

+0

這不是錯誤,但是在這一行上:'validation == true;'你可能意味着'validation = true;'但是,你應該刪除它和上面的'else',或者你將要如果最終測試(月份中的天數)超過,則始終將驗證設置爲true。爲了找到實際的錯誤,我建議一次一個地評論每個驗證步驟,以查看哪個導致問題。 – whamma

回答

3

你的日期驗證有一些錯誤。

通過Luchian指出的運算符優先級是一個,但你有幾個人:

  • 您測試month < 1 && month > 12 - 這是不可能的,一個月不能小於1且大於12
  • validation == true沒有做任何事情。 ==是一個比較運算符。幸運的是,這並沒有影響你的功能,因爲validation已經達到這個代碼true

我冒昧地重寫它在我認爲是一個更清潔的方式,但你很可能只是修復month檢查,如果你希望將自己的版本

bool isDateValid(int month, int day, int year) 
{ 
    bool validation = true; 
    if(!(year >= 1600 && year <=2100)) 
     validation = false;  

    if(day < 1) 
     validation = false; 

    switch(month) 
    { 
    case 2: 
     if(leapYear(year)) // We only care about leap years in February 
      if(day > 29) 
       validation = false; 
     else 
      if(day > 28) 
       validation = false; 
     break; 
    case 1: case 3: case 5: case 7: case 8: case 10: case 12: 
     if(day > 31) 
      validation = false; 
     break; 
    case 4: case 6: case 9: case 11: 
     if(day > 30) 
      validation = false; 
     break; 
    default: // the month is not between 1 and 12 
     validation = false; 
     break; 
    } 
    return validation; 
} 
+0

其實我的老師最後得出了同樣的結論,因爲我剛纔看到你的帖子。不過,我想表示非常感謝您花時間爲我清理它。我知道有一種更清晰的方式來寫我正在做的事情,但我不確定最好的方法是什麼。雖然我不會用它來完成這項任務,但我真的希望你知道它有多大幫助我更好地寫作,並且你的努力不是徒勞。我確信這聽起來非常可靠,但意識到我非常感謝你花時間幫助我,並且真正教會了我什麼。謝謝!! –

+0

很高興幫助!這就是爲什麼這個社區在附近!隨意問任何問題,如果版主不喜歡它,你會很快知道如果它的簡單問題,請隨時直接與我聯繫 – emartel

1

||是短路,所以(month == 1) || (month ==3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12) && (day > 31)評估爲true如果滿足第一個條件。您需要添加括號的額外集:

((month == 1) || (month ==3) || (month == 5) || (month == 7) || (month == 8) || (month == 10) || (month == 12)) && (day > 31)

同爲其他月份。

此外,

validation == true; 

是一個空操作,你可能是指validation = true

+0

+1 - 雖然它不是短路,這是問題......只是'&&'的優先級不低於'||'。即使堅持使用結構化編程方法,驗證的初始化爲true也應該移除「validation == true」。 –

+0

首先非常感謝你的幫助。好吧,所以我重寫了代碼以排除最後的else語句,並採用了驗證=真正的部分,並且似乎除了與一個月有關的任何事情之外,我對所有內容都進行了驗證!任何其他建議? –

0

個人而言,我建議更直接和簡潔的東西。三元運算符一開始可能有點混亂,但一旦清楚理解就很簡單。我認爲它是一種可以嵌入到表達式中的「if-else」。

bool isDateValid(int month, int day, int year) 
{ 
    return year >= 1600 && year<= 2100 && 
      month >= 1 && month <= 12 && 
      day >= 1 && 
      day <= (month == 2 ? (leapyear(year) ? 29 : 28) : 
        month == 9 || month == 4 || month == 6 || month == 11 ? 30 : 31); 
        // "30 days has September, April, June and November..." 
} 

它可能是一個有點更具可讀性,如果你使用一個枚舉的一個月常量:

enum Month { Jan = 1, Feb, Mar, Apr ... }; 

目的是實際代碼:

day <= (month == Feb ? ...