2016-09-22 12 views
0

日付が有効かどうかを出力しようとしています.5-6-2013は有効ですが、62-34-2013は有効ではありません。正式な日付を(5,5,2013)でメインのオブジェクトに入れたら、スイッチを通ってうまく動作しますが、私がファンキーな日付(例えば、80,52,2013)を入れると出力します日付は「無効な日付」とは言いません ここには何がありますか? (私の明白なコメントはすべてコード全体で無視してください - 私はどこにいるのかを覚えて読みやすくしてくれます)ありがとう!日付が有効かどうか出力が表示されない

#include <iostream> 
#include <cmath> 
#include <string> 
#include <stdio.h> 

using namespace std; 

class dateType 
{ 
    // defining the class 

public: 

    void setDate(int month, int day, int year); 
    // Function to set the date 

    int getDay() const; 

    int getMonth() const; 

    int getYear() const; 

    void printDate() const; 

    dateType(int month, int day, int year); 
    dateType(); 

    bool isLeapYear(int yNumber); 
    int dMonth; 
    int dDay; 
    int dYear; 

private: 
    int yearCalc; 
    // integer to store the year, only numbers above 0 will be valid 
    int monthCalc; 
    // variable to store the month, January through December (1-12) 
    int dayCalc; 
    // variable to store the day, this varies depending on the month 
    // aka 28 days in February unless it is a leap year, 31 days January, etc. 
    int outputDate; 
    // 
}; 

// function definitions - setDate needs to check for validity 
void dateType::setDate(int month, int day, int year) 
{ 
    bool notValid; 
    // we need to use a switch in order to run through each month. 

    switch (month) 
    { 
     // months with 28 days - February (2), unless Leap year - test for Leap year 
    case 2: 
     cout << "February" << endl; 
     if (isLeapYear(year)) 
      cout << "Leap year - 29 days" << endl; 
     if (day > 29) 
      notValid = true; 
     break; 


     // months with 31 days - April (4), June (6), September (9) November(11) 
    case 4: 
     cout << "April - valid, 30 days" << endl; 
     break; 
    case 6: 
     cout << "June - valid, 30 days" << endl; 
     break; 
    case 9: 
     cout << "September - valid, 30 days" << endl; 
     break; 
    case 11: 
     cout << "November - valid, 30 days" << endl; 
     if (day > 30) 
      notValid = true; 
     break; 

     // months with 31 days - January (1), March (3), May (5) July(7), August (8), October (10), December (12) 
     // will not incude break statement as we do not want to break 
     // out of switch until the 31 day checks have been run through 
    case 1: 
     cout << "January - valid, 31 days" << endl; 
     break; 
    case 3: 
     cout << "March - valid, 31 days" << endl; 
     // printing out as a test to validate these months 
     break; 
    case 5: 
     cout << "May - valid, 31 days" << endl; 
     break; 

    case 7: 
     cout << "July - valid, 31 days" << endl; 
     break; 

    case 8: 
     cout << "August - valid, 31 days" << endl; 
     break; 

    case 10: 
     cout << "October - valid, 31 days" << endl; 
     break; 

    case 12: 
     cout << "December - valid, 31 days" << endl; 
     if (day >= 32) 
      notValid = true; 
     cout << "This is not a valid entry" << endl; 
     break; 
     // now break as we have checked all months ending in 31 days 


    } 
    dMonth = month; 
    dDay = day; 
    dYear = year; 
} 

int dateType::getDay() const 
{ 
    return dDay; 
} 

int dateType::getMonth() const 
{ 
    return dMonth; 
} 

int dateType::getYear() const 
{ 
    return dYear; 
} 

void dateType::printDate() const 
{ 
    cout << dMonth << "-" << dDay << "-" << dYear; 

} 

dateType::dateType(int month, int day, int year) 
{ 
    setDate(month, day, year); 
} 

bool dateType::isLeapYear(int yNumber) 

{ 

    if ((yNumber % 4 == 0 && yNumber % 100 != 0) || (yNumber % 400 == 0)) 
     return true; 
    else 
     return false; 
} 

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

    dateType year1(80, 52, 2013); 
    cout << "Date: "; 
     year1.printDate(); 

     system("pause"); 
     return 0; 


} 
+0

@JennyMarksあなたの質問に答えた場合、このサイトの仕組みはうまくいけば、あなたは答えを受け入れるでしょう:ここで、[誰かが私の質問に答えたらどうすればいいですか?](http: /stackoverflow.com/help/someone-answers)。 – user3286661

+0

@ user3286661投稿されたらすぐにやろうとしましたが、すぐに回答が得られたので、15分待ってからベスト回答を選ぶようにしました。私はベッドに行かなければならなかったので、今私は今それを行うためにオンラインになった、ありがとう –

答えて

0

このコードを試してみてください。

#include <iostream> 
#include <cmath> 
#include <string> 
#include <stdio.h> 

using namespace std; 

class dateType { 
    // defining the class 

public: 

    void setDate(int month, int day, int year); 
    // Function to set the date 

    int getDay() const; 

    int getMonth() const; 

    int getYear() const; 

    void printDate() const; 

    dateType(int month, int day, int year); 
    dateType(); 

    bool isLeapYear(int yNumber); 
    int dMonth; 
    int dDay; 
    int dYear; 

private: 
    int yearCalc; 
    // integer to store the year, only numbers above 0 will be valid 
    int monthCalc; 
    // variable to store the month, January through December (1-12) 
    int dayCalc; 
    // variable to store the day, this varies depending on the month 
    // aka 28 days in February unless it is a leap year, 31 days January, etc. 
    int outputDate; 
    // 
}; 

// function definitions - setDate needs to check for validity 
void dateType::setDate(int month, int day, int year) { 
    bool notValid; 
    // we need to use a switch in order to run through each month. 

    switch (month) { 
    // months with 28 days - February (2), unless Leap year - test for Leap year 
    case 2: 
    cout << "February" << endl; 
    if (isLeapYear(year)) 
     cout << "Leap year - 29 days" << endl; 
    if (day > 29) 
     notValid = true; 
    break; 


    // months with 31 days - April (4), June (6), September (9) November(11) 
    case 4: 
    cout << "April - valid, 30 days" << endl; 
    break; 
    case 6: 
    cout << "June - valid, 30 days" << endl; 
    break; 
    case 9: 
    cout << "September - valid, 30 days" << endl; 
    break; 
    case 11: 
    cout << "November - valid, 30 days" << endl; 
    if (day > 30) 
     notValid = true; 
    break; 

    // months with 31 days - January (1), March (3), May (5) July(7), August (8), October (10), December (12) 
    // will not incude break statement as we do not want to break 
    // out of switch until the 31 day checks have been run through 
    case 1: 
    cout << "January - valid, 31 days" << endl; 
    break; 
    case 3: 
    cout << "March - valid, 31 days" << endl; 
    // printing out as a test to validate these months 
    break; 
    case 5: 
    cout << "May - valid, 31 days" << endl; 
    break; 

    case 7: 
    cout << "July - valid, 31 days" << endl; 
    break; 

    case 8: 
    cout << "August - valid, 31 days" << endl; 
    break; 

    case 10: 
    cout << "October - valid, 31 days" << endl; 
    break; 

    case 12: 
    cout << "December - valid, 31 days" << endl; 
    break; 
    // now break as we have checked all months ending in 31 days 
    default: 
    cout << "This is not a valid entry" << endl; 
    break; 

    } 
    if (day >= 32) { 
     notValid = true; 
     cout << "This is not a valid entry" << endl; 
    } 
    dMonth = month; 
    dDay = day; 
    dYear = year; 
} 

int dateType::getDay() const { 
    return dDay; 
} 

int dateType::getMonth() const { 
    return dMonth; 
} 

int dateType::getYear() const { 
    return dYear; 
} 

void dateType::printDate() const { 
    cout << dMonth << "-" << dDay << "-" << dYear; 

} 

dateType::dateType(int month, int day, int year) { 
    setDate(month, day, year); 
} 

bool dateType::isLeapYear(int yNumber) 

{ 

    if ((yNumber % 4 == 0 && yNumber % 100 != 0) || (yNumber % 400 == 0)) 
    return true; 
    else 
    return false; 
} 

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

    dateType year1(80, 52, 2013); 
    cout << "Date: "; 
    year1.printDate(); 

    system("pause"); 
    return 0; 


} 

あなたはswitch(month)defaultケースを逃していました。これを移動する必要があります:

if (day >= 32) { 
    notValid = true; 
    cout << "This is not a valid entry" << endl; 
} 

スイッチブロック後。月が有効だが、日がそうでない場合、このケースを処理します。また、ifブロックに{}が見つかりませんでした。

+0

申し訳ありませんが、間違って間違ったコードを貼り付けました。今修正しました。 – user3286661

+0

アホハシ完璧。どうもありがとうございました。感謝します! :) –

0

月が指定した値の1つに含まれていない場合、月を設定する以外は何も行われません。だから、52の場合、case 52またはdefaultがないので、月を52に設定するだけです。それだけです。

関連する問題